Docker - Can't share data between containers within a volume (docker-compose 3)

后端 未结 2 647
既然无缘
既然无缘 2021-02-07 20:15

I have some containers for a web app for now (nginx, gunicorn, postgres and node to build static files from source and a React server side rendering). In a Dockerfile for the no

相关标签:
2条回答
  • 2021-02-07 20:37

    Explanation of what happens step by step

    Dockerfile.node

        ...
        COPY ./client /usr/src
        ...
    

    docker-compose.yml

    services:
      ...
      node:
        ...
        volumes:
          - ./server/nginx.conf:/etc/nginx/nginx.conf:ro
          - vnode:/usr/src
      ...
    volumes:
      vnode:
    
    1. docker-compose up creates with this Dockerfile.node and docker-compose section a named volume with data saved in /usr/src.

    Dockerfile.nginx

    FROM nginx:latest
    
    COPY ./server/nginx.conf /etc/nginx/nginx.conf
    
    RUN mkdir -p /var/www/tg/static
    
    EXPOSE 80
    EXPOSE 443
    
    CMD ["nginx", "-g", "daemon off;"]
    
    1. That produces that nginx containers created with docker-compose will have an empty /var/www/tg/static/

    docker-compose.yml

     ...
     nginx:
        build:
          context: .
          dockerfile: ./Dockerfile.nginx
        container_name: tg_nginx
        restart: always
        volumes:
          - ./server/nginx.conf:/etc/nginx/nginx.conf:ro
          - vnode:/var/www/tg/static
        ports:
          - "80:80"
          - "443:443"
        depends_on:
          - node
          - gunicorn
        networks:
          - nw_web_tg
    
     volumes:
       vdata:
       vnode:
    
    1. docker-compose up will produce that vnode named volume is created and filled with data from /var/www/tg/static (empty by now) to existing vnode.

    So, at this point, - nginx container has /var/www/tg/static empty because it was created empty (see mkdir in Dockerfile.nginx) - node container has /usr/src dir with client file (see that was copied in Dockerfile.node) - vnode has content of /usr/src from node and /var/www/tg/static from nginx.

    Definitively, to pass data from /usr/src from your node container to /var/www/tg/static in nginx container you need to do something that is not very pretty because Docker hasn't developed another way yet: You need to combine named volume in source folder with bind volume in destination:

     nginx:
         build:
           context: .
           dockerfile: ./Dockerfile.nginx
         container_name: tg_nginx
         restart: always
         volumes:
           - ./server/nginx.conf:/etc/nginx/nginx.conf:ro
           - /var/lib/docker/volumes/vnode/_data:/var/www/tg/static
         ports:
           - "80:80"
           - "443:443"
         depends_on:
           - node
           - gunicorn
         networks:
           - nw_web_tg
    

    Just change in docker-compose - vnode:/var/www/tg/static by - /var/lib/docker/volumes/vnode/_data:/var/www/tg/static

    0 讨论(0)
  • 2021-02-07 20:54

    Two changes you need. In your node service add the volume like:

    volumes:
      - vnode:/usr/src/bundle_client
    

    Since you want to share /usr/src/bundle_client you should NOT be using /usr/src/ because that will share the full folder and the structure too.

    And then in your nginx service add the volume like:

    volumes:
      - type: volume
        source: vnode
        target: /var/www/test/static
        volume:
          nocopy: true
    

    The nocopy: true keeps our intention clear that on initial map of the container the content of the mapped folder should not be copied. And by default the first container to get mapped to the volume will get the contents of the mapped folder. In your case you want this to be the node container.

    Also before testing make sure you run below command to kill the cached volumes:

    docker-compose down -v
    

    You can see during my test the container had the files:

    0 讨论(0)
提交回复
热议问题