Day 19 Guide to Docker Volume and Network Setup

ยท

5 min read

Hey DevOps enthusiasts! ๐Ÿ‘‹ Ever wondered how Docker manages data or how containers communicate with each other? If yes, you're in the right place! Today, weโ€™re diving into Docker Volumes and Docker Networks in the most practical way possible. Plus, weโ€™ve got two fun hands-on tasks to help you get your hands dirty with Docker! ๐Ÿ› ๏ธ


What is a Docker Volume?๐Ÿค”

Imagine youโ€™re running a cafรฉ โ˜• where customers place orders, but every time they leave, you lose track of those orders. Thatโ€™s how a container behaves with its dataโ€”once it stops, the data inside it is gone. ๐Ÿ˜ฑ

Docker Volume is like a notepad ๐Ÿ“’ where you can write those orders. Even if the cafรฉ closes (container stops), the notepad (volume) remains, preserving the data for future use. ๐ŸŽ‰

Why use it? Because data in containers is temporary โ€” once the container stops, the data disappears. Volumes help you store data permanently! ๐Ÿ“‚

In simple terms:

Docker Volume is a storage mechanism used to save data that persists even when the container is stopped or removed.

Why Use Docker Volumes?

  • Data persistence: Keep your data even after the container is gone.

  • Data sharing: Share data between multiple containers.

  • Data backups: Easily back up important files.


What is a Docker Network?๐ŸŒ

Now, imagine your cafรฉ has two sectionsโ€”one for customers (frontend) and one for chefs (backend). They need to talk to each other! Docker Network works as the intercom ๐Ÿ“ž that lets them communicate.

In technical terms:

Docker Network allows containers to communicate with each other and with the outside world.

Why Use Docker Networks?

  • Isolate services: Keep your containers secure and isolated.

  • Simplify communication: Allow containers to talk without exposing them to the internet.

  • Custom networks: Set up specific networks for different environments.


๐Ÿ“‹ Prerequisites ๐Ÿ“‹

Make sure you have:

Before diving into the tasks, ensure you have:

  1. Docker and Docker Compose installed on your system.

  2. Basic Understanding of Docker commands and YAML files.

  3. A code editor like Vim, Nano, or VS Code to edit your docker-compose.yml.


Letโ€™s Get Hands-On! ๐Ÿ’ช

๐Ÿ—๏ธ Task 1: Create a Multi-Container Docker-Compose Setup

Goal:

We'll set up a web application and a database in separate containers, connected via a network, with persistent data using volumes. ๐Ÿ”—

Letโ€™s build a simple multi-container application with Docker Compose. Weโ€™ll spin up two containers:

  1. A web application (Frontend).

  2. A database (Backend).

๐Ÿ“ Step 1: Create the docker-compose.yml File

This file will define both your containers (application and database). Here's a simple example:

version: '3.9'

services:
  app:
    image: nginx:latest
    ports:
      - "8080:80"
    networks:
      - app-network
    depends_on:
      - db
    volumes:
      - app-data:/usr/share/nginx/html

  db:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: appdb
    networks:
      - app-network
    volumes:
      - db-data:/var/lib/mysql

networks:
  app-network:

volumes:
  app-data:
  db-data:

๐Ÿƒ Step 2: Start the Containers

Use the following command to start the containers in detached mode:

docker-compose up -d

This command:

  • Spins up the web app and database containers.

  • Runs them in the background (detached mode).

  • Create a network for communication.

  • Attach volumes to store data persistently.

๐Ÿ“ˆStep 3: Scale Your Application:

Want more replicas of your app? Easy! Run:

docker-compose up -d --scale app=3

๐Ÿ” Step 4: Check the Status

Check the running containers:

docker-compose ps

Use this to verify if the containers are running.

๐Ÿ“„ Step 5: View Logs

View the logs for a specific service:

docker-compose logs app

Check the logs of the app service to see if everythingโ€™s working smoothly.

๐Ÿ›‘ Step 6: Bring Down the Containers

When youโ€™re done, stop and clean up all resources:

docker-compose down

This stops and removes the containers, networks, and volumes.


๐Ÿ—๏ธ Task 2: Share Data Between Containers Using Docker Volumes

Goal:

Create two containers that share data using a named volume.

Time to see the magic of data sharing in action! ๐Ÿ—‚๏ธ

Letโ€™s create two containers that share data through a Docker Volume.

๐Ÿ—‚๏ธStep 1: Create a Named Volume

Create a volume named shared_data:

docker volume create shared_data

๐Ÿ“ Step 2: Create and Start Containers with a Shared Volume

Letโ€™s create two containers that share the same data.

docker run -d --name container1 --mount source=shared-data,target=/data nginx
docker run -d --name container2 --mount source=shared-data,target=/data httpd

Here, we:

  • Created a volume named shared-data.

  • Mounted it to the /data directory in both containers.

๐Ÿ” Step 3: Verify Data Sharing

  1. Create a file inside container1:

    Add some data inside container1:

     docker exec container1 bash -c "echo 'Hello from Container 1' > /data/hello.txt"
    
  2. Check the file in container2:

    Check if container2 can see the same data

     docker exec container2 cat /data/hello.txt
    

You should see Hello from Container 1, proving that both containers are sharing the same volume! ๐ŸŽ‰

๐Ÿ“œ Step 4: List and Remove Volumes

List all volumes:

docker volume ls

Remove the containers and shared volume:

docker rm -f container1 container2
docker volume rm shared-data

โœจKey Takeaways๐Ÿ“

  • Docker Volumes help you manage data that persists even when containers are removed.

  • Docker Networks enable communication between containers and isolate them for security.

  • Docker Compose, managing multi-container applications is a breezeโ€”just one file and a few commands to spin up, scale, and shut down your app.


๐Ÿ’ก Pro Tip: Start small and build up! Experiment with these commands and configurations, and soon you'll be deploying scalable apps like a pro. ๐Ÿง‘โ€๐Ÿ’ป๐Ÿ’ฅ


Happy Docking! ๐Ÿณ If you found this helpful, share your thoughts or questions below. Letโ€™s grow together in our DevOps journey! ๐Ÿš€

ย