Docker Compose commands
Compose simplifies the management of multi-container Docker apps by using a YAML configuration file to define services. With one command, it starts all the services according to your configuration.
Docker Compose Overview
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. Then, with a single command, you create and start all the services from your configuration.
Compose works in all environments; production, staging, development, testing, as well as CI workflows. It also has commands for managing the whole lifecycle of your application:
- Start, stop, and rebuild services
- View the status of running services
- Stream the log output of running services
- Run a one-off command on a service
Important Updates
The new Compose V2, which supports the compose command as part of the Docker CLI, is now available.
Compose V2 integrates compose functions into the Docker platform, continuing to support most of the previous docker-compose features and flags. You can run Compose V2 by replacing the hyphen (-) with a space, using docker compose, instead of docker-compose.
Source: docker compose version
Here is a list of Docker Compose commands that I use:
- docker compose up - Starts all of the services defined in your Docker Compose file.
- docker compose down - Stops and removes all of the services defined in your Docker Compose file.
- docker compose build - Builds the images for all of the services defined in your Docker Compose file.
- docker compose restart - Restarts all of the services defined in your Docker Compose file.
- docker compose logs - Streams the logs for all of the services defined in your Docker Compose file.
- docker compose ps - Lists all of the running containers for the services defined in your Docker Compose file.
- docker compose exec - Runs a one-off command on a service defined in your Docker Compose file.
- docker compose run - Runs a one-off command on a service defined in your Docker Compose file, creating the service if it doesn't already exist.
- docker compose rm
- docker compose pause - Pauses all of the services defined in your Docker Compose file.
- docker compose unpause - Unpauses all of the services defined in your Docker Compose file.
- docker compose port - Prints the public port for a port binding.
In addition to these basic commands, there are a number of other Docker Compose commands that can be useful, such as:
- docker compose pull - Pulls the latest images for all of the services defined in your Docker Compose file.
- docker compose push - Pushes the images for all of the services defined in your Docker Compose file to a registry.
- docker compose scale - Scales the number of replicas for a service.
- docker compose config - Validates and displays the configuration for your Docker Compose file.
- docker compose create - Creates a new Docker Compose file from a template.
- docker compose events - Streams events from Docker Compose.
- docker compose help - Displays help for a specific Docker Compose command.
docker compose up --build
We use the docker compose up --build command to ensure that our Docker images are always up-to-date before we start our containers. This is especially important if we are making changes to our code or Dockerfile.
The docker compose up command starts all of the services defined in our Docker Compose file. However, it will only build the images for those services if they do not already exist. This means that if we make a change to our code or Dockerfile and then run docker compose up, our containers may not be running the latest version of our code.
The --build flag forces Docker Compose to build the images for all of the services defined in our Docker Compose file before starting them. This ensures that our containers are always running the latest version of our code.
Here are some examples of when we might want to use the docker compose up --build command:
- We have made changes to our code or Dockerfile and we want to ensure that our containers are running the latest version of our code.
- We have pulled new images for our services from a registry and we want to start those images.
- We are starting our Docker Compose project for the first time and we need to build the images for all of our services.
It is generally a good practice to use the docker compose up --build command whenever we start our Docker Compose project. This will help to ensure that our containers are always running the latest version of our code.