Using Docker Compose to Handle Multiple Containers

Using Docker Compose to Handle Multiple Containers

Why can't we run multiple containers using CLI?

When dealing with multiple containers, relying on Docker CLI commands can quickly become messy and error prone. Writing each command manually increases the chance of mistakes, makes the setup harder to read, and complicates version control.

What is Docker Compose?

Docker Compose is a powerful tool that lets you define and run multi-container Docker applications with ease. It uses a docker-compose.yml file to outline your services, networks, and volumes in a clean, readable format. This approach simplifies the setup and helps you manage container relationships effortlessly.

Advantages of Docker Compose

With one command (docker compose up), you can spin up your entire application stack, all the containers, networking, and volumes in the correct order. Docker Compose handles everything, including service dependencies, scaling, and clean teardown (docker compose down).

Building a Healthcare App with Docker Compose

Let’s say we’re building a Healthcare System with three microservices:

  1. Patient Service – Manages patient data like registration and records.
  2. Doctor Service – Handles doctor details, schedules, and availability.
  3. Appointment Service – Manages appointment bookings between patients and doctors.

Here’s a docker-compose.yml file for this setup:

services:
  patient-service:
    image: healthcare/patient-service:latest
    ports:
      - "8081:8080"
    networks:
      - healthcare-network

  doctor-service:
    image: healthcare/doctor-service:latest
    ports:
      - "8082:8080"
    networks:
      - healthcare-network

  appointment-service:
    image: healthcare/appointment-service:latest
    ports:
      - "8083:8080"
    depends_on:
      - patient-service
      - doctor-service
    networks:
      - healthcare-network

networks:
  healthcare-network:
    driver: bridge        

What’s happening here?

  • We define three services, each with its own Docker image and exposed port.
  • All services are connected to the same network, allowing easy inter-service communication.
  • Depends on ensures the Appointment Service waits for the other two to start first.

Essential Docker Commands

Here’s a breakdown of some essential Docker commands to help manage your containers and images efficiently:

Working with Docker Images

  • docker images: List all Docker images present on the Docker server.
  • docker build . -t [image-name]: Build a Docker image from a Dockerfile.
  • docker image inspect [image-id]: Display detailed information for a given image.
  • docker image rm [image-id]: Remove one or more images by image ID.
  • docker image prune: Remove all unused images.
  • docker history [image-name]: Show intermediate layers and commands used to build the image.
  • docker image push [container_registry/username:tag]: Push an image to a container registry.
  • docker image pull [container_registry/username:tag]: Pull an image from a container registry.

Managing Docker Containers

  • docker ps: Show all running containers.
  • docker ps -a: Show all containers, including stopped ones.
  • docker container start [container-id]: Start one or more stopped containers.
  • docker container stop [container-id]: Stop one or more running containers.
  • docker container kill [container-id]: Instantly kill one or more running containers.
  • docker container restart [container-id]: Restart one or more containers.
  • docker container pause [container-id]: Pause all processes within one or more containers.
  • docker container unpause [container-id]: Resume all processes within one or more containers.
  • docker container inspect [container-id]: View detailed information about a container.
  • docker container logs [container-id]: Fetch logs from a container.
  • docker container logs -f [container-id]: Follow live log output from a container.
  • docker container stats: Display real-time stats (CPU, memory, I/O) for all containers.
  • docker exec -it [container-id] sh: Open an interactive shell inside a running container.
  • docker rm [container-id]: Remove one or more stopped containers.
  • docker container prune: Remove all stopped containers.

Cleaning and System Pruning

  • docker system prune: Remove stopped containers, dangling images, unused networks, volumes, and cache.
  • docker rmi [image-id]: Remove one or more images.

Working with Docker Hub (Container Registry)

  • docker login -u [username]: Log in to Docker Hub.
  • docker logout: Log out from Docker Hub.

Docker Compose Commands

  • docker compose up: Create and start containers from a Docker Compose file.
  • docker compose down: Stop and remove containers defined in the Compose file.
  • docker compose ps: List running services defined in your Compose file.
  • docker compose logs: View logs from services managed by Compose.
  • docker compose restart [service-name]: Restart specific services.
  • docker compose build: Build or rebuild services from the Compose file.

Now that we’ve covered Docker Compose and important commands, you’re ready to build and manage multi-container applications efficiently. Happy Dockerizing!

To view or add a comment, sign in

More articles by Shubham Gaigawale

Insights from the community

Others also viewed

Explore topics