Docker Week: Day 3 – Hands-On Guide to Docker Volumes: Managing Data in Containers

Docker Week: Day 3 – Hands-On Guide to Docker Volumes: Managing Data in Containers

Saturday, 21st September 2024


What Are Docker Volumes?

When working with containers, data management is crucial, especially since data inside a container can disappear as soon as the container stops or is removed. Docker Volumes help by providing a way to persist and share data across containers and sessions. In this hands-on guide, we'll focus on practical steps and examples for using Docker volumes in real-world scenarios.


Why Docker Volumes Matter

Volumes are critical for DevOps, Cloud Engineers, and anyone working with containerized applications. They help:

  • Persist Data: Keep important data even after the container is deleted.

  • Share Data Between Containers: Multiple containers can access the same volume, making data sharing easy.

  • Simplify Backups: Volumes can be backed up independently of containers.


Getting Started with Docker Volumes

Now, let’s jump into practical steps to create and manage volumes with hands-on examples. These examples assume you have Docker installed.


1. Creating and Using a Docker Volume

Start by creating a Docker volume and using it inside a container.

  1. Create a Volume: Run the following command to create a new volume:

     docker volume create my_data_volume
    

    Docker will manage this volume independently of containers.

  2. Run a Container with the Volume Attached: Now, let's run an NGINX container and attach the newly created volume:

     docker run -d --name my_nginx -v my_data_volume:/usr/share/nginx/html nginx
    
    • -d runs the container in detached mode.

    • -v attaches the my_data_volume to /usr/share/nginx/html inside the container.

With this command, any data written to /usr/share/nginx/html inside the container will be stored in the volume and will persist.

  1. Test the Volume: Now, let’s add a simple HTML file to the volume and see if it shows up in the container. On your host machine, run:

     echo "<h1>Hello from Docker Volumes!</h1>" > /var/lib/docker/volumes/my_data_volume/_data/index.html
    

    Access the NGINX container via your browser (localhost:80), and you should see the message: "Hello from Docker Volumes!" This shows that data stored in the volume is accessible by the container.


2. Sharing Data Between Containers with Volumes

Now let’s see how two containers can share the same volume. This is useful for applications that need access to shared data, such as a database and an application container.

  1. Create Two Containers Sharing the Same Volume: First, stop the previous NGINX container:

     docker stop my_nginx
    

    Now, create two containers and share the my_data_volume between them:

     docker run -d --name web1 -v my_data_volume:/app_data nginx
     docker run -d --name web2 -v my_data_volume:/app_data nginx
    

    Both containers now have access to the /app_data directory.

  2. Add Data from One Container: Add a new file inside the first container:

     docker exec web1 bash -c "echo 'Data from web1' > /app_data/web1.txt"
    
  3. Access the Data from the Second Container: Check if the second container can see the data added by the first container:

     docker exec web2 cat /app_data/web1.txt
    

    The output should be: Data from web1. This demonstrates how data can be shared between containers using Docker volumes.


3. Managing Volumes with Docker Compose

In real-world scenarios, especially with multi-container applications, using Docker Compose is a great way to manage containers and volumes together.

  1. Create a Docker Compose File: Create a docker-compose.yml file that defines a web service and a volume:

     version: "3.9"
     services:
       web:
         image: nginx
         volumes:
           - web_data:/usr/share/nginx/html
         ports:
           - "8080:80"
     volumes:
       web_data:
    
    • The service web uses the nginx image and maps the web_data volume to the NGINX web directory.

    • The volume web_data is defined in the volumes section.

  2. Run the Containers: Run the Docker Compose file:

     docker-compose up -d
    

    Docker will automatically create the volume and attach it to the web container. You can verify this by checking the list of volumes:

     docker volume ls
    
  3. Access the Container: Open a terminal inside the running container and add data to the volume:

     docker exec -it web bash
     echo "Hello from Docker Compose!" > /usr/share/nginx/html/index.html
    

    Open your browser and visit http://localhost:8080. You should see the message "Hello from Docker Compose!"


4. Removing Docker Volumes

When you no longer need a volume, you can remove it to free up space. Here's how:

  1. Stop and Remove Containers: Before removing a volume, ensure that no containers are using it:

     docker stop web1 web2
     docker rm web1 web2
    
  2. Remove the Volume: Once the containers are removed, you can safely delete the volume:

     docker volume rm my_data_volume
    

    You can confirm the volume is deleted by running:

     docker volume ls
    

Common Use Cases for Docker Volumes

Here are some real-world examples where Docker volumes are frequently used:

  • Database Containers: Use volumes to store database data like MySQL or PostgreSQL so that it persists across container restarts.

  • Log Storage: Store logs outside of the container so you can analyze or back them up even if the container is destroyed.

  • Configuration Files: Share configuration files between multiple containers that need the same settings.


Why Docker Volumes Are Essential for Cloud and DevOps Engineers

As a cloud or DevOps engineer, managing data is critical in production environments where containers are often stopped, restarted, or scaled across clusters. Docker volumes ensure that crucial data (e.g., database information, logs, configurations) is not lost during these operations, providing stability and continuity in your applications.

By integrating Docker volumes into CI/CD pipelines or orchestrating multiple services, engineers can confidently manage data across a complex infrastructure.


Conclusion

Docker volumes are an essential tool for managing persistent data in containerized applications. This hands-on guide covered practical ways to create, manage, and use volumes in real-world scenarios. Whether you’re sharing data between containers or ensuring that data remains available across restarts, volumes offer a powerful, flexible solution.

In the next post, we'll dive deeper into Docker Networking and explore how containers communicate with each other. Stay tuned for Day 4 of Docker Week!