Day 19 : Docker for DevOps Engineers


Docker-Volume
Docker allows you to create something called volumes. Volumes are like separate storage areas that can be accessed by containers. They allow you to store data, like a database, outside the container, so it doesn't get deleted when the container is deleted. You can also mount from the same volume and create more containers having same data
Docker Network
Docker allows you to create virtual spaces called networks, where you can connect multiple containers (small packages that hold all the necessary files for a specific application to run) together. This way, the containers can communicate with each other and with the host machine (the computer on which the Docker is installed). When we run a container, it has its own storage space that is only accessible by that specific container. If we want to share that storage space with other containers, we can't do that.
Task-1
- Create a multi-container docker-compose file which will bring UP and bring DOWN containers in a single shot ( Example - Create application and database container )
docker-compose file
command to bring docker up in a single command
docker ps vs docker compose ps
docker ps lists all running containers in docker engine. docker-compose ps lists containers related to images declared in docker-compose file . The result of docker-compose ps is a subset of the result of docker ps
command to bring docker down
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 docker volume rm command to remove the volume when you're done
Commands Used
Step Command
Create a volume docker volume create common_volume
Start container 1 docker run -dit --name app1 --mount source=shared_volume,target=/data busybox sh
Start container 2 docker run -dit --name app2 --mount source=shared_volume,target=/data busybox sh
Write data in app1 docker exec app1 sh -c "echo 'Hello from app1' > /data/shared.txt" Read data from app2 docker exec app2 cat /data/shared.txt
List volumes docker volume ls
Cleanup docker rm -f app1 app2 && docker volume rm common_volume
Subscribe to my newsletter
Read articles from Rahul Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
