🚀 My Docker & DevOps Learning Journey ! 🚀

Mohd SalmanMohd Salman
3 min read

Day 13: Wrapping Up Docker Basics – Diving Into Volumes & Networks in Docker Compose 🚀

Today was the final day of my Docker journey, and I focused on adding volumes and networks in Docker Compose files. These are essential components for managing data persistence and container communication. Here’s a summary of my learning as I prepare to wrap up Docker and move toward open-source projects.

🧠 What I Learned Today

🧱 Docker Volumes

Volumes in Docker help retain data even if the container is deleted or recreated. In Docker Compose, we define volumes under a dedicated volumes section in the YAML file, which we can then attach to any container. This keeps data like databases or logs persistent across restarts.

Defining a Volume in Docker Compose YAML File In a YAML file, you can define volumes like this:

volumes:
  mongodb-data:

Using the Volume in a Service To attach this volume to a MongoDB container and use it for storing data:

services:
  mongodb:
    image: mongo:latest
    volumes:
      - mongodb-data:/data/db

Here, the volume mongodb-data is connected to /data/db inside the MongoDB container, ensuring that MongoDB data will persist even if the container is removed.

🌐 Docker Networks

Docker Compose automatically connects containers in a shared network, so they can easily communicate using each other’s service names, like mongodb or mysql. For example, our custom_app service can connect to MongoDB or MySQL just by referencing them by name.

For simple cases, we don’t need to create a custom network as Docker Compose sets up a default network where containers can reach each other by name.


📝 Full Example of Docker Compose YAML with Volumes and Network

Here’s a complete YAML example incorporating services with defined volumes and using the default network:

version: "3.9"

services:
  mongodb:
    image: mongo:latest
    ports:
      - "27017:27017"
    volumes:
      - mongodb-data:/data/db

  mysql:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: root_password

  custom_app:
    build: ./Dockerfile
    ports:
      - "3000:3000"

volumes:
  mongodb-data:

📑 Explanation of Each Line:

  • version: "3.9": Specifies the version of Docker Compose. Version 3.9 is widely used and supports modern Compose features.

  • services: Defines all containers (services) used in this Compose setup.

  • mongodb: The MongoDB service using the mongo:latest image, exposing port 27017 and mounting the mongodb-data volume to /data/db.

  • mysql: The MySQL service with the latest MySQL image, using an environment variable to set the root password.

  • custom_app: Our custom application built from a Dockerfile and exposing port 3000.

  • volumes: Declares persistent volumes. Here, mongodb-data is defined for MongoDB’s data.


🔥 Takeaways

With Docker Compose, we’ve brought our containers together in a way that’s efficient and easy to manage. Adding volumes keeps data safe across container restarts, and Docker’s automatic networking enables smooth communication between services without extra configuration. I’m excited to move forward with open-source projects, and later, explore orchestration tools like Kubernetes to handle containerized applications at scale.


What’s Next?

That concludes my Docker journey for now. I’ll be shifting my focus to open-source contributions to deepen my understanding of real-world applications. Later, I plan to dive into orchestration tools like Kubernetes to expand my DevOps skills further.


🔗 Follow My Docker & DevOps Journey
To see my full journey and keep up with my next steps, check out the hashtag: #SalmanDockerDevOpsJourney.

0
Subscribe to my newsletter

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

Written by

Mohd Salman
Mohd Salman