Docker Series — Part 8: Automating Containers with Docker Compose

Nitin DhimanNitin Dhiman
3 min read

Welcome to Part 8 of the Docker: Basics to Advance series.
In the previous parts, we manually deployed containers, set up networking, and even ran WordPress with MySQL. Now, it's time to scale things up — and automate.

What Is Docker Compose?

Imagine spinning up multiple containers — like a DB, a web app, a caching layer — each with its own setup and networking, all in one command.
That’s what Docker Compose is built for.

It’s a tool that helps you:

  • Define container setups using YAML

  • Automate multi-container launches

  • Manage entire app stacks declaratively


Why Use Docker Compose?

Manually typing docker run commands for every container is:

  • Time-consuming

  • Error-prone

  • Not scalable

With Docker Compose, you write your config once in a YAML file, then just run:

docker-compose up

…and your stack is ready.


Installing Docker Compose

curl -SL https://github.com/docker/compose/releases/download/v2.12.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

Check version:

docker-compose version

Tip: Compose runs on top of the Docker engine, so compatibility matters.
Refer to the compatibility matrix if needed.


YAML: The Heart of Docker Compose

Compose config is written in YAML — a human-readable data format. Some basics:

version: "3.9"
services:
  web:
    image: "nginx"
    ports:
      - "8080:80"
  db:
    image: "mysql"
    environment:
      - MYSQL_ROOT_PASSWORD=secret
  • Use colons :

  • Use hyphens - for lists

  • Maintain indentation carefully — YAML is space-sensitive!


Basic Docker Compose Commands

CommandDescription
docker-compose upStarts all services in the YAML
docker-compose stopStops running containers
docker-compose psLists running services
docker-compose logs <service>View logs of a specific service
docker-compose exec <service> <cmd>Run commands inside a running container
docker-compose downStops and removes all services
-f filename.ymlRun with a specific YAML file
-dRun in detached/background mode

One-Shot Containers: Run and Exit

Sometimes, you just want to run a command and shut down the container:

docker run -it ubuntu:14.04 date

Or use --rm to auto-delete the container after running the command:

docker run -it --rm ubuntu:14.04 date

You can even delay container exit with sleep command:

docker run -it ubuntu:14.04 sleep 10

Compose File Structure (Example)

#file name - myapp.yml
version: "3"
services:
  app:
    image: "ubuntu:14.04"
    command: sleep 10

Run it:

docker-compose -f myapp.yml up

Key Concepts Recap

ConceptSummary
Docker ComposeAutomates container deployments using YAML
YAMLConfiguration file format (indentation sensitive)
ServicesDefine containers inside services: block
Detached Mode-d runs containers in the background
Logs & ExecUseful for debugging and command execution
One-Shot ContainersRun-and-destroy containers with --rm

Why Docker Compose Matters

When you're working on real-world projects, Compose lets you:

  • Version control your infrastructure

  • Automate local dev environments

  • Simulate production app stacks

  • Onboard team members faster


Have questions about YAML formatting, detached mode, or automating services with Compose?
Drop a comment — let’s learn together!

0
Subscribe to my newsletter

Read articles from Nitin Dhiman directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nitin Dhiman
Nitin Dhiman

Self-taught DevOps enthusiast on a journey from beginner to pro. Passionate about demystifying complex tools like Docker, AWS, CI/CD & Kubernetes into clear, actionable insights. Fueled by curiosity, driven by hands-on learning, and committed to sharing the journey. Always building, always growing 🚀