Understanding Docker Volumes and Networks: Complete Guide with Examples

Hey there, curious minds! In this blog, we'll delve into two fundamental Docker concepts—volumes and networks. Docker volumes provide a robust mechanism for persisting data beyond container lifecycles, enabling sharing and efficient management between containers and the host machine. On the other hand, Docker networks foster communication between containers, offering isolation, scalability, and customization options. Join me as I explore these foundational elements, understanding their significance in containerization, and unlocking their potential for robust and flexible Docker environments.

Docker Volume

Docker volumes serve as a pivotal tool in the Docker ecosystem, providing persistent storage for containers that outlast their life cycles. They act as dedicated data storage entities, decoupled from containers, ensuring data longevity even when containers are deleted or stopped. These volumes facilitate efficient data sharing and management between containers and the host machine.

Benefits of Docker volumes include:

  1. Persistent Data: Ensure data persistence across container reboots or recreation, preserving vital information beyond the container's life span.

  2. Shared Data: Enable multiple containers to access and modify shared data, fostering collaboration and seamless interaction.

  3. Simplified Backup and Restore: Streamline data backup and restoration processes by isolating data from container dependencies.

Docker volumes can be easily managed using commands or defined within Docker Compose files, simplifying the management of persistent data across containers.

Docker Network

Docker networks are the backbone of container communication within a Docker environment. They serve as enclosed environments where containers interact, ensuring seamless communication while maintaining distinct traffic segregation.

Benefits of Docker networks include:

  1. Isolation: Docker networks segregate container communication, guaranteeing privacy and isolation for traffic among containers.

  2. Diverse Types: Docker offers various network types—like bridge, overlay, host, and MACVLAN—each tailored for specific use cases. For example, the bridge network facilitates communication among containers on a single host.

  3. Scalability: Networks facilitate scaling applications by enabling communication among containers across multiple hosts in a swarm.

  4. Customization: Docker networks allow customization with specific IP addressing schemes, subnet configurations, and other parameters, ensuring adaptability in network design.

In essence, Docker networks are the linchpin orchestrating container connectivity, contributing significantly to the flexibility and resilience of container-based applications.

Example of docker network:

Create a custom network:
docker network create mynetwork

Run containers and attach to the custom network:
docker run -d --name web --network mynetwork nginx
docker run -d --name db --network mynetwork -e MYSQL_ROOT_PASSWORD=example_password mysql

verify network of the containers: docker inspect -f '{{range .NetworkSettings.Networks}}{{.NetworkID}}{{end}}' <container_name_or_id>

or you can use docker-compose.yaml file as shown below. Using this file you don't have to create docker network using docker create network command, docker-compose will create it for you automatically.

run docker-compose up -d

Task-1

  • Create a multi-container docker-compose file which will bring UP and bring DOWN containers in a single shot ( Example - Create application and database container ). Sample docker-compose.yaml file.

    Solution:

    Start the services using Docker Compose:
    docker-compose up -d

Use the docker-compose ps command to view the status of all containers.

Use docker-compose logs <service_name> to view the logs of a specific service.

Use the docker-compose up --scale <service_name>=<number_of_replicas> command to increase or decrease the number of replicas for 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.

  • Create two or more containers that read and write data to the same volume using the docker run --mount command.

  • Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.

  • Use the docker volume ls command to list all volumes and docker volume rm command to remove the volume when you're done.

Solution:

Example 1-
Create a named volume:
docker volume create my_shared_volume

Run container1 and write data to the shared volume:
docker run -it --name container1 --mount source=my_shared_volume,target=/shared_data alpine sh
Once inside the container, create a file in the shared volume:
echo "Data from container1" > /shared_data/file.txt
exit

Run container2 and read data from the shared volume:
docker run -it --name container2 --mount source=my_shared_volume,target=/shared_data alpine sh
Once inside the container, read the file from the shared volume:
cat /shared_data/file.txt
exit

Verify data consistency between containers: You can verify that both containers container1 and container2 has access to the same data within the shared volume.
List all volumes: docker volume ls

Example 2-

Create a docker-compose.yml file:

Start the services using Docker Compose:
docker-compose up -d
This will create two services: app and logger, each having access to the shared_logs named volume.

Use docker exec to inspect the volume in app service:
docker exec -it <app_container_id> ls /app/logs
docker exec -it <app_container_id> tail -f /app/logs/app.log

Thank you!