Docker volumes provide a convenient way to persist data generated by Docker containers. Volumes can be shared across containers, making them ideal for scenarios where data needs to be stored and accessed even when containers are stopped or recreated. In this tutorial, we will explore various commands and techniques for working with Docker volumes.
Step 1: Create a Docker Volume
To create a Docker volume, use the docker volume create
command. Specify a name for the volume, for example, “my-volume”, by running the following command:
docker volume create --name my-volume
Step 2: List Docker Volumes
To view a list of all available Docker volumes, use the docker volume ls
command:
docker volume ls
You should see the newly created volume, “my-volume”, listed.
Step 3: Run a Container with a Docker Volume
Now, let’s run a container and mount the created volume to it. We’ll use the docker run
command with the -v
or --mount
flag.
docker run -it --name my-container -v my-volume:/data <image-name>
Replace <container-name>
with your desired name for the container and <image-name>
with the name of a sample Docker image you want to use.
Alternatively, you can use the --mount
flag:
docker run -it --name my-container --mount source=my-volume,destination=/data <image-name>
This command runs a container named “my-container” and mounts the “my-volume” Docker volume to the /data
path inside the container.
Step 4: Remove a Docker Volume
To remove a Docker volume, use the docker volume rm
command followed by the volume name. Be cautious as this action permanently deletes the volume and its data.
docker volume rm my-volume
Step 5: Mount a Host Directory as a Docker Volume
You can also mount a directory from the host machine as a Docker volume. Use the -v
flag with the absolute path of the directory on the host and the path inside the container where the volume should be mounted.
docker run -it -v /path/on/host:/path/in/container <image-name>
Replace /path/on/host
with the actual path on your host machine and /path/in/container
with the desired path inside the container.
Step 6: Bind Mount a Host Directory as a Docker Volume
Another way to mount a host directory as a Docker volume is by using the --mount
flag with the type=bind
option. This technique provides more flexibility in terms of specifying additional options.
docker run -it --name my-container --mount type=bind,source=/path/on/host,target=/path/on/container <image-name>
Replace /path/on/host
with the actual path on your host machine and /path/on/container
with the desired path inside the container.
A Practical example
Let’s see a practical example about using Volumes with Docker. First we create a docker volume and then we mount volume to the docker container in order to persist data.
docker volume create postgres-volume
Now we mount the docker volume to the container once we run the container.
docker run --name learn_postgres -e POSTGRES_PASSWORD=docker_user -e POSTGRES_USER=docker_user -p 5433:5432 -v pgdata:/var/lib/postgresql/data -d postgres
To learn more about using PostgreSQL with Docker check this article: How to manage a PostgreSQL Database with Docker
Congratulations! You have successfully learned the basic usage of Docker volumes. By leveraging Docker volumes, you can persist data across container lifecycles and easily share data between containers or with the host machine.