docker compose
Some notes about the compose functions
Behind the scenes, the Go implementation of Docker compose is called a.k.a. Compose v2, not the Python implementation.
You can verify that docker compose
is installed by running
docker compose --help
Be careful! it's different from docker-compose --help
! Notice the -
between 'docker' and 'compose'.
Compose v2 has no -
in the command.
If that doesn't work, then install the cli plugin. it's just a single binary to download.
The Go implementation of compose is still experimental, so take the appropriate precautions.
If you don't need to set any project-wide options, like the project name or
the compose file path, you can just import docker
and start working.
from python_on_whales import docker
docker.compose.build()
docker.compose.up()
...
docker.compose.down()
Otherwise, you have to define your project-wide options only once, when creating the Docker client.
from python_on_whales import DockerClient
docker = DockerClient(compose_files=["./my-compose-file.yml"])
docker.compose.build()
docker.compose.up()
...
docker.compose.down()
You have multiple compose options available (like profiles, env_files, project name) when creating the Docker client. You can check them out
in the DockerClient
documentation.
About docker.compose.images()
.
The Docker command line has a docker compose images
command. Python-on-whales doesn't have
an equivalent because it's trivial to do so with existing functions.
images = [docker.image.inspect(container.image) for container in docker.compose.ps()]
docker.compose.ps()
returns the list of all containers in the compose stack.container.image
gives you the id of the Docker image of the container as astr
.docker.image.inspect()
gives you apython_on_whales.Image
from astr
.
ComposeCLI
build
build(services=None, build_args={}, cache=True, progress=None, pull=False, quiet=False, ssh=None, stream_logs=False)
Build services declared in a yaml compose file.
PARAMETER | DESCRIPTION |
---|---|
services |
The services to build (as list of strings).
If
TYPE:
|
build_args |
Set build-time variables for services. For example
TYPE:
|
cache |
Set to
TYPE:
|
progress |
Set type of progress output (auto, tty, plain, quiet) (default "auto")
TYPE:
|
pull |
Set to
TYPE:
|
quiet |
Don't print anything
TYPE:
|
ssh |
Set SSH authentications used when building service images.
(use
TYPE:
|
stream_logs |
If
TYPE:
|
config
config(return_json=False)
Returns the configuration of the compose stack for further inspection.
For example
from python_on_whales import docker
project_config = docker.compose.config()
print(project_config.services["my_first_service"].image)
"redis"
PARAMETER | DESCRIPTION |
---|---|
return_json |
If
TYPE:
|
Returns
A `ComposeConfig` object if `return_json` is `False`, and a `dict` otherwise.
create
create(services=None, build=False, force_recreate=False, no_build=False, no_recreate=False, stream_logs=False)
Creates containers for a service.
PARAMETER | DESCRIPTION |
---|---|
services |
The name of the services for which the containers will
be created. The default
TYPE:
|
build |
Build images before starting containers.
TYPE:
|
force_recreate |
Recreate containers even if their configuration and image haven't changed.
TYPE:
|
no_build |
Don't build an image, even if it's missing.
TYPE:
|
no_recreate |
If containers already exist, don't recreate them.
Incompatible with
TYPE:
|
stream_logs |
If
TYPE:
|
down
down(services=None, remove_orphans=False, remove_images=None, timeout=None, volumes=False, quiet=False, stream_logs=False)
Stops and removes the containers
PARAMETER | DESCRIPTION |
---|---|
services |
The services to stop. If
TYPE:
|
remove_orphans |
Remove containers for services not defined in the Compose file.
TYPE:
|
remove_images |
Remove images used by services.
TYPE:
|
timeout |
Specify a shutdown timeout in seconds (default 10).
TYPE:
|
volumes |
Remove named volumes declared in the volumes section of the Compose file and anonymous volumes attached to containers.
TYPE:
|
quiet |
If
TYPE:
|
execute
execute(service, command, detach=False, envs={}, index=1, tty=True, privileged=False, user=None, workdir=None)
Execute a command in a running container.
PARAMETER | DESCRIPTION |
---|---|
service |
The name of the service.
TYPE:
|
command |
The command to execute.
TYPE:
|
detach |
If
TYPE:
|
envs |
A dictionary of environment variables to set in the container.
TYPE:
|
index |
The index of the container to execute the command in (default 1) if there are multiple containers for this service.
TYPE:
|
tty |
If
TYPE:
|
privileged |
If
TYPE:
|
user |
The username to use inside the container.
TYPE:
|
workdir |
The working directory inside the container.
TYPE:
|
is_installed
is_installed()
Returns True
if docker compose (the one written in Go)
is installed and working.
kill
kill(services=None, signal=None)
Kills the container(s) of a service
PARAMETER | DESCRIPTION |
---|---|
services |
One or more service(s) to kill. The default (
TYPE:
|
signal |
the signal to send to the container. Default is
TYPE:
|
logs
logs(services=[], tail=None, follow=False, no_log_prefix=False, timestamps=False, since=None, until=None, stream=False)
View output from containers
PARAMETER | DESCRIPTION |
---|---|
services |
One or more service(s) to view
TYPE:
|
tail |
Number of lines to show from the end of the logs for each container. (default "all")
TYPE:
|
follow |
Follow log output WARNING: With this
option,
TYPE:
|
no_log_prefix |
Don't print prefix in logs
TYPE:
|
timestamps |
Show timestamps
TYPE:
|
since |
Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
TYPE:
|
until |
Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
TYPE:
|
stream |
Similar to the
TYPE:
|
Returns
`str` if `stream=False` (the default), `Iterable[Tuple[str, bytes]]`
if `stream=True`.
ls
ls(all=False, filters={})
Returns a list of docker compose projects
PARAMETER | DESCRIPTION |
---|---|
all |
Results include all stopped compose projects.
TYPE:
|
filters |
Filter results based on conditions provided.
TYPE:
|
Returns
A `List[python_on_whales.ComposeProject]`
pause
pause(services=None)
Pause one or more services
PARAMETER | DESCRIPTION |
---|---|
services |
TYPE:
|
port
port(service, private_port, index=1, protocol='tcp')
Returns the public port for a port binding.
PARAMETER | DESCRIPTION |
---|---|
service |
The name of the service.
TYPE:
|
private_port |
The private port.
TYPE:
|
index |
Index of the container if service has multiple replicas (default 1)
TYPE:
|
protocol |
tcp or udp (default "tcp").
TYPE:
|
Returns
tuple with (host, port). If port is unknown, then host and port are None.
ps
ps(services=None, all=False)
Returns the containers that were created by the current project.
Returns
A `List[python_on_whales.Container]`
pull
pull(services=None, ignore_pull_failures=False, include_deps=False, quiet=False, stream_logs=False)
Pull service images
PARAMETER | DESCRIPTION |
---|---|
services |
The list of services to select. Only the images of those
services will be pulled. If no services are specified (
TYPE:
|
ignore_pull_failures |
Pull what it can and ignores images with pull failures
TYPE:
|
include_deps |
Also pull services declared as dependencies
TYPE:
|
quiet |
By default, the progress bars are printed in stdout and stderr (both).
To disable all output, use
TYPE:
|
stream_logs |
If
TYPE:
|
push
push(services=None)
Push service images
PARAMETER | DESCRIPTION |
---|---|
services |
The list of services to select. Only the images of those
services will be pushed. If no services are specified (
TYPE:
|
restart
restart(services=None, timeout=None)
Restart containers
PARAMETER | DESCRIPTION |
---|---|
services |
The names of one or more services to restart (str or list of str).
If the argument is not specified,
TYPE:
|
timeout |
The shutdown timeout (
TYPE:
|
rm
rm(services=None, stop=False, volumes=False)
Removes stopped service containers
By default, anonymous volumes attached to containers will not be removed. You
can override this with volumes=True
.
Any data which is not in a volume will be lost.
PARAMETER | DESCRIPTION |
---|---|
services |
The names of one or more services to remove (str or list of str).
If
TYPE:
|
stop |
Stop the containers, if required, before removing
TYPE:
|
volumes |
Remove any anonymous volumes attached to containers
TYPE:
|
run
run(service, command=[], build=False, detach=False, labels={}, name=None, tty=True, stream=False, dependencies=True, publish=[], remove=False, service_ports=False, use_aliases=False, user=None, workdir=None)
Run a one-off command on a service.
PARAMETER | DESCRIPTION |
---|---|
service |
The name of the service.
TYPE:
|
command |
The command to execute.
TYPE:
|
detach |
if
TYPE:
|
labels |
Add or override labels
TYPE:
|
name |
Assign a name to the container.
TYPE:
|
dependencies |
Also start linked services.
TYPE:
|
publish |
Publish a container's port(s) to the host.
TYPE:
|
service_ports |
Enable service's ports and map them to the host.
TYPE:
|
remove |
Automatically remove the container when it exits.
TYPE:
|
use_aliases |
Use the service's network aliases in the connected network(s).
TYPE:
|
tty |
Allocate a pseudo-TTY. Allow the process to access your terminal to write on it.
TYPE:
|
stream |
Similar to
TYPE:
|
user |
Username or UID, format:
TYPE:
|
workdir |
Working directory inside the container
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[str, Container, Iterable[Tuple[str, bytes]]]
|
Optional[str] |
start
start(services=None, stream_logs=False)
Start the specified services.
PARAMETER | DESCRIPTION |
---|---|
services |
The names of one or more services to start.
If
TYPE:
|
stream_logs |
If
TYPE:
|
stop
stop(services=None, timeout=None, stream_logs=False)
Stop services
PARAMETER | DESCRIPTION |
---|---|
services |
The names of one or more services to stop (str or list of str).
If
TYPE:
|
timeout |
Number of seconds or timedelta (will be converted to seconds). Specify a shutdown timeout. Default is 10s.
TYPE:
|
stream_logs |
If
TYPE:
|
top
top()
Not yet implemented
unpause
unpause(services=None)
Unpause one or more services
PARAMETER | DESCRIPTION |
---|---|
services |
One or more service to unpause.
If
TYPE:
|
up
up(services=None, build=False, detach=False, abort_on_container_exit=False, scales={}, attach_dependencies=False, force_recreate=False, recreate=True, no_build=False, remove_orphans=False, renew_anon_volumes=False, color=True, log_prefix=True, start=True, quiet=False, wait=False, no_attach_services=None, pull=None, stream_logs=False, wait_timeout=None)
Start the containers.
Reading the logs of the containers is not yet implemented.
PARAMETER | DESCRIPTION |
---|---|
services |
The services to start. If
TYPE:
|
build |
If
TYPE:
|
detach |
If
TYPE:
|
abort_on_container_exit |
If
TYPE:
|
scales |
Scale SERVICE to NUM instances. Overrides
the scale setting in the Compose file if present. For example:
TYPE:
|
attach_dependencies |
Attach to dependent containers.
TYPE:
|
force_recreate |
Recreate containers even if their configuration and image haven't changed.
TYPE:
|
recreate |
Recreate the containers if already exist.
TYPE:
|
no_build |
Don't build an image, even if it's missing.
TYPE:
|
remove_orphans |
Remove containers for services not defined in the Compose file.
TYPE:
|
renew_anon_volumes |
Recreate anonymous volumes instead of retrieving data from the previous containers.
TYPE:
|
color |
If
TYPE:
|
log_prefix |
If
TYPE:
|
start |
Start the service after creating them.
TYPE:
|
quiet |
By default, some progress bars and logs are sent to stderr and stdout.
Set
TYPE:
|
wait |
Wait for services to be running|healthy. Implies detached mode.
TYPE:
|
no_attach_services |
The services not to attach to.
TYPE:
|
pull |
Pull image before running (“always”|”missing”|”never”).
TYPE:
|
stream_logs |
If
TYPE:
|
wait_timeout |
Maximum duration to wait for the project to be running|healthy
TYPE:
|
version
version()
Returns the version of docker compose as a str
.