Managing Volumes and Data in Docker

Tutorial 4 of 5

Introduction

This tutorial will guide you on how to manage volumes and data in Docker. Docker volumes are the preferred way to handle persistent data created by and used by Docker containers. We will discuss Docker volumes and bind mounts, understand their differences, and learn how to use them effectively.

By the end of this tutorial, you should be able to:
- Understand Docker volumes and bind mounts
- Create and manage Docker volumes
- Use bind mounts
- Handle data persistence in Docker

Prerequisites
Basic knowledge of Docker and familiarity with the command line interface is needed.

Step-by-Step Guide

Docker Volumes

Docker volumes are created and managed by Docker. They are a way to persist data generated by and used by Docker containers. Docker volumes are completely managed by the Docker CLI and are stored in a part of the host filesystem which is managed by Docker (/var/lib/docker/volumes/... on Linux).

Creating a volume

docker volume create my-vol

Listing volumes

docker volume ls

Bind Mounts

Bind mounts have been around since the early days of Docker. Bind mounts have limited functionality compared to volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its full or relative path on the host machine.

Using bind mounts

docker run -d --name devtest --mount type=bind,source="$(pwd)"/target,target=/app nginx:latest

Code Examples

  1. Creating and using Docker Volumes
# Create a volume
docker volume create my-vol

# Run a container and mount the volume to it
docker run -d --name my-container -v my-vol:/app nginx:latest

In the above example, we first create a Docker volume named 'my-vol', then we run a Docker container and mount the volume to the '/app' directory in the container.

  1. Using Bind Mounts
# Run a container and bind mount the current directory to it
docker run -d --name devtest --mount type=bind,source="$(pwd)"/target,target=/app nginx:latest

In this example, we run a Docker container and bind mount the current directory (source="$(pwd)"/target) to the '/app' directory in the container.

Summary

In this tutorial, we learned about Docker volumes and bind mounts, their differences, and how to use them effectively. Docker volumes are a way to persist data generated by and used by Docker containers, bind mounts, on the other hand, are a way to mount a file or directory from the host machine into a container.

Practice Exercises

  1. Exercise 1: Create a Docker volume and run a container with the volume attached to it. Check the contents of the volume from within the container.

Solution:
```bash
# Create a volume
docker volume create ex1-vol

# Run a container and mount the volume to it
docker run -d --name ex1-container -v ex1-vol:/app nginx:latest

# Go into the container
docker exec -it ex1-container /bin/bash

# Check the contents of the '/app' directory
ls /app
```

  1. Exercise 2: Run a container and bind mount a directory from your host machine to it. Check the contents of the mounted directory from within the container.

Solution:
```bash
# Run a container and bind mount the current directory to it
docker run -d --name ex2-container --mount type=bind,source="$(pwd)"/target,target=/app nginx:latest

# Go into the container
docker exec -it ex2-container /bin/bash

# Check the contents of the '/app' directory
ls /app
```

For further practice, try creating and managing multiple Docker volumes and bind mounts, and experiment with persisting data across container restarts and removals.