Day 19 Docker for DevOps Engineers
Docker Volumes and Networks: Key Concepts for DevOps
What is a Docker Volume?
A Docker volume can persist and share container data beyond its lifecycle. It enables:
Data persistence: Ensures data survives container restarts and removals.
Data sharing: Allows multiple containers to read and write to the same data.
Ideal Use Cases:
- Storing database files, logs, or application data that needs to persist.
What is a Docker Network?
Docker networks enable containers to communicate with each other and external systems securely and efficiently. By grouping containers in virtual networks, you can control how they interact.
Ideal Use Cases:
Connecting a web application container to a database container.
Isolating services for security and performance.
Task 1: Multi-Container Application with Docker Compose
Objective: Deploy an application and database together using Docker Compose.
Step 1: Create docker-compose.yml
version: '3'
services:
app:
image: my_app_image
ports:
- "5000:5000"
volumes:
- app_data:/usr/src/app
depends_on:
- db
db:
image: postgres:latest
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mydatabase
volumes:
- db_data:/var/lib/postgresql/data
volumes:
app_data:
db_data:
Key Features:
App service: This is used
my_app_image
and linked to the database container.DB service: Runs PostgreSQL with persistent data storage in a volume.
Step 2: Manage the Multi-Container Setup
Start Containers:
docker-compose up -d
- Runs all services in detached mode.
Scale the App Service (e.g., 3 replicas):
docker-compose up --scale app=3 -d
View Logs for troubleshooting:
docker-compose logs app
Stop and Remove Containers:
docker-compose down
Task 2: Sharing Data Using Docker Volumes
Objective: Share data between two containers using a shared volume.
Step 1: Create Containers with a Shared Volume
docker run -d --name container1 --mount source=shared_volume,target=/data busybox
docker run -d --name container2 --mount source=shared_volume,target=/data busybox
Step 2: Write Data in One Container
docker exec container1 sh -c "echo 'Hello from container1' > /data/file.txt"
Step 3: Read Data in Another Container
docker exec container2 cat /data/file.txt
Output:
Hello from container1
Step 4: Clean Up
List Volumes:
docker volume ls
Remove Shared Volume:
docker volume rm shared_volume
Why Volumes and Networks Matter for DevOps
Volumes: Enable data persistence and sharing, essential for stateful applications.
Networks: Provide secure, efficient communication between services.
Docker Compose: Simplifies the orchestration of multi-container applications, boosting productivity and reliability.
Summary of Commands Used
Command | Description |
docker-compose up -d | Start services defined in docker-compose.yml in detached mode. |
docker-compose ps | View the status of all containers. |
docker-compose logs <service> | View logs for a specific service. |
docker-compose down | Stop and remove all containers, networks, and volumes. |
docker volume create <volume-name> | Create a new Docker volume. |
docker run -d --name <container-name> --mount source=<volume>,target=<path> <image> | Run a new container with a shared volume. |
docker exec -it <container-name> <command> | Run a command inside a running container. |
docker volume ls | List all Docker volumes. |
docker volume rm <volume-name> | Remove a specific Docker volume. |
Mastering these concepts helps DevOps engineers build scalable and resilient containerized applications. Happy Dockerizing! 🚀
Subscribe to my newsletter
Read articles from Fauzeya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Fauzeya
Fauzeya
Hi there! I'm Fauzeya 👩💻, a passionate DevOps Engineer with a background in Computer Science Engineering🎓. I’m committed to enhancing security🔒, efficiency⚙️, and effectiveness in software development and deployment processes. With extensive knowledge in cloud computing☁️, containerization📦, and automation🤖, I aim to stay updated with the latest tools and methodologies in the DevOps field. Currently, I’m on a journey to deepen my understanding of DevOps I enjoy sharing my learning experiences and insights through my blog, 📝where I cover topics related to DevOps practices, tutorials, and challenges. I believe in continuous growth and learning and am excited to connect with fellow tech enthusiasts and professionals🤝. Let’s embark on this journey together!🚀