πŸš€ Docker Volumes & Networks for DevOps Engineers.

πŸš€ Docker Volumes & Networks for DevOps Engineers.

Docker Volumes & Networks: Persistent Storage and Seamless Container Communication.

Β·

3 min read


πŸ—„οΈ 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 name c1 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 container c1.

  • 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 from c2.


βœ… Read data from container2:

docker exec c2 cat /data/message.txt

πŸ”Ή Explanation:

  • docker exec c2 β†’ Runs a command inside the c2 container.

  • cat /data/message.txt β†’ Reads and displays the content of message.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

Β