Search code examples
dockerdocker-volume

When I use Docker, I directly bind a file between the host and the container,but the two files cannot be synchronized


I start a docker container with the followings:

cd /root 

docker run -it -d --privileged=true --name nginx nginx 

rm -fr dockerdata 

mkdir dockerdata 

cd dockerdata 

mkdir nginx 

cd nginx 

docker cp nginx:/usr/share/nginx/html . 

docker cp nginx:/etc/nginx/nginx.conf . 

docker cp nginx:/etc/nginx/conf.d ./conf 

docker cp nginx:/var/log/nginx ./logs 

docker rm -f nginx 

cd /root 

docker run -it -d -p 8020:80 --privileged=true --name nginx \
-v /root/dockerdata/nginx/html:/usr/share/nginx/html \
-v /root/dockerdata/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /root/dockerdata/nginx/conf:/etc/nginx/conf.d \
-v /root/dockerdata/nginx/logs:/var/log/nginx \
nginx

"docker inspect nginx" is followings

HostConfig-Binds

The bound directory can be synchronized, but directly bound files like "nginx. conf" cannot be synchronized. When I modify the "nginx. conf" in the host, the "nginx. conf" in the container does not change.

I want to know why this happens and how I can directly bind a single file between the host and the container.##


Solution

  • why this happens

    Mount bind mounts the file to inode. The nginx entrypoint executes in https://github.com/nginxinc/docker-nginx/blob/ed42652f987141da65bab235b86a165b2c506cf5/stable/debian/30-tune-worker-processes.sh :

    sed -i.bak
    

    sed creates a new file, then moves the new file to the old one. The inode of the file changes, so it's no longer mounted inode.

    how I can directly bind a

    It is bind. Instead, you should consider re-reading nginx docker container documentation on how to pass custom config to it:

    -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro
                                                  ^^^
    

    Which does skip sed at https://github.com/nginxinc/docker-nginx/blob/ed42652f987141da65bab235b86a165b2c506cf5/stable/debian/30-tune-worker-processes.sh#L12 .