Docker for DevOps Engineers

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?

  • Docker Volume: A volume in Docker is a mechanism for containers to store and share data outside of their own filesystem. Volumes ensure that data persists even after containers are stopped or removed, making them ideal for storing databases or files that need to outlive the container’s lifecycle.
  • Docker Network: Docker’s networking feature allows containers to communicate with each other and the outside world. By creating virtual networks, you can group containers together so they can interact securely and efficiently.


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:

  • App: The application container, linked to the database.
  • DB: The database container (PostgreSQL in this example), using a volume for persistent data storage.


2: Managing the Multi-Container Application

  1. Starting the containers: Run the following command to start both containers in the background:

 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:

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:

  • Volumes: Enable data persistence across container restarts, ensuring important data isn't lost. They simplify backups and data sharing between containers.
  • Networks: Allow isolated, secure communication between containers. Networks help configure different services to communicate efficiently without exposing them to external traffic.

Together, volumes and networks provide reliability, flexibility, and security, making them essential for managing containerized applications in DevOps.


To view or add a comment, sign in

More articles by Manit Singh

Others also viewed

Explore content categories