0
0
Dockerdevops~5 mins

Why Docker Compose simplifies multi-container apps - Why It Works

Choose your learning style9 modes available
Introduction
Running multiple containers for an app can be tricky because each container needs to be started in the right order and connected properly. Docker Compose solves this by letting you define all containers and their settings in one file, so you can start everything with a single command.
When you want to run a web app with a database and cache together on your local machine.
When you need to test how multiple services work together before deploying to production.
When you want to share your app setup with teammates so they can start all parts easily.
When you want to manage container networks and volumes without complex commands.
When you want to stop and remove all related containers with one simple command.
Config File - docker-compose.yml
docker-compose.yml
version: '3.9'
services:
  web:
    image: nginx:1.23
    ports:
      - "8080:80"
  db:
    image: postgres:15
    environment:
      POSTGRES_USER: exampleuser
      POSTGRES_PASSWORD: examplepass
      POSTGRES_DB: exampledb
    volumes:
      - db-data:/var/lib/postgresql/data
volumes:
  db-data: {}

This file defines two services: web and db.

web runs an Nginx server and maps port 8080 on your computer to port 80 in the container.

db runs a PostgreSQL database with environment variables to set user, password, and database name. It also uses a volume to keep data persistent.

The volumes section creates a named volume db-data to store database files outside the container.

Commands
This command starts all services defined in the docker-compose.yml file in the background. It creates containers, networks, and volumes as needed.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating volume "db-data" with default driver Creating example_db_1 ... done Creating example_web_1 ... done
-d - Run containers in detached mode (in the background)
This command lists all running containers started by Docker Compose, showing their status and ports.
Terminal
docker-compose ps
Expected OutputExpected
Name Command State Ports ---------------------------------------------------------------------------- example_db_1 docker-entrypoint.sh postgres Up 5432/tcp example_web_1 nginx -g 'daemon off;' Up 0.0.0.0:8080->80/tcp
This command shows the logs from the web service container, helping you see what the web server is doing or if there are errors.
Terminal
docker-compose logs web
Expected OutputExpected
[example_web_1] 2024/06/01 12:00:00 [notice] 1#1: using the "epoll" event method [example_web_1] 2024/06/01 12:00:00 [notice] 1#1: nginx/1.23.0 started
This command stops and removes all containers, networks, and default volumes created by Docker Compose, cleaning up your environment.
Terminal
docker-compose down
Expected OutputExpected
Stopping example_web_1 ... done Stopping example_db_1 ... done Removing example_web_1 ... done Removing example_db_1 ... done Removing network default
Key Concept

If you remember nothing else from this pattern, remember: Docker Compose lets you manage multiple containers as one app with a simple file and commands.

Common Mistakes
Running individual docker run commands for each container instead of using docker-compose.
This makes it hard to manage dependencies, networks, and volumes between containers manually.
Use a docker-compose.yml file to define all containers and start them together with docker-compose up.
Not mapping ports in the compose file, so services are not accessible from the host machine.
Without port mapping, you cannot reach the container services from your computer.
Define ports under each service to map container ports to host ports.
Forgetting to use volumes for databases, causing data loss when containers are removed.
Without volumes, database data is lost when the container stops or is deleted.
Use named volumes in docker-compose.yml to persist important data.
Summary
Docker Compose uses a single YAML file to define multiple containers and their settings.
The 'docker-compose up -d' command starts all containers in the background with one command.
You can check running containers with 'docker-compose ps' and stop everything with 'docker-compose down'.