15 Tips & Tricks to Improve Your Docker Compose Experience (DockerCon 2023)
Docker Compose is a powerful tool for managing multi-container Docker applications. It allows you to define your application’s services, networks, and volumes in a single YAML file, making it easy to spin up and manage your application’s environment. In this article, we’ll explore 15 tricks to improve your Docker Compose experience. Original DockerCon 2023 presentation
- Docker Compose Config: The
docker-compose config
command displays the fully rendered and templated version of your project in YAML or JSON. This is useful when reproducing bugs, integrating with other systems, or checking if your Compose file is valid. - Dry Run: The
docker compose up --dry-run
flag is a recent addition to Docker Compose that allows you to see what would happen when running a command without actually executing it. This is helpful for debugging and ensuring that your services are set up correctly. - Build: By default, Docker Compose won’t rebuild images if they already exist. However, you can use the
docker compose --build
flag to force a rebuild of your services. Additionally, you can use thebuild:
policy to always build a specific service, even without the--build
flag pull_policy: build
is a Docker Compose option that allows you to specify the policy for pulling images. When set tobuild
, Docker Compose will always build the image for a specific service, even if an image with the same tag already exists. This can be useful in situations where you want to ensure that you are always building the latest version of an image, even if it has not changed since the last build.- Build > SSH: You can pass SSH keys or use your SSH agent with Docker Compose to securely access private dependencies during the build process. This is particularly useful in a business setting where you may need to access private repositories. To use SSH with Docker Compose builds, you can add the
ssh
option to your service’sbuild
section and specify the path to your SSH private key or use your SSH agent. - Build > dockerfile_inline: Instead of having a separate Dockerfile, you can write a Dockerfile inline within your Compose file. This can be helpful for simple use cases or when programmatically generating the Compose file. To use inline Dockerfiles, you can specify the
dockerfile
option in your service’sbuild
section and provide the Dockerfile content as a string. - depends_on > condition: The
depends_on
option in Docker Compose allows you to specify the order in which services should be started. However, it does not guarantee that a service is fully functional before starting its dependencies. To ensure that a service is fully functional, you can use thecondition
option withdepends_on
. For example, you can wait for a database service to be healthy before starting a web service that depends on it. - depends_on > restart: You can also use the
restart
option withdepends_on
to ensure that a service is restarted if one of its dependencies restarts. For example, if you have a web service that depends on a database service, you can use therestart
option to ensure that the web service is restarted if the database service restarts. - depends_on > required: By default, Docker Compose will not fail if a service that another service depends on fails to start. However, you can use the
required
option withdepends_on
to ensure that a service is started only if all of its dependencies are started successfully. - YAML Anchors: YAML anchors allow you to reuse configuration across multiple services. This can be helpful for reducing duplication in your Compose file. For example, you can define a common environment variable or volume and reuse it across multiple services.
- !reset: The
!reset
flag in Docker Compose allows you to reset a service’s configuration to its default values. This can be useful when you want to override a specific configuration option without affecting other options. docker compose alpha viz
is a command-line tool that allows you to visualize your Docker Compose project as a graph. This can be helpful for understanding how your services are connected and for debugging complex issues.- COMPOSE_EXPERIMENTAL_OTEL: The
COMPOSE_EXPERIMENTAL_OTEL
environment variable enables experimental OpenTelemetry tracing in Docker Compose. This can be helpful for monitoring your application’s performance and for debugging issues. - include: The include option in Docker Compose allows you to include multiple Compose files in a single project. This can be helpful for organizing your project’s configuration and for reusing common configuration across multiple projects.
develop
: Thedevelop
option in Docker Compose allows you to automatically rebuild and restart services. If you are curious about “developing inside container”, here is an article about it.