Docker Ignore is not woking well with docker compose and my file structure

后端 未结 3 609
走了就别回头了
走了就别回头了 2021-02-06 13:21

I\'am setting up a new dockerfile and docker-compose.yml. I\'ll below show first my file structure with rails and I want to use a new .dockerignore to ignore node_modul

相关标签:
3条回答
  • 2021-02-06 13:31

    At build time, the directive COPY . . (inside the Dockerfile) correctly copies all files not listed in .dockerignore in $RAILS_ROOT (inside the image). No problem here (check that by running docker run --rm custom-web:1.0 ls -al).

    But here you run docker-compose to start the container, and you have defined a volume :

    volumes:
        - .:/var/www/${APP_NAME}_web
    

    That means that files from the same directory as docker-compose.yml are shared between the host and the container. That's why you find all files (even those listed in .dockerignore) in $RAILS_ROOT (workdir of custom-web:1.0 image) after starting the container via docker-compose.

    If you really need to share files between your host and the container (via a volume), I'll suggest you to mount the current directory in another location than the one specified in your Dockerfile, like :

    volumes:
        - .:/${APP_NAME}_web
    

    Otherwise, using COPY . . and a volume is redundant here.

    0 讨论(0)
  • 2021-02-06 13:32

    I faced the same problem with docker build, node_modules directories were not being ignored.

    SOLUTION
    Changing node_modules to **/node_modules inside .dockerignore solved my issue.

    0 讨论(0)
  • 2021-02-06 13:52

    Per this related answer you can mount your main directory but ignore the node_modules using a second anonymous volume in the docker-compose.yml. This allows you to mirror your directory but the container should keep its own node_modules.

    volumes:
      - .:/var/www/${APP_NAME}_web
      - /var/www/${APP_NAME}_web/node_modules/
    
    0 讨论(0)
提交回复
热议问题