A handy docker-compose cheatsheet for everyday work. Explains docker-compose commands and docker-compose examples.

Docker-compose commands cheatsheet

docker-compose up

docker-compose up [options] [--scale SERVICE=NUM...] [SERVICE...]

docker-compose up is used to start a project. It tries to automate a series of operations including building a mirror, (re)creating a service, starting a service, and associating a service-related container. Sometimes you will need docker-compose up --rebuild after making code changes.

-d, --detach  – Run containers in the background

--no-color – Produce monochrome output.

--no-deps – Don’t start linked services.

--force-recreate – Recreate containers even if their configuration and image haven’t changed.

--always-recreate-deps – Recreate dependent containers.

--no-recreate – If containers already exist, don’t recreate
them.

--no-build – Don’t build an image, even if it’s missing.

--no-start – Don’t start the services after creating them.

--build – Build images before starting containers.

-t, --timeout – TIMEOUT Use this timeout in seconds for container shutdown when attached or when containers are already running. (default: 10)

--remove-orphans – Remove containers for services not defined in the Compose file.

docker-compose down

docker-compose down [options]

Stops containers. And removes containers, networks, volumes, and images (defined in docker-compose.yml) created by up

--rmi type – Remove images. Type is ‘all’ or ‘local’

-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.

docker-compose ps

docker-compose ps [options] [SERVICE...]

Shows list of containers for a service.

-q, --quiet – Only display IDs

--services – Display services

--filter – KEY=VAL Filter services by a property

-a, --all – Show all stopped containers (including those created by the run command)

docker-compose bundle

docker-compose bundle [options]

Generates a Distributed Application Bundle (DAB) from the Compose file.

Images must have digests stored, which requires interaction with a Docker registry. If digests aren’t stored for all images, you can fetch them with docker-compose pull or docker-compose push. To push images automatically when bundling, pass –push-images. Only services with a build option specified have their images pushed.

--push-images – Automatically push images for any services which have a `build` option specified.

-o, --output – PATH Path to write the bundle file to. Defaults to “.dab”.

docker-compose config

docker-compose config [options]

Verify that the Compose file format is correct. If it is correct, the configuration is displayed. If the format is incorrect, the cause of the error is displayed.

--resolve-image-digests  – Pin image tags to digests.

-q, --quiet – Only validate the configuration, don’t print anything.

--services – Print the service names, one per line.

--volumes – Print the volume names, one per line.

--hash="*" – Print the service config hash, one per line. Set “service1,service2” for a list of specified services or use the wildcard symbol to display all services.

docker-compose events

docker-compose events [options] [SERVICE...]

Stream container events for every container in the project. Example docker-compose events --json to stream in JSON format.

--json – Output events as a stream of JSON objects

docker-compose logs

docker-compose logs [options] [SERVICE...]

Displays log output from services.

--no-color – Produce monochrome output.

-f, --follow – Follow log output.

-t, --timestamps – Show timestamps.

--tail="all" – Number of lines to show from the end of the logs for each container.

docker-compose port

docker-compose port [options] SERVICE PRIVATE_PORT

Prints the public port to which a container port is mapped.

--protocol=proto – tcp or udp [default: tcp]

--index=index – index of the container if there are multiple instances of a service [default: 1]

docker-compose pull

docker-compose pull [options] [SERVICE...]

Pulls an image associated with a service defined in a docker-compose.yml

--ignore-pull-failures – Pull what it can and ignores images with pull failures.

--parallel – Deprecated, pull multiple images in parallel (enabled by default).

--no-parallel – Disable parallel pulling.

-q, --quiet – Pull without printing progress information

--include-deps – Also pull services declared as dependencies

docker-compose push

docker-compose push [options] [SERVICE...]

Pushes images for services to their respective registry/repository

--ignore-push-failures – Push what it can and ignores images with push failures.

docker-compose version

docker-compose version

Prints the version of docker-compose.

docker-compose build

docker-compose build [options] [SERVICE...]

Under the project directory,  run docker-compose buildto build (rebuild) the service.

--compress – Compress the build context using gzip.

--force-rm – Always remove intermediate containers.

--no-cache – Do not use cache when building the image.

--pull – Always attempt to pull a newer version of the image.

-m, --memory – MEM Sets memory limit for the build container.

--build-arg key=val – Set build-time variables for services.

--parallel – Build images in parallel.

docker-compose start

docker-compose start [SERVICE...]

Starts an existing service container.

docker-compose stop

docker-compose up [options] [--scale SERVICE=NUM...] [SERVICE...]

Stops running containers without removing them. They can be started again with docker-compose start.

-d, --detach  – Run containers in the background

--no-color – Produce monochrome output.

--no-deps – Don’t start linked services.

--force-recreate – Recreate containers even if their configuration and image haven’t changed.

--always-recreate-deps – Recreate dependent containers.

--no-recreate – If containers already exist, don’t recreate
them.

--no-build – Don’t build an image, even if it’s missing.

--no-start – Don’t start the services after creating them.

--build – Build images before starting containers.

-t, --timeout – TIMEOUT Use this timeout in seconds for container shutdown when attached or when containers are already running. (default: 10)

--remove-orphans – Remove containers for services not defined in the Compose file.

docker-compose pause

docker-compose pause [SERVICE...]

Pauses running containers of a service. They can be unpaused with docker-compose unpause

docker-compose unpause

docker-compose unpause [SERVICE...]

Unpauses paused containers of a service.

docker-compose exec

docker-compose exec [options] [-e KEY=VAL...] SERVICE COMMAND [ARGS...]

This is similar to docker exec. docker-compose commands by default allocate a TTY. Example, use docker-compose exec web sh to get an interactive prompt.

-d, --detach – Detached mode: Run command in the background.

--privileged – Give extended privileges to the process.

-u, --user – USER Run the command as this user.

-T – Disable pseudo-tty allocation. By default `docker-compose exec` allocates a TTY.

--index=index – index of the container if there are multiple instances of service [default: 1]

-e, --env – KEY=VAL Set environment variables (can be used multiple times, not supported in API < 1.25)

-w, --workdir – DIR Path to workdir directory for this command.

docker-compose help

docker-compose help COMMAND

Displays help and usage instructions for a command.

docker-compose kill

docker-compose kill [options] [SERVICE...]

Forces running containers to stop by sending a SIGKILL signal. Optionally the signal can be passed, for example: docker-compose kill -s SIGINT

-s SIGNAL – SIGNAL to send to the container. The default signal is SIGKILL.

docker-compose restart

docker-compose restart [options] [SERVICE...]

Restarts all stopped and running services.

-t, --timeout – TIMEOUT Specify a shutdown timeout in seconds. (default: 10)

docker-compose rm

docker-compose rm [options] [SERVICE...]

Removes stopped service containers. Any data which is not in a volume is lost. By default, anonymous volumes attached to containers are not removed. You can override this with -v.

-f, --force – Don’t ask to confirm the removal

-s, --stop – Stop the containers, if required, before removing

-v – Remove any anonymous volumes attached to containers

docker-compose top

docker-compose top [SERVICE...]

View the processes running within each service container.

docker-compose run

docker-compose run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] [-l KEY=VALUE...] SERVICE [COMMAND] [ARGS...]

Runs a one-time command against a service. For example, the following command starts the web service and runs bash as its command docker-compose run web bash.

-d, --detach – Detached mode: Run container in the background, print new container name.

--name NAME – Assign a name to the container

--entrypoint CMD – Override the entrypoint of the image.

-e KEY=VAL – Set an environment variable (can be used multiple times)

-l, --label KEY=VAL – Add or override a label (can be used multiple times)

-u, --user="" – Run as specified username or uid

--no-deps – Don’t start linked services.

--rm – Remove container after run. Ignored in detached mode.

-p, --publish=[] – Publish a container’s port(s) to the host

--service-ports – Run command with the service’s ports enabled and mapped to the host.

--use-aliases – Use the service’s network aliases in the network(s) the container connects to.

-v, --volume=[] – Bind mount a volume (default [])

-T – Disable pseudo-tty allocation. By default docker-compose run allocates a TTY.

-w, --workdir="" – Working directory inside the container

Docker-compose examples

These are some sample examples.

docker-compose.yml example

Copy

Building – from Dockerfile or image

Copy

Ports binding

Copy

Command and Entrypoint

Copy

Volume – Storage mapping

Copy

Environment variables

Copy

Dependencies – dependent services

Copy

Labels

Copy

DNS Servers

Copy

Check out the article on Docker compose to get a good understanding of Docker-compose in depth. I would love to hear your thoughts, there are of course few more examples related to networking could have been added into the example section. But if you have read the networking section before, things should be simple to figure out for docker-compose.