Bird
Raised Fist0
Dockerdevops~5 mins

docker compose up and down - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What does the command docker compose up do in a multi-container application?
easy
A. Deletes the docker-compose file
B. Stops and removes all containers
C. Starts all the containers defined in the docker-compose file
D. Builds images but does not start containers

Solution

  1. Step 1: Understand the purpose of docker compose up

    This command reads the docker-compose.yml file and starts all the containers defined in it.
  2. Step 2: Compare with other commands

    docker compose down stops and removes containers, so it is not the same as up.
  3. Final Answer:

    Starts all the containers defined in the docker-compose file -> Option C
  4. Quick Check:

    docker compose up = start containers [OK]
Hint: Remember: 'up' means start containers [OK]
Common Mistakes:
  • Confusing 'up' with 'down'
  • Thinking 'up' deletes files
  • Assuming 'up' only builds images
2. Which of the following is the correct command to stop and remove all containers started by docker compose?
easy
A. docker compose stop
B. docker compose down
C. docker compose remove
D. docker compose kill

Solution

  1. Step 1: Identify the command to stop and remove containers

    docker compose down stops and removes containers, networks, and default volumes created by up.
  2. Step 2: Differentiate from other commands

    docker compose stop only stops containers but does not remove them. remove is not a valid command here.
  3. Final Answer:

    docker compose down -> Option B
  4. Quick Check:

    down = stop and remove [OK]
Hint: Use 'down' to stop and clean containers [OK]
Common Mistakes:
  • Using 'stop' which doesn't remove containers
  • Trying non-existent commands like 'remove'
  • Confusing 'kill' with 'down'
3. Given this sequence of commands:
docker compose up -d
docker compose down
What will be the state of the containers after these commands?
medium
A. Containers are removed and not running
B. Containers are stopped but still exist
C. Containers are running in detached mode
D. Containers are paused

Solution

  1. Step 1: Analyze docker compose up -d

    This starts containers in detached mode (running in background).
  2. Step 2: Analyze docker compose down

    This stops and removes all containers started by up.
  3. Final Answer:

    Containers are removed and not running -> Option A
  4. Quick Check:

    up -d starts, down removes [OK]
Hint: 'down' removes containers even if started detached [OK]
Common Mistakes:
  • Thinking containers remain stopped but not removed
  • Confusing paused with stopped
  • Assuming detached means containers stay after down
4. You ran docker compose down but your containers are still running. What is the most likely reason?
medium
A. You ran docker compose down in a different directory than the compose file
B. You forgot to add -d flag
C. The containers were started with docker run instead
D. Docker daemon is not running

Solution

  1. Step 1: Understand how docker compose down works

    This command affects containers defined in the compose file in the current directory.
  2. Step 2: Identify why containers remain running

    If run in a different directory, it won't find or stop those containers.
  3. Final Answer:

    You ran docker compose down in a different directory than the compose file -> Option A
  4. Quick Check:

    Correct directory needed for down [OK]
Hint: Run commands where your compose file is located [OK]
Common Mistakes:
  • Thinking -d flag affects stopping containers
  • Assuming down stops containers from docker run
  • Not checking Docker daemon status
5. You want to restart your multi-container app with fresh containers, removing old volumes and networks. Which command should you run?
hard
A. docker compose up --build
B. docker compose stop && docker compose start
C. docker compose restart
D. docker compose down -v && docker compose up -d

Solution

  1. Step 1: Understand the need to remove old volumes and networks

    Removing volumes requires the -v flag with docker compose down.
  2. Step 2: Combine commands to remove and start fresh

    docker compose down -v removes containers, networks, and volumes; docker compose up -d starts fresh containers in detached mode.
  3. Final Answer:

    docker compose down -v && docker compose up -d -> Option D
  4. Quick Check:

    Use down -v to remove volumes before up [OK]
Hint: Use 'down -v' to remove volumes before 'up' [OK]
Common Mistakes:
  • Using 'restart' which doesn't remove volumes
  • Using 'stop' and 'start' which keep old volumes
  • Assuming '--build' removes volumes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard