Docker Compose Command Reference

Essential Docker Compose commands with explanations and examples

Docker Compose Overview

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services, networks, and volumes.

Step 1: Download Python Code

First, download the Python code by clicking the button below:

Download Python Code

Build the App

docker compose build

Builds the Docker images for services defined in your docker-compose.yml file.

Usage:

Use this command when you've made changes to your Dockerfiles or need to rebuild your images before running your application.

Run the App

docker compose up -d

Starts all services defined in your docker-compose.yml file in detached mode (background).

Note:

When the app runs, launch the voting app in your browser: http://localhost:5000

List the Containers

docker compose ps

Shows a list of containers for the current Docker Compose project with their status and port mappings.

Usage:

Use this command to check the status of your running containers, see their names, and which ports they're using.

View Container Logs

docker compose logs -f web-fe

Shows the logs for a specific service (web-fe in this example) with follow mode enabled.

Explanation:

  • docker compose logs → Shows service logs
  • -f → Follow mode (streams logs in real-time)
  • web-fe → Service name (in docker-compose.yaml) to show logs for

Compose V2 - List Projects

docker compose ls

Lists all Docker Compose projects currently running on your system.

Note:

LS will list the current projects with their names and status.

Deploy Second Version (Fails)

docker compose up -d

Attempts to deploy a second version of the application.

Note:

This fails because we can only run an app a single time with the same configuration.

Deploy with Project Name

docker compose -p test up -d

Starts a Docker Compose project with a specific name, allowing multiple instances.

Note:

This fails because the localhost port 5000 is already assigned. Change the ports value from - "5000:5000" to - "5001:5000"

Deploy Again (After Fix)

docker compose -p test up -d

After fixing the port conflict, deploy the second version again. Check on http://localhost:5001

Check Status:

Check how many versions are running: docker compose ls

Cleanup

docker compose down
docker compose ls
docker compose -p test down
docker compose ls

Stops and removes containers, networks, and volumes created by the Docker Compose projects.

Complete Cleanup:

Run docker compose ls after cleanup to verify all projects are stopped.