Docker Introduction and Application Management Application Data

Keywords: Linux Nginx Docker

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.


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

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 
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
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
9c675487b319        nginx               "nginx -g 'daemon of..."   9 seconds ago       Up 8 seconds>80/tcp   sad_robinson
[root@localhost ~]# docker exec -it 9c675487b319 bash
root@9c675487b319:/# ls /usr/share/nginx/html
root@9c675487b319:/# cat /usr/share/nginx/html/index.html 
root@9c675487b319:/# 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
[root@localhost ~]# docker rm -f 9c675487b
[root@localhost ~]# cat wwwroot/index.html 
[root@localhost ~]#

Posted by Gutspiller on Thu, 10 Oct 2019 14:34:35 -0700