Docker Volume
What is a Volume?
If you try to run vi
to edit a file in a Nginx Docker container, you will find that vi
is not found. The reason is that only the necessary things will be included in the container. vi
is not one of them.
To edit files in a container conveniently, we use volume to "share" files between container and the real machine. A volume is like a "shared directory".
Commands
docker volume create
: create a volumedocker volume ls
: check all volumesdocker volume rm
: remove a volumedocker volume inspect
: check details of a certain volumedocker volume prune
: clear unused local volumesdocker volume --help
Create a Volume
You must add a volume to a container when calling docker run. If a container has already been created, you cannot add volumes to it. When you add a volume to a container, if the volume does not exist, it will be created automatically, which means you do not need to run docker volume create
by yourself.
To add a volume to a container:
docker run -d --name nginx -p 80:80 -v [unique_volume_name]:[directory_in_container] nginx
To check volumes:
docker volume ls
To see details of a volume, including the path on physical machine:
docker volume inspect [volume_name]
By default, the volume is located at /var/lib/docker/volumes/[volume_name]/_data
.
Now the directory in the physical machine and the one in the container are connected (doubly linked).