0
0
Dockerdevops~5 mins

docker-compose.yml structure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Docker Compose helps you run multiple containers together easily. It uses a file called docker-compose.yml to define how containers work together, like setting their names, ports, and shared networks.
When you want to start a web app and a database together on your computer.
When you need to test how different services talk to each other locally.
When you want to share your app setup with teammates so they can run it exactly the same way.
When you want to stop and start all parts of your app with one command.
When you want to keep your container settings in one easy-to-read file.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  web:
    image: nginx:1.23
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
  db:
    image: postgres:15
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: exampledb
    ports:
      - "5432:5432"

version: Defines the Compose file format version.

services: Lists the containers to run.

web: A service using the nginx image, exposing port 80 inside the container to port 8080 on the host, and sharing local html files.

db: A PostgreSQL database service with environment variables for user, password, and database name, exposing its default port.

Commands
Starts all services defined in docker-compose.yml in the background so your app and database run together.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating volume "default" with default driver Creating example_db_1 ... done Creating example_web_1 ... done
-d - Run containers in detached mode (in the background)
Shows the status of all running services defined in your docker-compose.yml file.
Terminal
docker-compose ps
Expected OutputExpected
Name Command State Ports ---------------------------------------------------------------------------- example_db_1 docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp example_web_1 nginx -g 'daemon off;' Up 0.0.0.0:8080->80/tcp
Stops and removes all containers, networks, and volumes created by docker-compose up.
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.yml defines multiple containers and how they work together in one simple file.

Common Mistakes
Using tabs instead of spaces in the docker-compose.yml file
YAML files require spaces for indentation; tabs cause parsing errors.
Always use spaces (usually 2) for indentation in docker-compose.yml.
Not exposing container ports to the host in the ports section
Without port mapping, you cannot access the service from your computer.
Map container ports to host ports using the format 'hostPort:containerPort' under ports.
Running docker-compose up without the -d flag and closing the terminal
The containers stop when the terminal closes if not run in detached mode.
Use 'docker-compose up -d' to keep containers running in the background.
Summary
docker-compose.yml defines multiple containers and their settings in one file.
Use 'docker-compose up -d' to start all services in the background.
Use 'docker-compose ps' to check running containers and 'docker-compose down' to stop and clean up.