简体   繁体   中英

Docker-compose - How to populate a container data to another container?

My docker-compose file is as follows:

version: '3'

services:
  database:
    image: postgres
    restart: always
    environment:
      POSTGRES_PASSWORD: qwerty
      POSTGRES_USER: qwerty

  backend:
    depends_on:
      - database
    build:
      #Dockerfile used here will use python image,build the django project and run using uwsgi in port 4000
      context: .
      dockerfile: dockerfile_uwsgi
    ports:
      - "4000:4000"
    image: backend_img
    environment:
      DB_HOST: database
      DB_NAME: qwerty
      DB_USER: qwerty
      DB_PASSWORD: qwerty

  migration:
    depends_on:
      - backend
    image: backend_img
    entrypoint: ["sh", "-c"]
    command: ["
      python manage.py collectstatic --noinput;
      python manage.py makemigrations;
      python manage.py migrate;"]
    environment:
      DB_HOST: database
      DB_NAME: qwerty
      DB_USER: qwerty
      DB_PASSWORD: qwerty

  frontend:
    depends_on:
      - backend
    build:
      #The dockerfile used her uses nginx image, it is configured to act as reverse proxy and serve static files.
      context: .
      dockerfile: dockerfile_nginx
    ports:
      - "9443:8443"


Explaination about docker-compose.yaml : Here the backend container setups the django project and serves the project using uwsgi, using same image the migration container will collect the static files from all the app directories and populates it into the current working directory of the container. The frontend container is a nginx, which acts as reverse proxy. Also i would like to serve static files from nginx container.

The issue im facing here is, i want to the static files which is created by the migration container to appear in frontend container. So that nginx can serve the static files. How this could be done?. If suppose the design is not supposed to be how it is shown here, please suggest me how could this be re-designed to achieve the requirement?

I know that using shared volume this could be done. But i do not want to use a shared volume since the data populated to shared volume will persist in it and suppose if developer modifies the static content in the app folder, the changes will not populated to the volume, unless the volume mount point is flused. This is based what i have obseved, please correct me if im wrong.

Whatever's serving your resources at the docker layer - gunicorn, uwsgi, whatever - will probably support serving static assets, and can do it significantly more efficiently than django itself.

In your situation, nginx is essentially external to your application. Instead of trying to "get your static assets into nginx", let the clients do that job and cache them in nginx once proxied. Nginx has good caching support.

If you really want to get the static assets into a, you can COPY --from=... like in a https://docs.docker.com/develop/develop-images/multistage-build/ to copy the static assets into your custom nginx container. Use the django container as the source - you'll have to make sure its built after your django container. This may not be possible completely within docker-compose. There's legitimate friction there; you'll have the same friction when/if you try to build and deploy docker artifacts to production servers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM