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
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.
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.
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/