Skip to main content

Command Palette

Search for a command to run...

Day 19: Docker for DevOps Engineers ๐Ÿš€

Updated
โ€ข3 min readโ€ขView as Markdown
Day 19: Docker for DevOps Engineers ๐Ÿš€
A

"I'm a 3rd-year Computer Engineering student at Marwadi University with skills in C++, web development (MERN stack), and DevOps tools like Kubernetes. I contribute to open-source projects and share tech knowledge on GitHub and LinkedIn. I'm learning cloud technologies and app deployment. As an Internshala Student Partner, I help others find jobs and courses." now currently focusing on #90DaysOfDevops

So far, you've learned how to create a docker-compose.yml file and push it to the repository. Let's move forward and explore more Docker Compose concepts. Today, let's study Docker Volume and Docker Network! ๐Ÿ˜ƒ

Docker Volume

Docker allows you to create volumes, which are like separate storage areas that can be accessed by containers. They enable you to store data, like a database, outside the container, so it doesn't get deleted when the container is removed. You can also mount the same volume to multiple containers, allowing them to share data. For more details, check out this reference.

Docker Network

Docker allows you to create virtual networks, where you can connect multiple containers together. This way, the containers can communicate with each other and with the host machine. Each container has its own storage space, but if we want to share storage between containers, we need to use volumes. For more details, check out this reference.

Task 1

Create a multi-container docker-compose file that will bring up and bring down containers in a single shot (e.g., create application and database containers).

Hints:

  • Use the docker-compose up command with the -d flag to start a multi-container application in detached mode.

  • Use the docker-compose scale command to increase or decrease the number of replicas for a specific service. You can also add replicas in the deployment file for auto-scaling.

  • Use the docker-compose ps command to view the status of all containers, and docker-compose logs to view the logs of a specific service.

  • Use the docker-compose down command to stop and remove all containers, networks, and volumes associated with the application.

Task 2

  • Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.

  • Create two or more containers that read and write data to the same volume using the docker run --mount command.

  • Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.

  • Use the docker volume ls command to list all volumes and the docker volume rm command to remove the volume when you're done.

Answer to Task 1

To create a multi-container docker-compose file, follow these steps:

  1. Create a docker-compose.yml file with the following content:
version: '3.8'

services:
  app:
    image: your-application-image
    ports:
      - "8080:8080"
    networks:
      - app-network
    depends_on:
      - db

  db:
    image: your-database-image
    volumes:
      - db-data:/var/lib/mysql
    networks:
      - app-network

networks:
  app-network:

volumes:
  db-data:
  1. Use the following commands to manage your multi-container application:
  • Start the application in detached mode:

      docker-compose up -d
    
  • Scale the application service:

      docker-compose scale app=3
    
  • View the status of all containers:

      docker-compose ps
    
  • View the logs of a specific service:

      docker-compose logs app
    
  • Stop and remove all containers, networks, and volumes:

      docker-compose down
    

Answer to Task 2

To use Docker Volumes and Named Volumes to share files and directories between multiple containers, follow these steps:

  1. Create and start containers with a shared volume:
docker run -d --name container1 --mount source=shared-data,target=/data your-image
docker run -d --name container2 --mount source=shared-data,target=/data your-image
  1. Verify that the data is the same in all containers:
  • Write data in container1:

      docker exec container1 sh -c 'echo "Hello from container1" > /data/hello.txt'
    
  • Read data in container2:

      docker exec container2 cat /data/hello.txt
    
  1. List all volumes:
docker volume ls
  1. Remove the volume when done:
docker volume rm shared-data

Thank you for reading!

ยฉ 2024 Anand Raval. All rights reserved.

90DaysOfDevops

Part 28 of 49

So In This Series I am following 90 DaysOfDevops challenge , what i will learn in my devops journey i will share with you with my blogs ,You will get blog on tools which used by devops engineer , example :- Linux,ansible,terraform,docker,etc

Up next

Day 18 : Mastering Docker Compose: A Comprehensive Guide for DevOps Engineers ๐Ÿš€

Till now you have created a Dockerfile and pushed it to the repository. Let's move forward and dig deeper into other Docker concepts. Today, let's study Docker Compose! ๐Ÿ˜ƒ Docker Compose Docker Compose is a tool that was developed to help define and...

More from this blog

A

Anand Raval

146 posts

I'm Anand Raval, a Cloud & DevOps Engineer with AWS Solutions Architect Associate (SAA-C03), Certified Kubernetes Administrator (CKA), and Azure Fundamentals (AZ-900) certifications. This blog covers AWS, Kubernetes, Terraform, CI/CD, cloud architecture, automation, cost optimization, troubleshooting guides, and hands-on DevOps projects.