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.
For more understanding of Docker volume read my article: Understanding Docker volume
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.
For more understanding of the docker network read my article: Understanding docker Network
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 ). Clone the repository https://github.com/Parimal-Pradhan/django-todo-cicd.git Thank you Shubham sir.
Note: 1. Docker Should be installed on your machine.
2. Create a user once the docker is installed otherwise you will need to use sudo every time. (command creates user and group: sudo usermod - aG docker $USER)
3. Install docker-compose (sudo apt install docker-compose).
Use the
docker-compose up
command with the-d
flag to start a multi-container application in detached mode.ubuntu@ip-172-31-93-214:~/django-todo-cicd$ sudo docker-compose up -d
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.docker-compose up -d --scale web_server=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.
Let's create volume first. To create volume in docker we need to use docker volume create command and to see the list of docker volume we need to use docker volume ls command.
docker volume create my_app_data
docker volume ls
So we have created volume my_app_data now our next task is to attach it while creating containers.
Create two or more containers that read and write data to the same volume using the
docker run --mount
command.docker run -d --name cont2 --mount source=my_app_data,target=/app_data nginx:latest
Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.
docker exec -it 972a6496d0a1 /bin/bash ls
Use ctrl+p followed by ctrl+q to come out of the container without the existing 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.
docker volume rm my_app_data docker volume ls
Thank you for reading this blog.Happy Learning!!!!!
You can follow me on LinkedIn for my daily updates:- linkedin.com/in/parimal-pradhan-b62021168
Great initiative by the #trainwithshubham community. Thank you Shubham Londhe