Docker Config Reference (docker compose)

We put out an excellent guide for using docker compose

Docker Config Reference (docker compose)

Example 1

docker compose up -d

This command initiates all services defined in the docker-compose.yml file in detached mode, facilitating background operation without occupying the terminal, which is suitable for production deployments requiring continuous runtime.

Example 2

docker compose down

This command halts and removes all containers, networks, and volumes associated with the services in the docker-compose.yml file, ensuring a complete cleanup of resources after testing or development cycles.

Example 3

docker compose ps

This command lists the status of all containers managed by Docker Compose, providing details such as names, commands, states, and ports, which is essential for monitoring active services in a multi-container application.

Example 4

docker compose logs -f

This command displays and follows the logs from all services in real-time, enabling continuous observation of output for debugging purposes in dynamic environments.

Example 5

docker compose build

This command constructs or reconstructs the images for services defined in the docker-compose.yml file, incorporating any changes in Dockerfiles, which is critical for updating application dependencies.

Example 6

docker compose start

This command commences the stopped containers for the services, resuming operations without recreating them, which is useful for restarting after a temporary pause.

Example 7

docker compose stop

This command gracefully terminates the running containers, preserving their state for future restarts, ideal for maintenance windows in operational setups.

Example 8

docker compose restart

This command stops and then restarts all services, applying configuration changes or recovering from transient issues without full redeployment.

Example 9

docker compose exec web sh

This command executes a shell (sh) within the "web" service container, allowing interactive command-line access for troubleshooting or configuration adjustments.

Example 10

docker compose run --rm temp-service

This command launches a one-off container for the "temp-service", removing it upon completion, suitable for ad-hoc tasks like database migrations.

Example 11

docker compose config

This command validates and displays the composed configuration from the docker-compose.yml file, ensuring syntax correctness before deployment.

Example 12

docker compose pull

This command retrieves the latest images for all services from their registries, updating the local environment to the most recent versions.

Example 13

docker compose push

This command uploads the built images for services to their respective registries, facilitating distribution in CI/CD pipelines.

Example 14

docker compose pause

This command suspends the processes within all running containers, temporarily halting operations while maintaining state, useful for resource conservation.

Example 15

docker compose unpause

This command resumes processes in paused containers, restoring normal functionality after a suspension period.

Example 16

docker compose top

This command shows the top processes running in each service container, providing insights into resource utilization and performance.

Example 17

docker compose events --json

This command streams real-time events from the services in JSON format, enabling programmatic monitoring of lifecycle changes.

Example 18

docker compose version

This command outputs the version information of Docker Compose, verifying compatibility and installation details.

Example 19

docker compose kill

This command forcibly terminates all running containers by sending a SIGKILL signal, applicable in scenarios requiring immediate shutdown.

Example 20

docker compose rm -f

This command removes stopped containers without confirmation, streamlining cleanup in automated scripts.

Example 21

docker compose up --scale web=3

This command starts services while scaling the "web" service to three instances, supporting load-balanced applications.

Example 22

docker compose down --volumes

This command stops services and removes associated volumes, ensuring complete data erasure for security-sensitive operations.

Example 23

docker compose logs web

This command retrieves logs specifically from the "web" service, isolating output for targeted analysis.

Example 24

docker compose build --no-cache

This command rebuilds images without using cached layers, guaranteeing fresh dependencies in controlled build environments.

Example 25

docker compose exec -u root db mysql

This command executes the mysql client as root user in the "db" container, allowing administrative database tasks.

Example 26

docker compose run -e DEBUG=true app

This command runs the "app" service with an overridden environment variable "DEBUG=true", enabling debug mode for testing.

Example 27

docker compose up --force-recreate

This command recreates containers even if unchanged, forcing a fresh start in scenarios with persistent state issues.

Example 28

docker compose down --rmi all

This command stops services and removes all associated images, performing a thorough reset of the environment.

Example 29

docker compose ps --services

This command lists only the service names defined in the compose file, useful for scripting or integration.

Example 30

docker compose convert

This command converts the compose file to a different format or validates it for compatibility with other tools, aiding in migration processes.