Docker compose Commands
Master Docker Compose commands: Learn to build, start, stop, scale, and manage multi-container applications with ease.
Published Dec 19, 2024
Docker Compose is a tool used for defining and running multi-container Docker applications. It uses YAML files to configure the services, networks, and volumes for your applicationβs containers. Here are some commonly used Docker Compose commands:
π docker-compose up: This command creates and starts all the containers defined in your
docker-compose.yml
file. If the containers don't exist, they are created.If you want the containers to run in detached mode (in the background), you can use the
-d
flag:π docker-compose down: This command stops and removes all the containers defined in your
docker-compose.yml
file.π docker-compose ps: This command shows the status of containers managed by Docker Compose.
By default, only running containers are shown.
--all
flag can be used to include stopped containers.π docker-compose logs: This command displays the logs from all the containers managed by Docker Compose.
π docker-compose build: This command builds the Docker images defined in your
docker-compose.yml
file.π docker-compose exec: This command allows you to run a command inside a running container managed by Docker Compose.
Example:
π docker-compose restart: This command restarts all the containers defined in your
docker-compose.yml
file.π docker-compose stop: This command stops all the containers managed by Docker Compose without removing them.
π docker compose config: The
docker-compose config
command is used to validate and view the configuration of your Docker Compose file (docker-compose.yml
). It checks the syntax of the YAML file and resolves any variables or references, displaying the final configuration that Docker Compose will use when running your services.This command will process the Docker Compose file and display the resolved configuration, including any environment variables that were substituted and the final settings for each service, network, and volume defined in the file.
If there are any syntax errors or issues with the Docker Compose file,
docker-compose config
will report them, helping you identify and fix configuration problems before attempting to run your services with docker-compose up
.Keep in mind that
docker-compose config
does not actually run the services; it only checks and displays the configuration. Use docker-compose up
to start your services based on the validated configuration.π docker compose cp: The
docker-compose cp
command is used to copy files or directories between a container managed by Docker Compose and your local filesystem. It is similar to the docker cp
command but is specifically designed for containers defined in a Docker Compose setup.[options]
: Optional additional options you can specify, such as-v
for verbose output.SERVICE
: The name of the service (container) in your Docker Compose setup.SRC_PATH
: The path of the file or directory inside the container that you want to copy from or to.DEST_PATH
: The destination path on your local filesystem or inside the container where the file or directory should be copied to or from.
For example, to copy a file named
example.txt
from a service named webserver
in your Docker Compose setup to your local filesystem, you would use a command like this:Conversely, if you want to copy a file named
localfile.txt
from your local filesystem to a service named flaskapp
in your Docker Compose setup, you would use a command like this:Just replace
/path/to/example.txt
and /path/to/localfile.txt
with the actual paths to the files you want to copy, and /local/destination/
and /app/
with the destination paths as needed.π docker compose create: The
docker-compose create
command is used to create the containers defined in a Docker Compose file without starting them. This can be useful if you want to prepare the containers but delay their startup until later.Some common options you might use with
docker-compose create
include:-v, --volume
: Create anonymous volumes for all defined volumes.-f, --file FILE
: Specify an alternate compose file (default: docker-compose.yml).--no-deps
: Don't start linked services.
Here are a few examples of how you might use
docker-compose create
:Create containers for specific services (
service1
and service2
) defined in a custom Compose file (custom-compose.yml
):Create containers without starting linked services:
Create containers and create anonymous volumes for defined volumes:
After running
docker-compose create
, you can later start the created containers using docker-compose start
or start and attach to them simultaneously using docker-compose up
.π docker compose events: The
docker-compose events
command is used to stream real-time events from services managed by Docker Compose. These events provide insights into what is happening with your containers, such as when they start, stop, or encounter errors.Stream events for a specific service (
webserver
) defined in a custom Compose file (custom-compose.yml
):Stream events in JSON format for all services:
With the
--json
flag, a json object is printed one per line with the format:π docker compose images: The
docker-compose images
command is used to list the images used by services in your Docker Compose setup. It's similar to the docker images
command, but it specifically shows the images referenced by the services defined in your Docker Compose file.List images used by a specific service (
webserver
) defined in a custom Compose file (custom-compose.yml
):List all images, including intermediate images, for all services:
π docker-compose kill: The
docker-compose kill
command is used to forcefully stop all containers managed by Docker Compose. It sends a SIGKILL signal to the containers, which immediately terminates them without giving them a chance to shut down gracefully.Forcefully stop all containers with a timeout of 5 seconds:
Forcefully stop a with a custom signal (
SIGTERM
):Forcefully stop a specific service (
webserver
) defined in a custom Compose file (custom-compose.yml
):π docker compose ls: The
docker-compose ls
command is used to list the services defined in your Docker Compose file along with their status. It provides a quick overview of the services managed by Docker Compose and their current state (e.g., running, stopped).List services quietly (display only service names):
π docker compose port: The
docker-compose port
command is used to display the public port for a specific service's container. It helps you identify the host port mapped to a container port for a service managed by Docker Compose.For example, to get the host port mapping for port 80 of the
webserver
service:This command will output the host port to which port 80 of the
webserver
container is mapped. The output will typically be in the format HOST_PORT
.If you donβt specify a specific service, the
docker-compose port
command will show the host port mapping for the specified port on all services.For example, to list the host port mapping for port 5000 on all services:
π docker compose pull: The
docker-compose pull
command is used to pull (download) the latest versions of the Docker images specified in your Docker Compose file. This command ensures that you have the most up-to-date versions of the images used by your services.Examples:
Consider the following
compose.yaml
:If you run
docker compose pull ServiceName
in the same directory as the compose.yaml
file that defines the service, Docker pulls the associated image.For example, to call the postgres image configured as the db service in our example, you would run
docker compose pull db
.docker compose pull
tries to pull image for services with a build section. If pull fails, it lets you know this service image must be built. You can skip this by setting --ignore-buildable
flag.π docker compose push: The
docker-compose push
command is used to push (upload) the built Docker images for your services to a container registry. This command is helpful when you want to make your Docker images available for deployment on other systems or share them with others.Ushes images for services to their respective registry/repository.
π docker compose restart: Restarts all stopped and running services, or the specified services only.
If you make changes to your
compose.yml
configuration, these changes are not reflected after running this command. For example, changes to environment variables (which are added after a container is built, but before the container's command is executed) are not updated after restarting.Restart a specific service (
webserver
) defined in your Docker Compose file:πdocker compose rm: Removes stopped service containers.
Running the command with no options also removes one-off containers created by
docker compose run
:ome common options you might use with
docker-compose rm
include:-f, --force
: Force removal of containers and networks (including running containers).-s, --stop
: Stop services before removing associated containers (default behavior).-v, --volumes
: Remove named volumes declared in thevolumes
section of the Compose file.-a, --all
: Remove all containers, networks, and volumes managed by Docker Compose (including running containers).
Keep in mind that
docker-compose rm
only removes stopped containers, networks, and volumes. Running containers are not affected unless you use the -f, --force
option, which forces removal of all containers, including running ones. Use this command with caution to avoid unintended data loss or disruptions.π docker compose version: The
docker-compose version
command is used to display the version of Docker Compose that is installed on your system.When you run this command in your terminal, it will output information similar to the following:
The output includes details such as:
- The version of Docker Compose (
docker-compose version
) - The build ID or commit hash of the Docker Compose version (
build
) - The version of the
docker-py
Python library used by Docker Compose (docker-py version
) - The version of Python (
CPython version
) - The version of OpenSSL (
OpenSSL version
) used by Docker Compose.
π Use -f to specify the name and path of one or more Compose files:
Use the
-f
flag to specify the location of a Compose configuration file.Specifying multiple Compose files:
You can supply multiple
-f
configuration files. When you supply multiple files, Compose combines them into a single configuration. Compose builds the configuration in the order you supply the files. Subsequent files override and add to their predecessors.For example, consider this command line:
The
docker-compose.yml
file might specify a webapp
service.If the
docker-compose.admin.yml
also specifies this same service, any matching fields override the previous file. New values, add to the webapp
service configuration.When you use multiple Compose files, all paths in the files are relative to the first configuration file specified with
-f
. You can use the --project-directory
option to override this base path.Use a
-f
with -
(dash) as the filename to read the configuration from stdin. When stdin is used all paths in the configuration are relative to the current working directory.The
-f
flag is optional. If you donβt provide this flag on the command line, Compose traverses the working directory and its parent directories looking for a compose.yaml
or docker-compose.yaml
file.Specifying a path to a single Compose file:
You can use the
-f
flag to specify a path to a Compose file that is not located in the current directory, either from the command line or by setting up a COMPOSE_FILE
environment variable in your shell or in an environment file.For an example of using the
-f
option at the command line, suppose you are running the Compose Rails sample, and have a compose.yaml
file in a directory called sandbox/rails
. You can use a command like docker compose pull
to get the postgres image for the db service from anywhere by using the -f
flag as follows:π Use -p to specify a project name:
Each configuration has a project name. Compose sets the project name using the following mechanisms, in order of precedence:
- The
-p
command line flag - The
COMPOSE_PROJECT_NAME
environment variable - The top level
name:
variable from the config file (or the lastname:
from a series of config files specified using-f
) - The
basename
of the project directory containing the config file (or containing the first config file specified using-f
) - The
basename
of the current directory if no config file is specified Project names must contain only lowercase letters, decimal digits, dashes, and underscores, and must begin with a lowercase letter or decimal digit. If thebasename
of the project directory or current directory violates this constraint, you must use one of the other mechanisms.
Or:
π Use profiles to enable optional services:
Use
--profile
to specify one or more active profiles Calling docker compose --profile frontend up
starts the services with the profile frontend
and services without any specified profiles. You can also enable multiple profiles, e.g. with docker compose --profile frontend --profile debug up
the profiles frontend
and debug
is enabled.Profiles can also be set by
COMPOSE_PROFILES
environment variable.π Configuring parallelism:
Use
--parallel
to specify the maximum level of parallelism for concurrent engine calls. Calling docker compose --parallel 1 pull
pulls the pullable images defined in the Compose file one at a time. This can also be used to control build concurrency.Parallelism can also be set by the
COMPOSE_PARALLEL_LIMIT
environment variable.π Set up environment variables:
You can set environment variables for various docker compose options, including the
-f
, -p
and --profiles
flags.Setting the
COMPOSE_FILE
environment variable is equivalent to passing the -f
flag, COMPOSE_PROJECT_NAME
environment variable does the same as the -p
flag, COMPOSE_PROFILES
environment variable is equivalent to the --profiles
flag and COMPOSE_PARALLEL_LIMIT
does the same as the --parallel
flag.If flags are explicitly set on the command line, the associated environment variable is ignored.
Setting the
COMPOSE_IGNORE_ORPHANS
environment variable to true
stops docker compose from detecting orphaned containers for the project.π Use Dry Run mode to test your command:
Use
--dry-run
flag to test a command without changing your application stack state. Dry Run mode shows you all the steps Compose applies when executing a command, for example:Β