Step-by-Step Guide to Create, Bind, and Mount Docker Volumes on Linux

Teslim LawalTeslim Lawal
1 min read

Volumes in Docker allow you to persist data and share files between the host system and containers.

Prerequisites:

Docker installed on linux

Step 1:

Create a named volume.

docker volume create my_volume

Confirm that the volume has been created.

docker volume ls

Inspect the volume.

docker volume inspect my_volume

Step 2:

Create a directory.

mkdir ~/host_data
echo "Hello from host!" > ~/host_data/test.txt

Create a container with a bind mount.

docker run -it -v ~/host_data:/container_data ubuntu bash

Confirm the container.

ls /container_data
cat /container_data/test.txt

Result.

test.txt
Hello from host!

Change the message and confirm if it works.

echo "Modified inside container!" >> /container_data/test.txt

Exit the container (exit).

Check the file on the host.

cat ~/host_data/test.txt

Result.

Modified inside container!

Step 3:

Run a container with the named volume.

docker run -it -v my_volume:/data ubuntu bash

Create a file inside the volume.

echo "This is stored in Docker Volume" > /data/volume_test.txt
exit

Read into the file.

cat /data/volume_test.txt

Result.

This is stored in Docker Volume

Mount.

docker run -it \
  --mount type=bind,source=$(pwd)/host_data,target=/container_data \
  ubuntu bash
0
Subscribe to my newsletter

Read articles from Teslim Lawal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Teslim Lawal
Teslim Lawal