Day-8 | Docker Advanced | Part-1

Dhruv RajvanshiDhruv Rajvanshi
3 min read

Introduction

Welcome to Day 8 of my DevOps learning journey! Today, I explored Docker volumes and networking, diving into how they work, their different types, and how to utilize them effectively. These concepts are crucial for managing data persistence and communication between containers, two foundational aspects of working with Docker.

Docker Volumes

Docker volumes are essential for managing data that needs to persist beyond the lifecycle of a container. Unlike bind mounts, volumes are completely managed by Docker, offering a more integrated and consistent approach to data storage.

Key Commands

  • Create a Volume:

      docker volume create my-volume
    
  • Inspect a Volume:

      docker volume inspect my-volume
    
  • List Volumes:

      docker volume ls
    
  • Remove a Volume:

      docker volume rm my-volume
    

Binding or Mounting a MySQL Volume

To bind or mount a MySQL volume, follow these steps:

  • Create a Volume:

      docker volume create mysql-data
    
  • Run MySQL Container:

      docker run -d --name mysql-container -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root mysql:latest
    

Docker Networking

Docker networking allows containers to communicate with each other, with the host, or with external networks. Understanding networking types and configurations is crucial for building scalable and efficient applications.

Types of Docker Networks:

  • Bridge: Default network type where containers can communicate with each other on the same host using an internal network.

  • Host: Containers share the host's networking namespace, allowing direct access to host network interfaces.

  • None: Disables networking for a container.

  • User-defined Bridge: Custom bridge network where you define communication rules, providing better control over container networking.

  • Overlay: Connects containers across different Docker hosts, useful for multi-host swarm deployments.

  • IPvlan & MACvlan: Allows containers to be directly exposed to the physical network with unique IP or MAC addresses.

Key Commands:

  • Create a User-Defined Network:

      docker network create my-network
    
  • List Networks:

      docker network ls
    
  • Connect Container to a Network:

      docker network connect my-network my-container
    

Project Implementation: Two-Tier Application

In this project, I implemented a two-tier application that includes a Flask web application and a MySQL database. Here's how I approached it:

  1. Containerizing the Flask Application:

    • I created a Dockerfile for the Flask application and exposed it to port 5000. This container serves as the frontend of the application.
  2. Setting Up the MySQL Database:

    • I ran a MySQL container with a custom Docker volume to ensure persistent data storage. The volume was bound to the MySQL data directory inside the container.
  3. Creating a Custom Network:

    • I created a user-defined bridge network to enable communication between the Flask and MySQL containers.
  4. Connecting the Containers:

    • Both containers were connected to the custom network. The Flask application was configured to interact with the MySQL database through environment variables that define the database host, user, password, and database name.
  5. Exposing the Flask Application:

    • Finally, the Flask application was exposed to port 5000 on the host, making it accessible for external traffic.

Conclusion

Today's deep dive into Docker volumes and networking has strengthened my understanding of data persistence and inter-container communication. Successfully implementing a two-tier application using these concepts was a great way to solidify this knowledge. Next up, I'll be diving deeper into Docker fundamentals to further enhance my skills.

Stay tuned for more updates as I continue my DevOps journey!

0
Subscribe to my newsletter

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

Written by

Dhruv Rajvanshi
Dhruv Rajvanshi