"Mastering Docker Volumes & Networks: Building Multi-Container Apps with Ease! 🚀🐳📦"

Saksham KambleSaksham Kamble
4 min read

Day 19: Docker for DevOps Engineers - Exploring Docker Volume and Docker Network 🚀

Hey there, DevOps enthusiasts! 🌟 Today, we dive into Docker Volumes and Docker Networks—two powerful concepts that make Docker containers even more versatile. Let's go through what these terms mean, how they’re used, and, of course, we’ll walk through some hands-on tasks. By the end of this, you’ll be set to post a project-worthy update about your Docker skills on LinkedIn. Let's get started! 💪


Docker Volume 📦

A Docker Volume is essentially a storage space that you create separately from the container. When you attach this volume to a container, it’s like giving that container a special “folder” where it can store data. Here’s why it’s useful:

  1. Data Persistence: The volume will keep the data even if the container is deleted. This is ideal for database storage, configuration files, or anything you want to keep safe.

  2. Data Sharing: You can attach the same volume to multiple containers, allowing them to share data seamlessly.

Example: Creating and Using a Volume 🛠️

# Create a volume
docker volume create my_data_volume

# Start a container and mount the volume
docker run -d --name container1 --mount source=my_data_volume,target=/app ubuntu

# Run another container and mount the same volume
docker run -d --name container2 --mount source=my_data_volume,target=/app ubuntu

In the above example:

  • We first create a volume named my_data_volume.

  • We then attach it to two separate containers (container1 and container2) so they can share any files stored in /app. This setup is useful when you want to keep certain files or databases accessible across containers.


Docker Network 🌐

Docker Network allows containers to communicate with each other on a private network. Here’s how it works:

  1. Isolation: Containers on a network can talk to each other but remain isolated from other containers not on the network.

  2. Easier Communication: When on the same network, containers can be reached by their service name, making it easy to set up complex applications where multiple containers work together.

Example: Creating and Using a Network 🛠️

# Create a network
docker network create my_network

# Run containers attached to the network
docker run -d --name app_container --network my_network nginx
docker run -d --name db_container --network my_network mysql

In this setup:

  • app_container and db_container are on the same network (my_network), allowing them to communicate. The nginx container could act as a frontend server, while mysql could be a backend database, all running together in an isolated environment.

Task 1: Multi-Container docker-compose.yml File 📄

Now, let’s create a docker-compose.yml file to start multiple containers—one for an application and another for a database. Docker Compose allows us to spin up these containers in one go.

Example docker-compose.yml for an App and Database

version: '3'
services:
  app:
    image: nginx:latest
    networks:
      - my_network
    volumes:
      - app_data:/usr/share/nginx/html
    ports:
      - "8080:80"
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: my_database
    networks:
      - my_network
    volumes:
      - db_data:/var/lib/mysql

networks:
  my_network:

volumes:
  app_data:
  db_data:

Running the Commands 🚀

  1. Start the Application: docker-compose up -d

  2. Scale a Service: docker-compose up -d --scale app=3

  3. Check Status: docker-compose ps

  4. View Logs: docker-compose logs app

  5. Stop and Remove Everything: docker-compose down

Here’s what this file does:

  • Defines two services, app (using Nginx) and db (using MySQL), each with their own configuration.

  • Sets up networking so that app and db can communicate.

  • Configures volumes to ensure data persists even if the containers are removed.


Task 2: Sharing Files with Docker Volumes 📂

Let's take it further by creating containers that read and write to the same volume.

Step-by-Step Guide for Volume Sharing 📝

# Create the volume
docker volume create shared_data

# Run the first container and mount the volume
docker run -d --name writer --mount source=shared_data,target=/data ubuntu sh -c "echo 'Hello from container 1!' > /data/message.txt"

# Run the second container and mount the same volume
docker run -d --name reader --mount source=shared_data,target=/data ubuntu tail -f /data/message.txt

In this example:

  1. writer writes a message to a file in the volume.

  2. reader reads that message from the same volume, allowing both containers to access the shared file.

Verify the Shared Data 📊

  1. Inspect the Volume:

     docker exec writer cat /data/message.txt
    
  2. Remove the Volume (when done):

     docker volume rm shared_data
    

Wrapping Up 🎉

That’s it! You’ve successfully used Docker Compose to set up a multi-container environment with networking and volumes. These skills are valuable for any DevOps role where you might need to set up or manage containerized applications.

0
Subscribe to my newsletter

Read articles from Saksham Kamble directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saksham Kamble
Saksham Kamble