Till now you have learned how to create a docker-compose.yml file and push it to the Repository. Let's move forward and dig more into other Docker-compose.yml concepts. Aaj thodi padhai krte hai on Docker Volume & Docker Network ๐
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.
We can receive the data in case the container is destroyed.
Steps to create a volume -
Create a directory for volume.
Create a volume using the command -
docker volume create --name <volume name> --opt type=none --opt device=<volume path> --opt o=bind
Inspect the volume to see the configuration of volume -
docker volume inspect django-notes-volume
Build and run the container -
docker build . -t django-notes
docker run -d -p 8000:8000 --mount source=django-notes-volume,target=/app/backend django-notes:latest
Now, when you execute a container in bash mode, you gain access to its internal file system, allowing you to inspect all the data inside the container. Additionally, any data stored within a volume folder that's mounted into the container is visible outside the container as well. This two-way connection ensures that any changes made within the container are seamlessly synchronized with the data in the volume folder, maintaining data consistency between the container and the host system. Same, we can see in below screenshot -
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 that will bring UP and bring DOWN containers in a single shot ( Example - Create application and database container )
Created below docker-compose file for notes app, nginx, and database. I have already cloned an application code to the same directory and created its Dockerfile -
Dockerfile.yml -
docker-compose.yml -
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 addreplicas
in the deployment file for auto-scaling. -The Command to scale -
docker-compose up -d --scale nginx-notes=3
-
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. - We have already included the volumes as above.
Create two or more containers that read and write data to the same volume using the
docker run --mount
command. - Let's create one more container using the same volume.Verify that the data is the same in all containers by using the docker exec command to run commands inside each container. - Below is the example, where I have created a file in Container1 and same is updated in Container2 and the local volume folder.
Use the docker volume ls command to list all volumes and the docker volume rm command to remove the volume when you're done. - Make sure to stop and remove the container to remove the volume.
Thank you :)