1. Three ways to mount data from host to container
Docker provides three ways to mount data from the host to the container: Volumes: Docker manages part of the host file system (/var/lib/docker/volumes). The best way to save data. Bid mounts: Mount files or directories anywhere on the host into containers. tmpfs: Mounts are stored in the memory of the host system and not written to the host's file system. If you do not want to persist data anywhere, you can use tmpfs while avoiding writing to the Writable Layer of the Container to improve performance.
2.Volume
Create volume: docker volume create nginx-vol //View volume: docker volume ls docker volume inspect nginx-vol //Mount volume: docker run -d -p 89:80 --name=nginx-test --mount src=nginx-vol,dst=/usr/share/nginx/html nginx docker run -d -p 89:80 --name=nginx-test -v nginx-vol:/usr/share/nginx/html nginx //Delete volume: docker rm -f $(docker ps -a |awk '{print $1}') docker rm -f $(docker ps -qa) docker volume rm nginx-vol //Note: 1. If no volume is specified, it is automatically created. 2. Suggested use--mount,More general
3.Bind Mounts
Create a container with volumes: docker run -d -it --name=nginx-test --mount type=bind,src=/app/wwwroot,dst=/usr/share/nginx/html nginx docker run -d -it --name=nginx-test -v /app/wwwroot:/usr/share/nginx/html nginx //Verify the binding: docker inspect nginx-test //Clear: docker stop nginx-test docker rm nginx-test //Note: 1. If the source file/directory does not exist, it will not be created automatically and an error will be thrown. 2. If the mount target is a non-empty directory in the container, the existing contents of that directory will be hidden [root@localhost ~]# mkdir wwwroot;touch wwwroot/index.html [root@localhost ~]# docker run -d -p 89:80 --mount type=bind,src=$PWD/wwwroot,dst=/usr/share/nginx/html nginx 9c675487b319d6a723f2de35abd09c465aca6472b91e7232a9de6893012f3f63 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9c675487b319 nginx "nginx -g 'daemon of..." 9 seconds ago Up 8 seconds 0.0.0.0:89->80/tcp sad_robinson [root@localhost ~]# docker exec -it 9c675487b319 bash root@9c675487b319:/# ls /usr/share/nginx/html index.html root@9c675487b319:/# cat /usr/share/nginx/html/index.html root@9c675487b319:/# exit exit [root@localhost ~]# cat wwwroot/index.html [root@localhost ~]# echo "hello" >wwwroot/index.html [root@localhost ~]# docker exec -it 9c675487b319 cat /usr/share/nginx/html/index.html hello [root@localhost ~]# docker rm -f 9c675487b 9c675487b [root@localhost ~]# cat wwwroot/index.html hello [root@localhost ~]#