π Docker Volumes & Networks for DevOps Engineers.
Docker Volumes & Networks: Persistent Storage and Seamless Container Communication.
ποΈ Docker Volumes (Persistent Storage)
By default, container data is deleted when a container is removed. Volumes store data outside the container so it persists even if the container is deleted.
π Key Benefits:
β Data stays even if the container is removed.
β Multiple containers can share the same volume.
β Better performance than bind mounts.
π Useful Commands:
β Create a volume:
docker volume create my_volume
β List all volumes:
docker volume ls
β Inspect volume details:
docker volume inspect my_volume
β Remove a volume:
docker volume rm my_volume
π Docker Network (Container Communication)
Containers by default are isolated. Docker Networks allow containers to communicate with each other securely.
π Network Types:
β Bridge (Default) β Connects containers on the same host.
β Host β Uses the hostβs network directly.
β Overlay β Used in multi-host Docker Swarm setups.
π Useful Commands:
β Create a network:
docker network create my_network
β List all networks:
docker network ls
β Inspect network details:
docker network inspect my_network
β Remove a network:
docker network rm my_network
ποΈ Task 1: Multi-Container Setup Using Docker Compose
Let's create a multi-container setup with an application and a database using docker-compose which uses volume and network concept.
π Example docker-compose.yml
file:
version: '3.8'
services:
app:
image: my-app
networks:
- my_network
depends_on:
- db
db:
image: postgres
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
volumes:
- db_data:/var/lib/postgresql/data
networks:
- my_network
volumes:
db_data:
networks:
my_network:
π Running the Setup:
β Start containers in detached mode:
docker-compose up -d
β Stop and clean up everything:
docker-compose down
π Task 2: Share Data Between Containers Using Volumes
Let's create two containers that share the same volume and verify data persistence.
β Run two containers with a shared volume:
docker run -d --name c1 --mount source=shared_vol,target=/data busybox
docker run -d --name c2 --mount source=shared_vol,target=/data busybox
πΉ Explanation:
docker run
β Starts a new container.-d
β Runs the container in detached mode (in the background).--name c1
β Assigns the namec1
to the first container.--mount source=shared_vol,target=/data
βsource=shared_vol β Uses a named volume called
shared_vol
.target=/data β Mounts it inside the container at
/data
.
busybox
β A lightweight Linux-based container image.
Both c1
and c2
use the same volume, meaning they can share data.
β Write data in container1:
docker exec c1 sh -c "echo 'Hello from container1' > /data/message.txt"
πΉ Explanation:
docker exec c1
β Runs a command inside the running containerc1
.sh -c
β Executes the following shell command.echo 'Hello from container1' > /data/message.txt
βWrites "Hello from container1" into the file
/data/message.txt
.Since
/data
is a shared volume, this file is accessible fromc2
.
β Read data from container2:
docker exec c2 cat /data/message.txt
πΉ Explanation:
docker exec c2
β Runs a command inside thec2
container.cat /data/message.txt
β Reads and displays the content ofmessage.txt
.Since
c1
wrote the file to the shared volume,c2
can now read it! β
β List all volumes:
docker volume ls
β Remove the volume when done:
docker volume rm shared_vol