The Ultimate Docker Compose Cheat Sheet

Facebook
Twitter
LinkedIn

Never miss a post!

Sign up for our newsletter and get FREE Development Trends delivered directly to your inbox.

You can unsubscribe any time. Terms & Conditions.
Categories

Docker is a popular container tool used by developers and operations teams to automate the creation and deployment of applications in lightweight containers on VMs. This allows applications to work in different environments.

Let’s say you have multiple containers in Docker. Consider a web server and database running in separate containers, It can be difficult to build, run, and connect containers from different Docker files. This can lead to a long time. Docker Compose is here to help. It allows multiple services to run simultaneously.

Docker Compose can be used to run multiple containers at once. The containers can run independently but can also interact with one another when necessary. It is very simple to create Docker Compose files using YAML (a scripting language that uses XML). YAML stands for Yet Another Markup Language. Docker Compose’s best feature is the ability to activate all services (containers), with one command.

This article will provide a cheat sheet on Docker Compose. It will explain all commands that you can use with Docker Compose.

 

docker-compose up

docker-compose up is used to create defined docker compose services. It can either create images from.yml files, or launch services from existing images on the docker host. docker­compose up uses docker-compose.yml to start services.

 

Command options:

 

docker­compose -f <filename.yml> -f <filename­local.yml> up uses a yml file in a custom location/name to start services

 

  • –d, –detach – It will run the services in detached/background mode
  • –build        –   Before you start services, build forces image from dockerfile
  • –no-build – Bypasses the image build
  • –force-recreate. – Force recreation of containers when starting services
  • –no-color – Outputs without colour
  • –quiet-pull – Pull information without printing it
  • –no-deps   – Don’t start linked services.
  • –always-recreate-deps –  Recreate dependent containers. Incompatible with –no-recreate.
  • –no-recreate –  If containers already exist, don’t recreate them. Incompatible with –force-recreate and –renew-anon-volumes.
  • –no-start –  Don’t start the services after creating them.
  • –abort-on-container-exit – Stops all containers if any container was stopped. Incompatible with –detach.
  • –attach-dependencies –    Attach to dependent containers.
  • -t, –timeout TIMEOUT  –  Use this timeout in seconds for container shutdown when attached or when containers are already running. (default: 10)
  • -V, –renew-anon-volumes Recreate anonymous volumes instead of retrieving data from the previous containers.
  • –remove-orphans   –   Remove containers for services not defined in the Compose file.
  • –exit-code-from SERVICE – Return the exit code of the selected service container. Implies –abort-on-container-exit.
  • –scale SERVICE=NUM –    Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.

docker-compose down

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

Command Options:

  • –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 version

 

Prints the docker-compose version.

 

docker-compose start

docker-compose start [SERVICE…]

Starts an existing service container.

 

docker-compose stop

docker-compose stop [SERVICE…]

You can stop running containers without removing them. They can be restarted with docker-composee start.

 

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 automatically allocate a TTY. For example, to get an interactive prompt, you can use docker-compose exec web sh.

 

Command options:

 

  • -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…]

 

SIGKILL signals are used to force running containers to stop. Optionally, the signal can  be passed, for example, docker-compose kill -s SIGINT

 

Command option:

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

 

Command options:

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

 

docker-compose rm

 

docker-compose rm [options] [SERVICE…]

Stopped containers get removed. All data that isn’t in a volume are lost. Anonymous volumes attached to containers by default are not deleted. This can be overridden with -v.

 

Command options:

  • -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…]

This command runs a single command against a service. The following command will start the web service and run bash with its command docker_compose run web bash.

 

Command options:

 

  • -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 ps

 

docker-compose ps [options] [SERVICE…]

Shows list of containers for a service.

 

Command options:

  • -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 config

 

docker-compose config [options]

Verify that the Compose format is correct. The configuration will be displayed if it is correct. If the format is not correct, the cause of the error is displayed.

 

Command options:

  • –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 logs

docker-compose logs [options] [SERVICE…]

Displays log output from services.

 

Command options:

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

 

Command options:

 

  • –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

 

Command options:

  • –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

 

Command options:

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

 

docker-compose build

docker-compose build [options] [SERVICE…]

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

 

Command options:

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

Final Thoughts

So that was all about docker compose cheat sheet. This is all you need to know to run docker compose commands. An application can consist of multiple containers running different services. It can be tedious to start and manage containers manually, so start using docker compose to handle multiple containers with ease.

Facebook
Twitter
LinkedIn

Our website uses cookies that help it to function, allow us to analyze how you interact with it, and help us to improve its performance. By using our website you agree by our Terms and Conditions and Privacy Policy.