A Docker volume is a handy way to deploy a container with persistent storage or to share data between two containers. With this handy trick, you could deploy multiple websites that use the same volume housing their data. It’s a great feature that helps to give Docker more flexibility. I want to show you how to create and use a Docker volume, so you can enjoy simplified data sharing and persistent data with your containers.

The first thing you must do is create a new volume. Let’s call it shared-data, and the command for this would be

docker volume create --name shared-data

The directory housing that volume on Linux is /var/lib/docker/volumes.

Let’s deploy a Ubuntu container that uses the new volume with the command

docker run -ti -v shared-data:/shared-data ubuntu

This will land us on the running container’s bash prompt where we can create a file in the shared directory with the command

echo "Hello, TechRepublic" >> /shared-data/test.txt

Exit from that container, and stop it with the

exit

command.

Let’s deploy a second container that uses the same volume with the command

docker run -ti -v shared-data:/shared-data ubuntu

You should see that the new container has the test.txt file that was saved within the volume. Test that with the command

cat /shared-data/test.txt

and you should see Hello, TechRepublic.

Congratulations! You have just created your first Docker volume that can be used for any number of containers.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

If you are interested in learning more about Docker, check out the following resources in TechRepublic Academy:

Subscribe to the Cloud Insider Newsletter

This is your go-to resource for the latest news and tips on the following topics and more, XaaS, AWS, Microsoft Azure, DevOps, virtualization, the hybrid cloud, and cloud security. Delivered Mondays and Wednesdays

Subscribe to the Cloud Insider Newsletter

This is your go-to resource for the latest news and tips on the following topics and more, XaaS, AWS, Microsoft Azure, DevOps, virtualization, the hybrid cloud, and cloud security. Delivered Mondays and Wednesdays