Day 19 : Docker for DevOps Engineers: Unraveling Docker Compose, Volumes, and Networks

ยท

3 min read

Day 19 : Docker for DevOps Engineers: Unraveling Docker Compose, Volumes, and Networks

Docker Compose: Simplifying Multi-Container Applications ๐Ÿณ

๐Ÿ“‚ Task-1: Mastering Docker Compose

Step 3: Docker Compose in Action

3.1 Create a Docker Compose File

yamlCopy codeversion: '3'
services:
  webapp:
    image: nginx:latest
    ports:
      - "8080:80"
  database:
    image: postgres:latest
    environment:
      POSTGRES_PASSWORD: mysecretpassword

3.2 Start and Stop Multi-Container Application

bashCopy codedocker-compose up -d
docker-compose down

3.3 Inspect the Docker Compose Environment

bashCopy codedocker-compose ps
docker-compose logs

3.4 Cleanup

bashCopy codedocker-compose down -v  # Remove volumes

Docker Compose Essentials

Docker Compose is a powerful tool for defining and running multi-container Docker applications. It uses a YAML file to configure the application's services, networks, and volumes. In this task, we've created a simple Docker Compose file (docker-compose.yml) that orchestrates two services: webapp and database.

  • Services: Represent the containers that form the application. Each service defined in the docker-compose.yml file can be started or stopped as a single unit.

  • Ports Mapping: The ports section defines the mapping of the host machine's port to the container's port. In our example, the web application's port 80 is mapped to the host machine's port 8080.

  • Environment Variables: The environment section allows us to set environment variables for services. Here, we set the PostgreSQL password for the database service.

  • Docker Compose Commands:

    • docker-compose up -d: Starts the multi-container application in detached mode.

    • docker-compose down: Stops and removes all containers, networks, and volumes associated with the application.

Docker Volumes: Beyond Container Lifecycles

๐Ÿ“‚ Task-2: Mastering Docker Volumes & Named Volumes

Step 1: Using Docker Volumes

1.1 Create a Named Volume

bashCopy codedocker volume create my_data_volume

1.2 Create Containers Sharing the Same Volume

bashCopy codedocker run -d --name container1 --mount source=my_data_volume,target=/data alpine sh -c "echo 'Hello from Container 1' > /data/file.txt && sleep infinity"
docker run -d --name container2 --mount source=my_data_volume,target=/data alpine sh -c "sleep infinity"

1.3 Verify Data Consistency Across Containers

bashCopy codedocker exec container1 cat /data/file.txt  # Output: Hello from Container 1
docker exec container2 cat /data/file.txt  # Output: Hello from Container 1

1.4 Cleanup

bashCopy codedocker rm -f container1 container2
docker volume rm my_data_volume

Docker Volumes Unleashed

Docker volumes provide a way to persist data outside the lifecycle of containers. Key concepts:

  • Named Volumes: Allow for easy referencing and sharing of storage across containers. Created using docker volume create.

  • Mounting Volumes: Containers share data through mounted volumes. The --mount option specifies the source (named volume) and target (container path).

  • Data Consistency: Changes made in one container reflect in others sharing the same volume. Ideal for scenarios requiring shared or persistent data.

  • Cleanup: docker volume rm removes named volumes, offering a clean slate for experimentation.

Feel free to experiment, explore, and enrich your DevOps skill set! ๐Ÿ‘จโ€๐Ÿ’ป๐Ÿ“ฆ

#DevOps #Docker #DockerCompose #DockerVolumes #DockerNetworks #LearnWithMe

ย