Mastering Docker-Compose: Volumes and Networks Demystified Day - 19

Vishal ShekokarVishal Shekokar
2 min read

Hello Docker enthusiasts! ๐Ÿ‘‹ Today, let's delve into the fascinating world of Docker-Compose with a focus on volumes and networks. These concepts are crucial for orchestrating multi-container applications, ensuring data persistence, and enabling seamless communication between containers.

Task 1: Multi-Container Docker-Compose

What are Volumes?

Docker volumes are the secret sauce for preserving data beyond the lifecycle of a container. Imagine having a database container; you wouldn't want to lose your data every time you stop or remove the container. Volumes solve this problem by providing separate storage areas accessible by containers.

The Docker-Compose Magic

To demonstrate this, let's create a multi-container Docker-Compose setup with an application and a database service.

# docker-compose.yml

version: '3'
services:
  app:
    image: your-app-image:latest
    ports:
      - "8080:80"
    networks:
      - mynetwork
    depends_on:
      - db

  db:
    image: your-db-image:latest
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: mydatabase
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    networks:
      - mynetwork
    volumes:
      - db-data:/var/lib/mysql

networks:
  mynetwork:
    driver: bridge

volumes:
  db-data:

This Compose file defines two services - app and db. The db service uses a volume (db-data) to ensure data persistence for the MySQL database.

Managing Containers

  • To start the containers: docker-compose up -d

  • To scale the services: docker-compose scale app=3

  • To view the status of containers: docker-compose ps

  • To view logs: docker-compose logs

  • To bring down the containers: docker-compose down

Task 2: Docker Volumes and Named Volumes

Sharing is Caring: Docker Volumes

Docker volumes aren't just for persistence; they facilitate sharing data between containers. Let's create two containers that read and write data to the same volume.

# Create a named volume
docker volume create myvolume

Now, run two containers sharing this volume:

docker run -d --name container1 --mount source=myvolume,target=/data your-image:tag
docker run -d --name container2 --mount source=myvolume,target=/data your-image:tag

Verify data consistency:

# Run commands inside each container to check the data
docker exec container1 ls /data
docker exec container2 ls /data

Cleanup Time

Don't forget to list and remove volumes when done:

# List all volumes
docker volume ls

# Remove the volume
docker volume rm myvolume

And there you have it! Docker volumes and networks are powerful tools for orchestrating complex applications. Experiment with these concepts, tweak the Compose file to suit your needs, and watch your Dockerized applications.

Happy Dockering! ๐Ÿณโœจ

0
Subscribe to my newsletter

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

Written by

Vishal Shekokar
Vishal Shekokar

๐Ÿ‘‹ Hello, I'm Vishal, an aspiring Information Technology enthusiast currently embarking on a journey towards a Bachelor's degree in Engineering. My passion lies in exploring the dynamic realms of cloud computing and DevOps technologies, where I constantly strive to bridge the gap between innovation and practical implementation. ๐Ÿ’ก As a student of Information Technology, I'm on a mission to absorb knowledge, solve real-world problems, and contribute to the tech community. My academic pursuits fuel my curiosity, and my hands-on experience with cloud and DevOps tools empowers me to navigate the evolving landscape of modern technology. ๐Ÿš€ Join me as I share insights, discoveries, and challenges encountered on this exciting educational and professional adventure. Let's connect, collaborate, and grow together in the ever-expanding world of IT. ๐Ÿ”— Connect with me on social media and let's build a network that fosters learning, sharing, and innovation. Happy coding! ๐ŸŒŸ