0
0
Dockerdevops~5 mins

docker compose up and down - Commands & Configuration

Choose your learning style9 modes available
Introduction
Running multiple containers together can be tricky. Docker Compose helps start and stop all related containers with simple commands, making it easy to manage multi-container apps.
When you want to start a web app and its database together on your local machine.
When you need to test how different services work together before deploying.
When you want to quickly stop all running containers of your app to free resources.
When you want to recreate containers after changing configuration or code.
When you want to share your app setup with teammates using a single file.
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: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: exampledb

This file defines two services: web running an Nginx server and db running a PostgreSQL database.

The web service maps port 8080 on your computer to port 80 inside the container.

The db service sets environment variables to configure the database user, password, and database name.

Commands
Starts all services defined in the docker-compose.yml file in the background so you can keep using the terminal.
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 containers started by Docker Compose to verify they are up.
Terminal
docker compose ps
Expected OutputExpected
NAME COMMAND SERVICE STATUS PORTS example_db_1 "docker-entrypoint.s…" db running 5432/tcp example_web_1 "nginx -g 'daemon off;'" web running 0.0.0.0:8080->80/tcp
Stops and removes all containers, networks, and volumes created by docker compose up to clean 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 up starts all your app's containers together, and docker compose down stops and cleans them up.

Common Mistakes
Running 'docker compose up' without the -d flag and expecting the terminal to be free.
Without -d, the command runs in the foreground and blocks the terminal until stopped.
Add the -d flag to run containers in the background: 'docker compose up -d'.
Running 'docker compose down' without realizing it removes containers and networks.
This deletes the running containers and network, so your app stops and data in volumes may be lost if not persisted.
Use 'docker compose down' only when you want to stop and clean up all resources; otherwise, use 'docker compose stop' to just stop containers.
Not checking container status after 'docker compose up' and assuming everything started correctly.
Containers might fail to start due to errors, but 'docker compose up' may not show this clearly without logs.
Run 'docker compose ps' to check status and 'docker compose logs' to see container output if needed.
Summary
Use 'docker compose up -d' to start all services in the background.
Check running containers with 'docker compose ps' to ensure they are healthy.
Use 'docker compose down' to stop and remove all containers and networks created.