Day 19: Maximizing Docker Efficiency: Harnessing Volumes and Networks in DevOps
Table of contents
๐ท Docker Volume
Think of a Docker volume as a special folder that Docker containers can share and access. It's a way for containers to store and retrieve data separately from the container itself. Imagine you have a group project where everyone needs to access the same set of files. Instead of each person keeping a copy on their own laptop, you all work from a shared folder stored in a central location. Docker volumes work in a similar way, allowing containers to easily access and share data, making it handy for tasks like database storage, file sharing, or configuration files.
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. reference
๐ท Docker Network
Picture a Docker network as a digital highway that allows different containers to communicate with each other. Just like cars on a highway can travel to different destinations and interact with each other, containers on a Docker network can send data and messages to one another. This network helps containers talk to each other, share information, and work together smoothly, making it easier for your applications to function effectively in a Docker environment.
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. reference
๐ท Task 1
Create a multi-container docker-compose file which will bring UP and bring DOWN containers in a single shot.
- git clone from
I have already cloned the file from git so it'll just say the file already exists
Use the docker-compose up
command with the -d
flag to start a multi-container application in detached mode.
#To start the multi-container
docker-compose up -d
Use the docker-compose ps
command to view the status of all containers, and docker-compose logs
to view the logs of a specific service.
#To view the status
docker-compose ps
Use the docker-compose down
command to stop and remove all containers, networks, and volumes associated with the application
#To stop the container
docker-compose down
๐ท Task 2
- Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.
#To create docker volume
docker volume create --name django_todo_volume --opt type=none --opt device=/home/sh/Desktop/90daysofdevops/projects/django-todo-cicd/volume --opt o=bind
- Create two or more containers that read and write data to the same volume using the
docker run --mount
command.
#mount
docker run -d --mount source=django_todo_volume,target=/data -p 8000:8000 django-todo-cicd: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 <container-id> bash
docker exec -it 23e4f8de944c bash
Use the docker volume ls command to list all volumes and the docker volume rm command to remove the volume when you're done.
I already deleted and removed the container images which i forgot to take screenshot of so here is the next step
In conclusion, Docker volumes play a crucial role in managing data persistence and sharing between containers, especially in multi-container and multi-stage setups. Volumes provide a mechanism to decouple data storage from the container lifecycle, ensuring data integrity, scalability, and easy container management.
For multi-container applications orchestrated with tools like Docker Compose, volumes enable seamless data sharing between different services, allowing them to collaborate without the constraints of container boundaries. This promotes modularization, enhances security, and simplifies data management.
~Dipen : )