Let's explore the capabilities of Docker, a robust containerization platform that amplifies the adaptability and scalability of containerized applications through features like volumes and networks.
This piece will delve into the essence of Docker volumes and networks, unraveling their pivotal role in orchestrating data persistence and fostering seamless communication among containers
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 the same data.
A crucial feature for scenarios where preserving data integrity is paramount, such as in databases.
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). 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
Let's understand concepts with the help of the task.
Task-1
- Create a multi-container docker-compose file that will bring UP and bring DOWN containers in a single shot ( Example - Create application and database container )
-solution
let's create volume with docker volume create my_valume.
Now we write docker-compose file (YAML) which runs 2 containers
a. Web Application-Django
b. DB- mysql
c. Volume =my_volume
To start this multi-container application, use: docker-compose up -d
Containers can access this volume, ensuring that the data stored within volume and preserved to run it we execute the below command.
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, anddocker-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.
Solution:
Creating namespace "docker volume create ubuntu_volume"
Create two or more containers that read and write data to the same volume using the
docker run --mount
command.We have run container HTTPD and NIGINX with same volume i.e new_volume.
Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.
- Will connect to one container and create file there and access the same file from another container as we attached both containers with new_volume.
Use the docker volume ls command to list all volumes and docker volume rm command to remove the volume when you're done.
I believe this task clarifies the concept of volumes—how to attach separate volumes to containers, ensuring data/database safety in unpredictable scenarios by retrieving it from distinct volumes. Additionally, we've explored configuring multiple containers to utilize a single volume. Our upcoming chapter will delve into the communication between two containers within Docker.
Thanks,
Kishor Chavan