For data that needs to be persisted outside the container, you need to use mount
Early dockers could only mount the local disk path, which is the first way below
The first way is to mount the local path directly
docker run -d -it --name=nginx1 -p 8001:80 -v ~/tmp:/usr/share/nginx/html nginx
In this way, the docker will not cover any file contents in the local path
In other words, if there are no files in the local path TMP, then after mounting, the container will not have any files in the mounted path
This is a way to use local content to override the content of the container’s corresponding path
The second way is to create a volume managed by docker
docker volume create tmpvol
docker run -d -it --name=nginx2 -p 8002:80 -v tmpvol:/usr/share/nginx/html nginx
The convenience of this method is that it can persist the data and continue to use the contents of the container
This is a way to store the container content to the specified volume
On Linux, it’s very easy to access this path directly from the host
The path is
On MacOS, it is more complex, because the docker desktop of MacOS is also based on virtual machine
After the old version of docker desktop is attached to TTY with the following command, you can access the above path similar to Linux
screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty
However, in version 3.1.0, I didn’t find this TTY file
In fact, this operation has become simpler than before
Notice the CLI icon in the upper right corner?
But this method is not on the host, but provided by docker exec