Skip to content

Docker: Volumes

Rayan edited this page Mar 20, 2024 · 10 revisions

Docker Volumes

Docker volumes are a mechanism for persisting and sharing data between Docker containers and the host machine, providing a flexible and scalable solution for managing data in containerized environments.

Definition

Docker volumes are a mechanism for persisting and sharing data between Docker containers and the host machine, providing a flexible and scalable solution for managing data in containerized environments.

This sentence accurately describes docker volumes, however there is a lot more to docker volumes. I will go into more detail down below.

Docker volumes are vital components of a docker environment architecture, serving as a kind of a bridge between containers and the underlying host system's storage. This bridge allows the docker environment resp. docker containers to have persistent data. They also enable containers to access and manipulate data stored outside their isolated environments, thus creating data persistence, sharing and synchronization across container instances. This makes horizontal scaling possible, since the instances/nodes that are automatically created still use the same data. This is a .yaml file comes into play. See following documentation to see more.

📄Docker Documentation: Persist Container Data

Use Cases

As you can interpret from the text above, persistent data is made possible through docker volumes but why do this? These are some different use cases for using docker volumes:

Use Cases

As you can interpret from the text above, persistent data is made possible through docker volumes, but why do this? Docker volumes serve several critical purposes in containerized environments, enabling a wide range of use cases:

  1. Database Management: Docker volumes are commonly used to persist database data across container restarts and updates, ensuring data integrity and consistency in database-driven applications. This allows for efficient database management without the risk of losing critical data.

  2. Configuration Sharing: Docker volumes facilitate the sharing of configuration files and other resources among containers within a distributed application. This ensures consistent configurations across multiple instances, simplifying management and deployment processes.

  3. Logging and Monitoring: Docker volumes enable containers to write log files and store monitoring data externally, making it easier to analyze and manage application performance. Centralized logging and monitoring solutions can efficiently collect and process data from multiple containers using shared volumes.

  4. Content Management: Docker volumes are ideal for managing content in content management systems (CMS) and web applications. By storing media files, user uploads, and other content externally, containers remain lightweight and scalable while ensuring data persistence and availability.

  5. Horizontal Scaling: As mentioned before Docker volumes support horizontal scaling by allowing multiple container instances to access the same data simultaneously. This enables seamless load balancing and failover scenarios, where containers can be added or removed dynamically without disrupting data access or integrity.

  6. Development and Testing Environments: Docker volumes facilitate the creation of consistent development and testing environments by sharing code repositories, dependencies, and other resources across containers. Developers can work in isolated environments while maintaining access to the same data and configurations.

  7. Backup and Disaster Recovery: Docker volumes provide a reliable mechanism for backing up data and implementing disaster recovery strategies. By storing critical data externally, organizations can create regular backups and replicate data across multiple locations for redundancy and fault tolerance.

  8. Cross-Platform Compatibility: Docker volumes offer cross-platform compatibility, allowing containers to access data stored on different operating systems and storage solutions. This flexibility enables seamless migration of applications between environments and ensures interoperability across diverse infrastructure setups.

Setup

Setting up a Docker volume is a straightforward process. Docker volumes are used to persist data generated by and used by Docker containers. Here's a basic guide on how to set up a Docker volume:

  1. Create a Volume: You can create a Docker volume using the docker volume create command. For example:

    docker volume create docker-volume-setup-example
    

    This will create a volume named docker-volume-setup-example.

  2. Mount the Volume to a Container: You can mount a volume to a container when you run it using the -v or --volume flag followed by the volume name and the mount point within the container. For example:

    docker run -d --name my_container -v docker-volume-setup-example:/data portainer
    

    In this example, the volume docker-volume-setup-example is mounted to the /data directory within the container.

  3. Verify the Volume: You can list all volumes using the docker volume ls command:

    docker volume ls
    

    This will show you a list of all Docker volumes, including the one you just created.

  4. Inspect Volume Information: You can inspect the details of a specific volume using the docker volume inspect command. For example:

    docker volume inspect docker-volume-setup-example
    

    This will give you detailed information about the volume, including its mount point on the host system.

  5. Use the Volume: Once the volume is mounted to a container, any data written to the specified mount point within the container will be stored in the volume. This data will persist even after the container is stopped or removed.

  6. Remove the Volume: If you no longer need the volume, you can remove it using the docker volume rm command. For example:

    docker volume rm docker-volume-setup-example
    

    This will delete the volume from your system.

Basic Setup: Persistent data with docker volumes

Giving a docker container persistent data with docker volumes is best done over another docker related file. Here we will use docker-compose.yaml You can read more to docker-compose here in my Wiki 📄Docker: Docker-Compose

Step 1: Create a Dockerfile for Apache Image

Create a Dockerfile with the following content in your project directory:

FROM httpd:latest

This will use the latest version of Apache HTTP Server available on Docker Hub.

Step 2: Create a Docker Compose File

Create a docker-compose.yml file in the same directory with the following content:

version: '3'

services:
  apache:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - docker-volume-setup-example:/usr/local/apache2/htdocs
    ports:
      - "80:80"

volumes:
  docker-volume-setup-example:
    driver: local

In this Compose file:

  • We define a service named apache using the image built from the Dockerfile we created.
  • We mount the volume docker-volume-setup-example to the Apache container at /usr/local/apache2/htdocs. This is where Apache typically serves files from.
  • Port 80 of the container is exposed to port 80 on the host.
  • We define a named volume docker-volume-setup-example using the local driver.

Step 3: Build and Run the Environment

Open your terminal and navigate to the directory where your docker-compose.yml and Dockerfile are located.

Run the following command to build and run the Docker environment:

docker-compose up -d

This command will build the Apache image using the Dockerfile and start the container based on the configuration in the docker-compose.yml file. The -d flag runs the containers in detached mode, meaning they will run in the background.

Step 4: Test the Setup

You can now access your Apache server in your browser by navigating to http://localhost. Any files you place in the docker-volume-setup-example volume will be served by Apache.

Step 5: Stopping and Removing the Environment

When you're done testing, you can stop and remove the Docker environment using the following command:

docker-compose down

This will stop and remove the containers defined in the docker-compose.yml file, but it will preserve the named volume docker-volume-setup-example, keeping your data intact for future use.