Day 19: Docker Project for DevOps Engineers (Part-3)

DevOps Learning

Let’s dive into Docker volumes and Docker networking. These are fundamental concepts in the world of containerization, and understanding them is crucial for anyone working with Docker.

Docker Volumes

What Are Docker Volumes?

Docker volumes are a way to persist data generated by and used by Docker containers. Unlike bind mounts (which depend on the host machine’s directory structure), volumes are entirely managed by Docker. Here are some key points about volumes:

  1. Data Persistence: Volumes allow you to store data outside the container’s writable layer. This means your data survives even if the container is removed or replaced.

  2. Advantages Over Bind Mounts:

    • Easier to back up or migrate.

    • Managed using Docker CLI commands or the Docker API.

    • Work on both Linux and Windows containers.

    • Can be safely shared among multiple containers.

    • Volume drivers provide additional functionality (e.g., remote hosts, encryption).

  3. Types of Volumes:

    • Named Volumes: Explicitly named and managed by Docker. Useful for sharing data between containers.

    • Anonymous Volumes: Created automatically when a container needs storage but doesn’t specify a volume name.

Example:

Suppose you’re building a simple web application that consists of an Nginx web server and a MySQL database. You want to ensure that the data generated by these services persists even if the containers are restarted or replaced. Let’s set up named volumes for this scenario.

  1. Create a Docker Compose File (docker-compose.yml):

     version: '3.8'
     services:
       web:
         image: nginx
         volumes:
           - web_data:/usr/share/nginx/html:ro
         ports:
           - "80:80"
       db:
         image: mysql
         environment:
           MYSQL_ROOT_PASSWORD: pass
         volumes:
           - db_data:/var/lib/mysql
     volumes:
       web_data:
       db_data:
    

    In this configuration:

    • We define two services: web (Nginx) and db (MySQL).

    • Each service has its own named volume: web_data for the Nginx web server and db_data for the MySQL database.

    • The web service mounts the web_data volume to the Nginx HTML directory in read-only mode.

    • The db service mounts the db_data volume to the MySQL data directory.

  2. Running Docker Compose:

    Execute the following command in the same directory where your docker-compose.yml file resides:

     docker-compose up
    

    Docker Compose will create the named volumes (web_data and db_data) if they don’t already exist. Now your Nginx web server and MySQL database containers share these volumes, ensuring data persistence.

  3. Accessing the Web Application:

    Open your web browser and navigate to http://localhost. You should see the default Nginx welcome page served from the web_data volume.

    • If you still cannot see the Nginx welcome page, you might need to make some changes in the etc/nginx/sites-enabled/default file.

         root /usr/share/nginx/html/;
      
         location / {   
                        # host.docker.internal or IP address of local machine      
                        proxy_pass http://host.docker.internal:3000;
                }
      
  4. Database Persistence:

    Any data written by the MySQL container (e.g., databases, tables) will be stored in the db_data volume. Even if you stop and restart the containers, your database data remains intact.

Docker Networking

What Is Docker Networking?

Docker networking enables communication between containers, the Docker host, and the outside world. Containers have networking enabled by default, but they don’t know whether their peers are also Docker workloads or not. Here’s what you need to know:

  1. User-Defined Networks:

    • Create custom networks to connect multiple containers.

    • Containers in the same network can communicate using IP addresses or container names.

    • Example:

        docker network create my_bridge
        docker run --network=my_bridge -itd --name=container3 busybox
      
  2. Network Drivers:

    • Bridge (default): Isolated network for containers.

    • Host: No isolation between container and host.

    • None: Complete isolation.

    • Overlay: Connects Docker daemons across hosts.

    • Macvlan: Assigns MAC addresses to containers.

  3. Published Ports:

    • By default, containers on bridge networks don’t expose ports externally.

    • Use -p or --publish to make a port available outside the bridge network.

Suppose you’re running a microservices architecture. Each service is a separate container. You create a custom user-defined network (my-microservices-net) and connect all services to it. Now they can communicate seamlessly using container names or IP addresses.

Remember, Docker networking is like setting up a virtual LAN for your containers!

Thank you for reading😉.