docker-cli-volumes

Contents

Roadmap info from roadmap website

Docker Volumes

Docker volumes are a mechanism for persisting data generated by and used by Docker containers. They allow you to separate the data from the container itself, making it easy to backup, migrate, and manage your persistent data.

Why Volumes are Important

Docker containers are ephemeral by nature, meaning they can be stopped, deleted, or replaced easily. While this is great for application development and deployment, it poses a challenge when dealing with persistent data. That’s where volumes come in. They provide a way to store and manage the data separately from the container’s lifecycle.

Types of Volumes

There are three types of volumes in Docker:

  • Host Volumes: They are stored on the host machine’s filesystem, usually in the /var/lib/docker/volumes directory. These can be easily accessed, but can pose issues with portability or file system compatibility.
  • Anonymous Volumes: These are created automatically when a container is run without specifying a volume. Their ID is generated by Docker and they are also stored on the host machine’s filesystem.
  • Named Volumes: Similar to anonymous volumes, named volumes are stored on the host machine’s filesystem. However, you can provide a custom name, making it easy to reference in other containers or for backups.

Volume Management with Docker CLI

Docker CLI provides various commands to manage volumes:

  • docker volume create: Creates a new volume with a given name.
  • docker volume ls: Lists all volumes on the system.
  • docker volume inspect: Provides detailed information about a specific volume.
  • docker volume rm: Removes a volume.
  • docker volume prune: Removes all unused volumes.

To use a volume in a container, you can use the -v or --volume flag during the docker run command. For example:

docker run -d --name my-container -v my-named-volume:/var/lib/data my-image

This command creates a new container named “my-container” using the “my-image” image and mounts the “my-named-volume” volume at the /var/lib/data path inside the container.

Docker Compose examples for each type of volume: Host Volumes, Anonymous Volumes, and Named Volumes

1. Host Volume

Host volumes are directories from the host machine that are mounted directly into the container.

version: '3'
services:
  app:
    image: nginx
    volumes:
      - /path/on/host:/path/in/container  # Host volume
    ports:
      - "8080:80"
  • In this example, /path/on/host is a directory on your host machine that is mounted to /path/in/container inside the container.

2. Anonymous Volume

Anonymous volumes are automatically created when no name is specified, and they are removed when the container stops unless explicitly preserved.

version: '3'
services:
  app:
    image: nginx
    volumes:
      - /path/in/container  # Anonymous volume
    ports:
      - "8080:80"
  • Here, Docker automatically creates an anonymous volume and mounts it at /path/in/container. The host path is managed by Docker, typically under /var/lib/docker/volumes/.

3. Named Volume

Named volumes are explicitly defined and can be easily referenced by other services or containers.

version: '3'
services:
  app:
    image: nginx
    volumes:
      - my_named_volume:/path/in/container  # Named volume
    ports:
      - "8080:80"

volumes:
  my_named_volume:  # Define the named volume
  • In this example, my_named_volume is a named volume that will persist even when the container is stopped or removed, making it easy to reuse or back up.

Summary

  • Host Volume: Explicitly maps a directory from the host machine into the container.

  • Anonymous Volume: Automatically created and managed by Docker without a specified name.

  • Named Volume: Defined by a name, making it easy to reference and manage across containers.

Each of these volume types has different use cases depending on the persistence and management requirements for your data.

Helpers

Get docker volumes size

docker system df --verbose --format '{{ range .Volumes }}{{ .Name }} {{ .Size }}\n{{ end }}'
#roadmap #docker #docker-cli #ready #online #commands