Define and run multi-container applications with Docker.
Usage:
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file
(default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name
(default: directory name)
--verbose Show more output
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi Do not print ANSI control characters
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by --tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don't check the daemon's hostname against the
name specified in the client certificate
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
--compatibility If set, Compose will attempt to convert deploy
keys in v3 files to their non-Swarm equivalent
Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information
Here are the options from Compose V2:
$ docker compose --help
Usage: docker compose [OPTIONS] COMMAND
Define and run multi-container applications with Docker
Options:
--all-resources Include all resources, even those not used by services
--ansi string Control when to print ANSI control characters ("never"|"always"|"auto") (default "auto")
--compatibility Run compose in backward compatibility mode
--dry-run Execute command in dry run mode
--env-file stringArray Specify an alternate environment file
-f, --file stringArray Compose configuration files
--parallel int Control max parallelism, -1 for unlimited (default -1)
--profile stringArray Specify a profile to enable
--progress string Set type of progress output (auto, tty, plain, quiet) (default "auto")
--project-directory string Specify an alternate working directory
(default: the path of the, first specified, Compose file)
-p, --project-name string Project name
Commands:
attach Attach local standard input, output, and error streams to a service's running container
build Build or rebuild services
config Parse, resolve and render compose file in canonical format
cp Copy files/folders between a service container and the local filesystem
create Creates containers for a service
down Stop and remove containers, networks
events Receive real time events from containers
exec Execute a command in a running container
images List images used by the created containers
kill Force stop service containers
logs View output from containers
ls List running compose projects
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart service containers
rm Removes stopped service containers
run Run a one-off command on a service
scale Scale services
start Start services
stats Display a live stream of container(s) resource usage statistics
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker Compose version information
wait Block until the first service container stops
watch Watch build context for service and rebuild/refresh containers when files are updated
Run 'docker compose COMMAND --help' for more information on a command.
Its configuration file is
docker-compose.yaml (or
.yml). This file is also known as Compose file. It contains:
- services - list of service names; Each service can have:
- build -
- command -
- environment -
- image -
- ports -
- restart -
- volumes -
docker-compose up will read Compose file
, create and start containers (services) start listed there.
To check the state of all services, run docker-compose ps from the working directory (which is by default the one that contains Compose file):
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------
my-project_service1_1 /bin/sh -c yarn Up 0.0.0.0:3001->3001/tcp
install && ...
my-project__postgres_1 postgres.sh Up 0.0.0.0:5432->5432/tcp
my-project_service3_1 /usr/local/bin/example Up 0.0.0.0:4567->4567/tcp
To list the IDs of all running containers:
$ docker-compose ps -q
1e8a22167a4ac8354a456a29550cc7fd5fa5eb80ce68f6db04db8ba9764733cf
a1404d3f917c5daf8921731175ae0aa0940eccb518e9c0fd786183036d000d20
e6aeaf2e79b10a29bc834b0e3f712add1c02216b50afa1193663248162721b86
To list all images:
$ docker-compose images
Container Repository Tag Image Id Size
------------------------------------------------------------------
my-project_service1_1 service1 latest 58f28f305f80 875 MB
my-project__postgres_1 postgres latest f97c959a7d9c 298 MB
my-project_service3_1 service3 latest 4e02459e1040 159 MB
Another way to find the association between running containers and their IDs:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1403d3f917c postgres "postgres.s…" 29 hours ago Up 3 hours 0.0.0.0:5432->5432/tcp postgres
e6aecf2e79b1 service3 "/usr/local/bin/example…" 29 hours ago Up 3 hours 0.0.0.0:4567->4567/tcp browser-engagement_s3_1
1e8a12167a4a service1 "/bin/sh -c 'yarn in…" 29 hours ago Up 3 hours 0.0.0.0:3001->3001/tcp my-project_service1_1
Stops containers and removes containers, networks, volumes, and images
created by `up`.
docker-compose down
It completely destroys containers (but not images).
$ docker-compose down --help
Stops containers and removes containers, networks, volumes, and images
created by `up`.
By default, the only things removed are:
- Containers for services defined in the Compose file
- Networks defined in the `networks` section of the Compose file
- The default network, if one is used
Networks and volumes defined as `external` are never removed.
Usage: down [options]
Options:
--rmi type Remove images. Type must be one of:
'all': Remove all images used by any service.
'local': Remove only images that don't have a
custom tag set by the `image` field.
-v, --volumes Remove named volumes declared in the `volumes`
section of the Compose file and anonymous volumes
attached to containers.
--remove-orphans Remove containers for services not defined in the
Compose file
-t, --timeout TIMEOUT Specify a shutdown timeout in seconds.
(default: 10)
Example:
$ docker-compose down --rmi all --volumes
docker-compose: option to automaticaly remove container after run in docker-compose.yml
docker-compose pull
Let's assume our
docker-compose.yml contains two services, db and db_sync and each of them has some Dockerfile which defines base image for their containers. When we do
docker-compose up first time, Docker will pull the latest version of those images but each subsequent
docker-compose up will not be checking if base images have newer version in the remote image repository - we need to update these images manually. We can look Dockerfile and update each base image manually, like:
$ docker pull node:alpine
We might make mistake and omit some images. Better way is to call
docker-compose pull (from a directory which contains yml file) - it will iterate through each service listed in yml and update its base image(s). Example:
$ docker-compose pull
Pulling db ... done
Pulling db_sync ... done
docker-compose pull
docker-compose up doesn't pull down latest image if the image exists locally #3574
docker-compose restart
It is possible to restart a single container (service). Eg. if we have an app container and db container and we want to restart only app container we can do:
$ docker-compose restart my-app
Caveat: this does not pick up any changes in yml config file or image itself. To rebuild the image and restart the container:
$ docker-compose build my-app && docker-compose restart my-app