Day 19 of #90daysofdevops
Hey Techies! Welcome to this blog
In this blog, we are going to start the Docker Project for DevOps Engineers (Part - 3)
Docker Volume
Docker Volume is a storage mechanism that is used for persisting data generated by and used by Docker containers. Docker Volumes are managed by Docker itself and are independent of the container lifecycle. This means that the data in a volume is not lost when a container is stopped or deleted, and can be easily backed up or migrated.
Docker Volumes have several advantages over other methods of storing data in containers, such as:
Volumes are easier to use and more flexible than bind mounts, which depend on the directory structure and OS of the host machine.
Volumes can be shared and reused among multiple containers, even across different hosts.
Volumes can use different drivers to store data on remote hosts, and cloud providers, or to encrypt or compress the data.
Volumes can be created with default content from an existing container or image.
Docker Network
Docker Network is a virtual network created by Docker to enable communication between Docker containers. Docker Network allows containers to connect to each other and to non-Docker workloads, such as external services or databases. Docker Network also provides network isolation and security for containers.
Docker Network supports different types of networks, such as:
Bridge: The default network type that connects containers to a private network on the host machine. Containers on the same bridge network can communicate with each other by name or alias, while containers on different bridge networks need to use port mapping or links to communicate.
Host: A network type that connects containers directly to the host’s network stack, without any isolation. Containers on the host network can communicate with each other and with any other process on the host, but they cannot use port mapping or links.
Overlay: A network type that connects containers across multiple hosts or clusters, using a distributed key-value store such as Consul or etcd. Containers on the same overlay network can communicate with each other by name or alias, regardless of their physical location.
Macvlan: A network type that assigns a MAC address to each container and connects them to the host’s physical network interface, without any isolation. Containers on the same macvlan network can communicate with each other and with any other device on the same subnet, but they cannot communicate with the host machine.
None: A network type that disables networking for a container, providing only a loopback interface. Containers on the none network cannot communicate with any other container or process.
Create a multi-container docker-compose file
You will create a multi-container docker-compose file that will bring up and bring down containers in a single shot. You will create an application that consists of a web server, an API server, and a MongoDB database.
version: '3'
services:
redis-server:
image: redis
app:
build: .
ports:
- "8000:8000"
app-2:
build: .
ports:
- "8001:8000"
app-3:
build: .
ports:
- "8002:8000"
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 stop the container
docker-compose down
Task 2: Docker Volumes
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/DevOps/projects/volumes/django-app --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 -p 8000:8000 --mount source=django-todo-volume,target=/data django-todo-app:latest
- Verify that the data is the same in all containers by using the
docker exec
command to run commands inside each container.
- Verify that the data is the same in all containers by using the
#Docker exec -it <container-id> bash
docker exec -it 76250168a0c9 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 screenshots of so here is the next step.
And 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.
Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Happy Learning!
~kritika :)