Understanding of Docker Volume,Docker Networks and Docker Compose | #Day19 #90daysofdevops

ยท

8 min read

Understanding of Docker Volume,Docker Networks and Docker Compose | #Day19 #90daysofdevops

๐Ÿณ Understanding Docker Volumes and Container Mounting ๐Ÿ“ฆ**

Docker has revolutionized the way we develop, ship, and deploy applications. One of its powerful features is Docker Volumes, which allow us to persist data beyond the lifecycle of a container. In this blog, we'll delve into what Docker Volumes are, how to create them, and how to mount containers to these volumes.

What is a Docker Volume?

Docker Volume is a way to manage and persist data outside the container's lifecycle. It's like a bridge between the host system and the container, ensuring that data remains even if the container is stopped, removed, or replaced.

Steps to Create a Docker Volume

  1. Create a folder using the following command:

    mkdir <name_of_directory>

  2. Create a new directory in this directory using following command:

    mkdir <name_of_new_directory>

  3. Creating a Volume: To create a Docker volume, you can use the following command:

     #this is the command for createing volume 
     docker volume create
     #the full command to create the the volume 
     docker volume create --name=django-volume --opt type=none --opt device=/home/ubuntu/docker_projects/volumes/django-app-volume --opt o=bind
     #explaination
     docker volume create --name=<name_of_volume> --opt type=<type_of_volume> --opt device<path_of_volume_directory_created_earlier> --opt o=bind
    

    This creates a volume named django-volume.

  4. Listing Volumes: To see a list of available volumes, use:

     docker volume ls
    

Steps to Mount a Container in a Docker Volume

  1. Creating a Container with Volume Mount: To create a container and mount a volume to it, use:

     docker run -d --mount source=django-volume,target=/data -p 8000:800 django-app:latest
    

    Here, django-volume is the previously created volume, /data is the path within the container where the volume is mounted, and django-app is the Docker image.

  2. Inspecting Container's Volumes: To inspect a container and see its volume configuration, run:

     docker inspect my_container
    
  3. Accessing Volume Data: Data stored in the mounted volume is now accessible from both the host system and the container.

Benefits of Using Docker Volumes:

  • Data Persistence: Volumes allow data to survive container shutdowns and removals.

  • Backup and Restore: Volumes can be backed up and restored easily.

  • Database Storage: Ideal for databases where data integrity is crucial.

๐ŸŒ Understanding Docker Networking and Its Importance in DevOps ๐Ÿ–ง

Docker Networking plays a crucial role in enabling communication between containers and external networks. It allows containers to interact with each other, the host, and other services. Let's explore Docker Networking, its commands, and its significance in DevOps.

What is Docker Networking?

Docker Networking is a way to establish communication between containers and between containers and external networks. It enables isolated, secure, and efficient data exchange.

Docker Networking Commands

  1. Listing Networks: To see a list of Docker networks, use:

     docker network ls
    
  2. Creating a Network: To create a Docker network, use:

     docker network create my_network
    
  3. Connecting Containers to a Network: When running a container, connect it to a network using:

     docker run -d --name my_container --network my_network my_image
    

Why Use Docker Networking in DevOps?

  • Microservices Architecture: In a microservices architecture, containers communicate over networks, enabling individual services to scale independently.

  • Isolation and Security: Networking ensures containers are isolated, reducing the attack surface and enhancing security.

  • Service Discovery: Networking facilitates automatic service discovery, allowing containers to find and connect to each other seamlessly.

Commonly Used Network in DevOps: Bridge Network

The Bridge Network is the default network created when Docker is installed. It's suitable for single host applications. Containers connected to the bridge network can communicate with each other using internal IP addresses.

๐Ÿš€ Building a Multi-Container Docker Compose Setup for a 2-Tier Flask Application ๐Ÿณ**

As a DevOps engineer, managing multi-container applications efficiently is crucial. Docker Compose simplifies this task by allowing you to define and manage multi-container applications using a single configuration file. In this blog, we'll walk through creating a Docker Compose file for a 2-tier Flask application, including steps to bring the containers up and down seamlessly.

Prerequisites

Before we begin, make sure you have Docker and Docker Compose installed on your system.

1. Setting Up the Project Structure

Start by creating a directory for your project. Inside this directory, create the following structure:

perlCopy codemy-flask-app/
โ”‚
โ”œโ”€โ”€ app/
โ”‚   โ””โ”€โ”€ app.py
โ”‚
โ”œโ”€โ”€ db/
โ”‚   โ””โ”€โ”€ ...
โ”‚
โ””โ”€โ”€ docker-compose.yml

Here, the app directory will hold your Flask application code, and the db directory will hold your database-related files (e.g., data storage).

2. Creating the Flask Application

In the app directory, create a simple Flask application. For this example, let's assume the app.py file contains a basic Flask app that interacts with the database.

3. Write a Docker file for this application

4. Writing the Docker Compose Configuration

Create a docker-compose.yml file in the project root directory. This file will define the services, networks, and volumes for your multi-container setup.

version: "3"
services:
  flask-app:
    build: ./app
    ports:
      - "5000:5000"
    depends_on:
      - database

  database:
    image: mysql:5.7
    environment:
      MYSQL_DB: mydatabase
      MYSQL_USER: devops
      MYSQL_ROOT_PASSWORD: test@123
      MYSQL_PASSWORD: devops
     volume: db-data:/var/lib/postgresql/data

networks:
  default:
    driver: bridge

In this example:

  • We define two services: flask-app and database.

  • The flask-app service is built from the ./app directory and exposes port 5000.

  • The database service uses the PostgreSQL image and sets environment variables for database configuration.

  • The depends_on attribute ensures that the flask-app service starts only after the database service is up.

  • We use the default bridge network.

4. Bringing Up the Containers

To bring up the containers defined in the Docker Compose file, open a terminal and navigate to your project directory. Run the following command:

docker-compose up -d

The -d flag runs the containers in the background.

5. Bringing Down the Containers

To stop and remove the containers, use the following command:

docker-compose down

This command will stop and remove the containers, networks, and volumes defined in the Docker Compose file.

Scaling Services with Docker Compose

In certain scenarios, you might need to scale up or down the number of replicas for a specific service in your multi-container application. Docker Compose offers the scale command for this purpose. Consider a scenario where you have a service named web and you want to scale it to run three replicas:

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

This command instructs Docker Compose to create and manage three instances of the web service. Scaling services can help you achieve better performance, load balancing, and high availability for your application.

Monitoring Container Status and Logs

Keeping track of the status and logs of your containers is crucial for understanding how your application is performing. Docker Compose provides commands to assist in this area:

  • To view the status of all containers defined in your docker-compose.yml file, you can use:

      docker-compose ps
    
  • To check the logs of a specific service, you can use:

      docker-compose logs <service-name>
    

These commands offer insights into the health and behavior of your containers. You can use the information to troubleshoot issues, monitor application performance, and ensure everything is running as expected.

Understanding Docker Volumes and Named Volumes

Docker Volumes are mechanisms that allow containers to store data outside of their writable layers. They offer a way to persist data even when containers are stopped or removed. Named Volumes are a specific type of Docker Volume that is given a user-friendly name, making it easier to manage and reference. Docker Volumes can be created and managed using the Docker CLI or Docker Compose.

Creating and Sharing Data with Docker Volumes

Let's walk through the process of creating and sharing data using Docker Volumes:

  1. Creating a Volume: You can create a volume using the docker volume create command. For example:

     luaCopy codedocker volume create mydata
    
  2. Running Containers with Volumes: To share the created volume between containers, use the --mount flag while running containers. This can be done using the docker run command. For instance:

     bashCopy codedocker run -d --name container1 --mount source=mydata,target=/app/data alpine
     docker run -d --name container2 --mount source=mydata,target=/app/data alpine
    
  3. Verifying Data Sharing: With both containers running, you can verify data sharing by running commands inside each container using docker exec:

     bashCopy codedocker exec container1 ls /app/data
     docker exec container2 ls /app/data
    

    The output should be the same, confirming that the data is shared.

Benefits of Named Volumes

Named Volumes provide additional convenience and organization:

  1. Human-Friendly Naming: Named Volumes have more descriptive names, making it easier to identify their purpose.

  2. Automatic Volume Management: Docker handles the creation and management of Named Volumes, eliminating the need to manually remove unused volumes.

Step-by-Step Implementation

Step 1: Create a Named Volume

bashCopy codedocker volume create mydata

Step 2: Run Containers with the Named Volume

bashCopy codedocker run -d --name container1 --mount source=mydata,target=/app/data alpine
docker run -d --name container2 --mount source=mydata,target=/app/data alpine

Step 3: Verify Data Sharing

bashCopy codedocker exec container1 sh -c "echo 'Hello from container1' > /app/data/file.txt"
docker exec container2 cat /app/data/file.txt

Step 4: List and Remove the Named Volume

bashCopy codedocker volume ls
docker volume rm mydata

Conclusion

In this blog, we have seen how to use Docker Volume Docker networks. We have also seen how to create a multi-container docker-compose file that will bring up and bring down containers in a single shot. And how to create and mount volumes on containers, verify that the data is shared between containers and list and remove volumes using Docker commands.

ย