Docker for DevOps Engineers
Volumes and Networks for DevOps Engineers
Docker Volumes and Docker Networks. These allow containers to store data persistently and communicate effectively in a multi-container environment.
Docker Volumes and Networks?
1: Multi-Container Docker Compose Setup
One of Docker’s strengths is managing multi-container applications using Docker Compose. Imagine you need to deploy both an application and a database in separate containers but want to bring them up together. Here’s how you can do it with a single command using a docker-compose.yml file.
1: Creating the docker-compose.yml File
version: '3'
services:
app:
image: my_app_image # Replace with your application image
ports:
- "5000:5000" # Mapping host port to container port
volumes:
- app_data:/usr/src/app
depends_on:
- db # App depends on the database service
db:
image: postgres:latest # Using PostgreSQL as an example
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mydatabase
volumes:
- db_data:/var/lib/postgresql/data # Persistent data storage for DB
volumes:
app_data: # Volume for application
db_data: # Volume for database
This YAML file defines two services:
2: Managing the Multi-Container Application
docker-compose up -d
2. Scaling the application: Need more instances of the app? Scale it with this command:
docker-compose up --scale app=3 -d
3. Viewing container status: Check the status of your containers with:
docker-compose ps
4. Viewing logs: To troubleshoot or monitor a specific service, use:
Recommended by LinkedIn
docker-compose logs app
5. Stopping and removing containers: When you’re done, you can stop and remove all containers, networks, and volumes with:
docker-compose down
2: Sharing Data Between Containers Using Docker Volumes
you might need multiple containers to share the same data. Docker Volumes make this possible by allowing containers to read from and write to the same storage area. Here’s how to set it up.
1: Create Containers with Shared Volumes
# Create first container and mount the volume
docker run -d --name container1 --mount source=shared_volume,target=/data busybox
# Create second container and mount the same volume
docker run -d --name container2 --mount source=shared_volume,target=/data busybox
2: Write Data in One Container
docker exec container1 sh -c "echo 'Hello from container1' > /data/file.txt"
3: Verify Data in the Second Container
docker exec container2 cat /data/file.txt
output
Hello from container1
4: Listing and Removing Volumes
docker volume ls
clean up by removing the shared volume:
docker volume rm shared_volume
Volumes and Networks Matter for DevOps From my Pov
Why Volumes and Networks Matter in DevOps:
Together, volumes and networks provide reliability, flexibility, and security, making them essential for managing containerized applications in DevOps.