Day 19: Docker for DevOps Engineers ๐

"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 upcommand with the-dflag to start a multi-container application in detached mode.Use the
docker-compose scalecommand to increase or decrease the number of replicas for a specific service. You can also addreplicasin the deployment file for auto-scaling.Use the
docker-compose pscommand to view the status of all containers, anddocker-compose logsto view the logs of a specific service.Use the
docker-compose downcommand 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 --mountcommand.Verify that the data is the same in all containers by using the
docker execcommand to run commands inside each container.Use the
docker volume lscommand to list all volumes and thedocker volume rmcommand to remove the volume when you're done.
Answer to Task 1
To create a multi-container docker-compose file, follow these steps:
- Create a
docker-compose.ymlfile 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:
- Use the following commands to manage your multi-container application:
Start the application in detached mode:
docker-compose up -dScale the application service:
docker-compose scale app=3View the status of all containers:
docker-compose psView the logs of a specific service:
docker-compose logs appStop 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:
- 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
- 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
- List all volumes:
docker volume ls
- Remove the volume when done:
docker volume rm shared-data
Thank you for reading!
ยฉ 2024 Anand Raval. All rights reserved.





