Summary
With docker-compose, mounting a volume with a destination directory that exists in the Nginx base image, works as expected. However, I get an error with docker-compose up
when the mount target is a subdirectory created in the derived image. Here's the gist of the error:
mounting "newdir" to rootfs at "/usr/share/nginx/html/newdir" caused: mkdir newdir: read-only file system: unknown
I don't understand why it seems to be attempting to create the directory (newdir) when it already exists!
Details
Full error below:
ERROR: for test_nginx_1 Cannot start service nginx: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting "/home/user/test/newdir" to rootfs at "/usr/share/nginx/html/newdir" caused: mkdir /var/lib/docker/overlay2/accdaa147d7825a7d2c71b68f76e0b8663d00f6e46fcfb5c0ec32ada8baf85af/merged/usr/share/nginx/html/newdir: read-only file system: unknown
ERROR: for nginx Cannot start service nginx: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting "/home/user/test/newdir" to rootfs at "/usr/share/nginx/html/newdir" caused: mkdir /var/lib/docker/overlay2/accdaa147d7825a7d2c71b68f76e0b8663d00f6e46fcfb5c0ec32ada8baf85af/merged/usr/share/nginx/html/newdir: read-only file system: unknown.
Here is the docker-compose.yml:
version: '3'
services:
nginx:
image: local/mynginx:withsubdir
volumes:
- ./html:/usr/share/nginx/html:ro
- ./newdir:/usr/share/nginx/html/newdir
The above image, local/mynginx:withsubdir
, was built with the following Dockerfile:
FROM docker.io/library/nginx:1.21
RUN mkdir /usr/share/nginx/html/newdir && ls -lah /usr/share/nginx/html/newdir
And I verified that I can mount the volume and see its content with docker:
$ docker run --rm -ti -v /home/user/test/newdir:/usr/share/nginx/html/newdir local/mynginx:withsubdir bash
root@7db709476271:/# ls /usr/share/nginx/html/newdir
hallo.imhere
So the issue seems specific to docker-compose. If I remove the last line in docker-compose.yml then docker-compose up
starts successfully. How can I mount the directory created in the derived image? Thank you.
Extra Notes
By the way, my original docker-compose.yml is longer and it has a build step that points to the Dockerfile that creates that new directory. But I created this smaller reproducer while troubleshooting, and I've built the new image manually with:
docker build . -t local/mynginx:withsubdir
I get the same error in two different environments (Docker version 20.10.7, build 20.10.7-0ubuntu1~20.04.1 and Docker version 20.10.12, build e91ed5707e), with or without :ro
.
My test
directory files:
.
├── docker-compose.yml
├── Dockerfile
├── html
│ └── index.html
└── newdir
└── hallo.imhere
I did some online research but could not find any solution.