Reputation: 12505
I don't want my nginx docker image to have /etc/nginx/conf.d/default.conf
so I wrote a simple nginx dockerfile, mainly to add RUN rm /etc/nginx/conf.d/default.conf
To my surprise it did not work. /etc/nginx/conf.d/default.conf is still there.
Why and how do I delete it ?
My dockerfile:
FROM nginx:latest
# Copy custom configuration file from the current directory
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/nginx.conf
COPY conf.d/node2 /etc/nginx/conf.d/node2
EXPOSE 80 8080
My docker-compose.yml for nginx service adds a name volumn
volumes:
- conf:/etc/nginx/conf.d
Update:
I finally found out that it is because I created a name volume in my docker-compose.yml that create /etc/nginx/conf.d/default.conf
so rm in dockerfile won't work at all. But why creating a name volume will result in creating `/etc/nginx/conf.d/default.conf ?
Update2:
With the second answer I got I finally figure out why. So RUN rm /etc/nginx/conf.d/default.conf
did work. The problem is I used a volume in my docker-compose.yml that was created before I run RUN rm /etc/nginx/conf.d/default.conf
and that volume has default.conf.
It happened like this, I first used a dockerfile without rm and run docker-compose up
, then I added rm to it to rebuild the image and run docker-compose up
again. But I never change my docker-compose.yml or clean my volume. So the file was there no matter how I build/rebuild my image.
After I deleted all the old volume (using docker volume rm docker volume ls -q -f dangling=true
) it finally works.
I leave my question here instead of deleting it just in case someone else may make the same mistake like I did :$
Upvotes: 2
Views: 10352
Reputation: 265190
Volumes are a run time change to the filesystem. The build runs without your volume to create the image. Therefore the file is deleted in the image, but then you mount a volume in your container that has the file.
The fix is to either not use a volume so you can see the filesystem as it exists in the image, or you can delete the file from the volume. The following should work for cleaning the file from the volume:
docker run -it --rm -v ${vol_name}:/target busybox rm /target/default.conf
Upvotes: 0
Reputation: 5076
I don't think you're using your new image.
I tested it by simply creating a new Dockerfile:
FROM nginx:alpine
RUN rm /etc/nginx/conf.d/default.conf
and I've executed the following commands:
docker build -t test-nginx .
docker run -it --name mynginx-instance test-nginx ls -la /etc/nginx/conf.d
docker rm mynginx-instance
Upvotes: 2