Docker volume command

In this tutorial, How to use Docker volume command to details for Data volumes. There are three main use cases for Docker data volumes:

  • Keep data around when a container is removed
  • To share data between the host filesystem and the Docker container
  • To share data with other Docker containers

Docker volume command

Docker volume create

The syntax docker volume commands as below:

$ docker volume create [--option] NAME_VOLUME

Related docker volume command:

  • docker volume create: Create a volume docker volume inspect: Display detailed information on one or more volumes
  • docker volume ls: List volumes docker volume prune: Remove all unused volumes docker
  • volume rm: Remove one or more volumes

How to create a new volume and configure the container

$ docker volume create DATA
$ docker run -d -v DATA:/world centos ls /world

How to inspect container ID with docker inspect command

$ sudo docker inspect a04ae7b6cc17

For example, the output as below

"Mounts": [
 {
 "Name": "0ed908df33e1790711933ad6b463fd2a88199802c5cf2b1beb5001b81bc27750",
 "Source": "/var/lib/docker/volumes/0ed908df33e1790711933ad6b463fd2a88199802c5cf2b1beb5001b81bc27750/_data",
 "Destination": "/var/www/html",
 "Driver": "local",
 "Mode": "",
 "RW": true,
 "Propagation": ""
 },

To use docker inspect display Source and Destination for container ID

$ sudo docker inspect -f '{{ .Mounts}}' a04ae7b6cc17 | awk '{ print "Source:"$2 "\t " "Destination:"$3}'

The output below:

Source:/var/lib/docker/volumes/0ed908df33e1790711933ad6b463fd2a88199802c5cf2b1beb5001b81bc27750/_data Destination:/var/www/html

How to use docker volume ls to list all volume for container

$ sudo docker volume ls

The output as below

[sudo] password for huupv:
 DRIVER VOLUME NAME
 local 0ed908df33e1790711933ad6b463fd2a88199802c5cf2b1beb5001b81bc27750
 local 1d4c9030aea8554335b281d554a27422773f067f77ead5663e9b52f85d64ff6f
 local 387a0f11796f6d6d254abbbdf523410a42ed004dc5879c59ae45f5c922038441
 local 85621d725c9cee6444ab83162123000dd18bdd9a0a974a3cbe05b60d0175bc33
 local bbaaa1ea0d348d1e66ae6f606396502d895db8f6f2cd6f1cc9ca34605da3cf3c
 local devopsroles
 local e1f8e62ec711164a363bbc9bc53a0868e093dc7e4983fb327fe5f77b4e48d965
 local wordpress_db-data
 local wordpress_nginx-data
 local wordpress_php-data
 local wordpress_wp-data

How to use docker inspect exploit for container ID a04ae7b6cc17

$ sudo docker inspect -f '{{ (index .Mounts 0).Source }}' a04ae7b6cc17

The output Mounts source container ID a04ae7b6cc17

/var/lib/docker/volumes/0ed908df33e1790711933ad6b463fd2a88199802c5cf2b1beb5001b81bc27750/_data

Other dockers inspect command Mounts source container ID a04ae7b6cc17

$ sudo docker inspect --format '{{ range .Mounts }}{{ if eq .Destination "/var/www/html" }}{{ .Source }}{{ end }}{{ end }}' a04ae7b6cc17

How to create a Docker Volume Using a Dockerfile

$ vim Dockerfile

The content as below

# Create a volume
 VOLUME /dockerfilevolume

Next, build an image named dockerfile-volumetest from this Dockerfile with the command:

$ sudo docker build -t dockerfile-volumetest .

Then launch a container named “my-dockerfile-test” from this image with the command:

$ sudo docker run --name my-dockerfile-test -it dockerfile-volumetest /bin/bash

Detach from the container with Ctrl-p + Ctrl-q and return to the host machine’s command prompt.

Sharing Volumes Between Containers

Sharing a Volume on the Host

$ sudo mkdir /webdata
$ sudo docker run -it --name webapp -v /webdata:/var/www/html nginx /bin/bash

Using a Container as a Shared Data Volume

$ sudo docker run -it -v /shared-data --name data-storage centos /bin/bash
$ echo "Hello from the data-storage container." >> /shared-data/data-storage-hello.txt
$ sudo docker run -it --name app --volumes-from data-storage python /bin/bash

Mounting a Volume as Read-Only

$ docker run -v /directory:/path:ro
$ sudo docker volume create --name limited-access
$ sudo docker run -it --name allowed-to-write -v limited-access:/data centos /bin/bash

Conclusion

Thought the article, help you to details for data volume. Docker volume command helps useful to determine for containers. The update about docker volume command later. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.