Docker Volume
March 3, 2025Less than 1 minute
Docker Volume
The runtime environment of the container provider should be decoupled from the data generated by the program and the configurations the program depends on. A volume is a virtual directory that acts as a bridge between the container's internal directory and the host's directory.
Data Mount Directory
/var/lib/docker/volumes/
docker run -d --name nginx -p 80:80 -v html:/usr/share/nginx/html nginx
-v html:/usr/share/nginx/html mounts the host's html directory to the container's /usr/share/nginx/html directory.
Mount Local Directory
docker run -d --name nginx -p 80:80 -v /Users/crc/Documents/IT-Study/Project/my-docs/src/posts/docker/html:/usr/share/nginx/html nginx
Mount local directory
-v local_directory:container_directory
Mount local file
-v local_file:container_file Note: The local directory or file must start with / or ./; if it starts directly with a name, it will be recognized as a volume name rather than a local directory name.
Command
Description | Command |
---|---|
Create a volume | docker volume create |
List all volumes | docker volume ls |
Remove a specific volume | docker volume rm |
Inspect a specific volume | docker volume inspect |
Remove all unused volumes | docker volume prune |
Notice!
The mounting of containers and data volumes must be configured when creating the container. For already created containers, data volumes cannot be set. Additionally, during the container creation process, data volumes are automatically created and do not need to be manually created.