Docker Basics

Essential Docker system commands for beginners

Docker Volume Management

Docker volumes are the preferred mechanism for persisting data generated by and used by Docker containers. Volumes are completely managed by Docker and have several advantages over bind mounts.

Key Benefits:

  • Volumes are easier to back up or migrate than bind mounts
  • You can manage volumes using Docker CLI commands or the Docker API
  • Volumes work on both Linux and Windows containers
  • Volumes can be more safely shared among multiple containers
  • Volume drivers let you store volumes on remote hosts or cloud providers

Working with Docker Volumes

Follow these steps to create, use, and manage Docker volumes.

1

Create a Volume

docker volume create myvol

Creates a new Docker volume named "myvol". Volumes are the preferred way to persist data in Docker containers.

Explanation:

  • docker volume create → Command to create a new volume
  • myvol → Name of the volume to create
2

List Volumes

docker volume ls

Lists all Docker volumes on your system, showing their names and drivers.

Usage:

Use this command to see all available volumes and verify that your volume was created successfully.

3

Run Container with Volume

docker run -d --name voltest -v myvol:/app nginx:latest

Starts a new Nginx container named "voltest" that uses the "myvol" volume mounted at the "/app" directory inside the container.

Explanation:

  • docker run → Start a new container
  • -d → Detached mode (runs in background)
  • --name voltest → Names the container "voltest"
  • -v myvol:/app → Mounts volume "myvol" to "/app" directory in container
  • nginx:latest → The image to use
4

Connect to Container

docker exec -it voltest bash

Opens an interactive bash shell inside the running "voltest" container.

Explanation:

  • docker exec → Execute a command in a running container
  • -it → Interactive mode with pseudo-TTY
  • voltest → Container name to connect to
  • bash → Command to run inside container
5

Install Nano and Create File

First, update package lists and install Nano:

apt-get update
apt-get install nano

Then navigate to the app folder and create a file:

cd /app
nano test.txt

Updates package lists, installs Nano text editor, navigates to the /app directory (where our volume is mounted), and creates a text file using Nano.

Nano Commands:

  • Type some text in the editor
  • CTRL-O → Write out (save) the file
  • CTRL-X → Exit Nano
  • exit → Exit the container shell
6

Stop and Remove Container

First, stop the container:

docker stop voltest

Then remove the container:

docker rm voltest

Stops the "voltest" container and then removes it. The volume persists independently of the container.

Note:

Even though we're removing the container, the volume "myvol" and the file we created ("test.txt") still exist because volumes are independent of container lifecycle.

7

Verify Data Persistence

Run a new container with the same volume:

docker run -d --name voltest -v myvol:/app nginx:latest

Check if the file still exists:

docker exec -it voltest bash
cd /app
cat test.txt

Creates a new container using the same volume and checks if the file we created earlier still exists with its content intact.

Result:

The "test.txt" file should still exist with the content you previously added, demonstrating that Docker volumes persist data independently of containers.

8

Cleanup

Stop and remove the container:

docker stop voltest
docker rm voltest

Remove the volume:

docker volume rm myvol

Stops and removes the container, then removes the volume to clean up resources.

Note:

You cannot remove a volume that is currently in use by a container. Make sure to stop and remove any containers using the volume first.

Summary

This exercise demonstrates how Docker volumes work and how they provide persistent storage that survives container lifecycle changes. Key takeaways:

  • Volumes are created independently of containers
  • Data stored in volumes persists even when containers are removed
  • Multiple containers can use the same volume simultaneously
  • Volumes need to be explicitly removed when no longer needed
  • This makes volumes ideal for databases, file storage, and other persistent data needs