简体   繁体   English

使用 Docker 容器 - Django 构建后维护媒体文件

[英]Maintain the media files after build with Docker Container - Django

I am using Django as a Web Framework.我正在使用 Django 作为 Web 框架。 Azure NGINEX as WebServer. Azure NGIEX 作为 WebServer。 My Project is deployed with Docker Containers.我的项目部署了 Docker 容器。

In my Django project root structure will be as follows:在我的 Django 项目中,根结构如下:

root:
  - app1
  - app2
  - media

whenever saving images, it will correctly save under media folder.每当保存图像时,它都会正确保存在媒体文件夹下。 But whenever doing "docker-compose up" it will replaces the source code, so that my media folder will be cleaned up everytime.但是每当执行“docker-compose up”时,它都会替换源代码,这样我的媒体文件夹就会每次都被清理干净。

In my settings.py file, I have added as follows:在我的 settings.py 文件中,我添加了如下内容:

 MEDIA_ROOT = os.path.join(BASE_DIR,'media')
 MEDIA_URL = 'media/'

Kindly help me to maintain the media files with Docker based Environment请帮助我使用基于 Docker 的环境维护媒体文件

You have to basically do 2 things:你必须基本上做两件事:

  • Changing the directory to keep the media files to somewhere out of your source code directory - it's not any good practice to keep them in the source directory because of several reasons,更改目录以将媒体文件保存在源代码目录之外的某个位置 - 由于多种原因,将它们保存在源目录中并不是任何好的做法,
  • Using docker volumes in your docker-compose configuration file to persist your media directory.在您的docker-compose配置文件中使用 docker 卷来保存您的媒体目录。 You can find the detailed documentation on the way to configure volumes in docker-compose here .您可以在此处找到有关在docker-compose中配置卷的方式的详细文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django 应用程序的媒体文件在哪里存储,以便在 docker 容器更新后保存它们? - Where to store media files of Django app in order to save them after docker container updating? 如何使用 docker 和 nginx 服务 django 媒体文件? - How to serve django media files with docker and nginx? 使用 Django 在 Docker 中设置默认媒体文件 - Set default media files in Docker using Django Django 从 docker 容器中的 FileField 保存文件 - Django save files from FileField in docker container Django static 文件在 docker 容器内找不到 - Django static files are not found inside docker container 如何使用 Django、Nginx、Docker 和 Traefik 管理媒体文件? - How to manage to serve media files with Django, Nginx, Docker and Traefik? 将文件上传到 Django + Nginx 不会将其保存在 Docker 的媒体卷中 - Uploading files to Django + Nginx doesn't save it in the media volume in Docker 在 Docker 容器中运行时,Django 静态文件不提供服务 - Django static files not serving when running inside Docker container Django静态文件未复制到Docker容器上的saticfiles文件夹中 - django static files are not copied to saticfiles folder on docker's container 在heroku上部署django项目后看不到媒体文件 - Media files are not visible after deploying the django project on heroku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM