# Defining and running multi-container applications with Docker Compose



Developers face challenges with multi-container Docker applications, including
complex configuration, dependency management, and maintaining consistent
environments. Networking, resource allocation, data persistence, logging, and
monitoring add to the difficulty. Security concerns and troubleshooting issues
further complicate the process, requiring effective tools and practices for
efficient management.

Docker Compose solves the problem of managing multi-container Docker
applications by providing a simple way to define, configure, and run all the
containers needed for an application using a single YAML file. This approach
helps developers to easily set up, share, and maintain consistent development,
testing, and production environments, ensuring that complex applications can be
deployed with all their dependencies and services properly configured and
orchestrated.

## What you’ll learn

- What Docker Compose is and what it does
- How to define services
- Use cases for Docker Compose
- How things would be different without Docker Compose

## Who’s this for?

- Developers and DevOps engineers who need to define, manage, and orchestrate
  multi-container Docker applications efficiently across multiple environments.
- Development teams that want to increase productivity by streamlining
  development workflows and reducing setup time.

## Tools integration

Works well with Docker CLI, CI/CD tools, and container orchestration tools.

<div id="compose-lp-survey-anchor"></div>

## Why Docker Compose?

Docker Compose is an essential tool for defining and running multi-container
Docker applications. Docker Compose simplifies the Docker experience, making it
easier for developers to create, manage, and deploy applications by using YAML
files to configure application services.

Docker Compose provides several benefits:

- Lets you define multi-container applications in a single YAML file.
- Ensures consistent environments across development, testing, and production.
- Manages the startup and linking of multiple containers effortlessly.
- Streamlines development workflows and reduces setup time.
- Ensures that each service runs in its own container, avoiding conflicts.



<div id="compose-lp-survey-anchor"></div>

## Demo: set up and use Docker Compose

This Docker Compose demo shows how to orchestrate a multi-container application
environment, streamlining development and deployment processes.

- Compare Docker Compose to the `docker run` command
- Configure a multi-container web app using a Compose file
- Run a multi-container web app using one command



<div id="compose-lp-survey-anchor"></div>

## Common challenges and questions

<!-- vale Docker.HeadingLength = NO -->

#### Do I need to maintain a separate Compose file for my development, testing, and staging environments?

You don't necessarily need to maintain entirely separate Compose files for your
development, testing, and staging environments. You can define all your
services in a single Compose file (`compose.yaml`). You can use profiles to
group service configurations specific to each environment (`dev`, `test`,
`staging`).

When you need to spin up an environment, you can activate the corresponding
profiles. For example, to set up the development environment:

```console
$ docker compose --profile dev up
```

This command starts only the services associated with the `dev` profile,
leaving the rest inactive.

For more information on using profiles, see [Using profiles with
Compose](/compose/how-tos/profiles/).

#### How can I enforce the database service to start up before the frontend service?

Docker Compose ensures services start in a specific order by using the
`depends_on` property. This tells Compose to start the database service before
even attempting to launch the frontend service. This is crucial since
applications often rely on databases being ready for connections.

However, `depends_on` only guarantees the order, not that the database is fully
initialized. For a more robust approach, especially if your application relies
on a prepared database (e.g., after migrations), consider [health
checks](/reference/compose-file/services/#healthcheck). Here, you can
configure the frontend to wait until the database passes its health check
before starting. This ensures the database is not only up but also ready to
handle requests.

For more information on setting the startup order of your services, see
[Control startup and shutdown order in Compose](/compose/how-tos/startup-order/).

#### Can I use Compose to build a Docker image?

Yes, you can use Docker Compose to build Docker images. Docker Compose is a
tool for defining and running multi-container applications. Even if your
application isn't a multi-container application, Docker Compose can make it
easier to run by defining all the `docker run` options in a file.

To use Compose, you need a `compose.yaml` file. In this file, you can specify
the build context and Dockerfile for each service. When you run the command
`docker compose up --build`, Docker Compose will build the images for each
service and then start the containers.

For more information on building Docker images using Compose, see the [Compose
Build Specification](/compose/compose-file/build/).

#### What is the difference between Docker Compose and Dockerfile?

A Dockerfile provides instructions to build a container image while a Compose
file defines your running containers. Quite often, a Compose file references a
Dockerfile to build an image to use for a particular service.

#### What is the difference between the `docker compose up` and `docker compose run` commands?

The `docker compose up` command creates and starts all your services. It's
perfect for launching your development environment or running the entire
application. The `docker compose run` command focuses on individual services.
It starts a specified service along with its dependencies, allowing you to run
tests or perform one-off tasks within that container.

<div id="compose-lp-survey-anchor"></div>

