|
| 1 | +--- |
| 2 | +title: Defining and Running Multi-Container Applications with Docker Compose |
| 3 | +summary: Simplify the process of defining, configuring, and running multi-container Docker applications to enable efficient development, testing, and deployment. |
| 4 | +description: Learn how to use Docker Compose to define and run multi-container Docker applications. |
| 5 | +params: |
| 6 | + image: images/learning-paths/compose.png |
| 7 | + skill: Beginner |
| 8 | + time: 5 minutes |
| 9 | + prereq: None |
| 10 | +--- |
| 11 | + |
| 12 | +{{< columns >}} |
| 13 | + |
| 14 | +Developers face challenges with multi-container Docker applications, including |
| 15 | +complex configuration, dependency management, and maintaining consistent |
| 16 | +environments. Networking, resource allocation, data persistence, logging, and |
| 17 | +monitoring add to the difficulty. Security concerns and troubleshooting issues |
| 18 | +further complicate the process, requiring effective tools and practices for |
| 19 | +efficient management. |
| 20 | + |
| 21 | +Docker Compose solves the problem of managing multi-container Docker |
| 22 | +applications by providing a simple way to define, configure, and run all the |
| 23 | +containers needed for an application using a single YAML file. This approach |
| 24 | +helps developers to easily set up, share, and maintain consistent development, |
| 25 | +testing, and production environments, ensuring that complex applications can be |
| 26 | +deployed with all their dependencies and services properly configured and |
| 27 | +orchestrated. |
| 28 | + |
| 29 | +<!-- break --> |
| 30 | + |
| 31 | +## Who’s this for? |
| 32 | + |
| 33 | +- Developers and DevOps engineers who need to define, manage, and orchestrate |
| 34 | + multi-container Docker applications efficiently across multiple environments. |
| 35 | +- Development teams that want to increase productivity by streamlining |
| 36 | + development workflows and reducing setup time. |
| 37 | + |
| 38 | +## Tools integration |
| 39 | + |
| 40 | +Works well with Docker CLI, CI/CD tools, and container orchestration tools. |
| 41 | + |
| 42 | +{{< /columns >}} |
| 43 | + |
| 44 | +## Modules |
| 45 | + |
| 46 | +{{< accordion large=true title=`Why Docker Compose?` icon=`play_circle` >}} |
| 47 | + |
| 48 | +Docker Compose is an essential tool for defining and running multi-container |
| 49 | +Docker applications. Docker Compose simplifies the Docker experience, making it |
| 50 | +easier for developers to create, manage, and deploy applications by using YAML |
| 51 | +files to configure application services. |
| 52 | + |
| 53 | +Docker Compose provides several benefits: |
| 54 | + |
| 55 | +- Lets you define multi-container applications in a single YAML file. |
| 56 | +- Ensures consistent environments across development, testing, and production. |
| 57 | +- Manages the startup and linking of multiple containers effortlessly. |
| 58 | +- Streamlines development workflows and reduces setup time. |
| 59 | +- Ensures that each service runs in its own container, avoiding conflicts. |
| 60 | + |
| 61 | +{{< youtube-embed 2EqarOM2V4U >}} |
| 62 | + |
| 63 | +{{< /accordion >}} |
| 64 | + |
| 65 | +{{< accordion large=true title=`Demo: Set up and use Docker Compose` icon=`play_circle` >}} |
| 66 | + |
| 67 | +{{< youtube-embed P5RBKmOLPH4 >}} |
| 68 | + |
| 69 | +{{< /accordion >}} |
| 70 | + |
| 71 | +{{< accordion large=true title=`Common challenges and questions` icon=`quiz` >}} |
| 72 | + |
| 73 | +<!-- vale Docker.HeadingLength = NO --> |
| 74 | + |
| 75 | +### Do I need to maintain a separate Compose file for my development, testing, and staging environments? |
| 76 | + |
| 77 | +You don't necessarily need to maintain entirely separate Compose files for your |
| 78 | +development, testing, and staging environments. You can define all your |
| 79 | +services in a single Compose file (`compose.yml`). You can use profiles to |
| 80 | +group service configurations specific to each environment (`dev`, `test`, |
| 81 | +`staging`). |
| 82 | + |
| 83 | +When you need to spin up an environment, you can activate the corresponding |
| 84 | +profiles. For example, to set up the development environment: |
| 85 | + |
| 86 | +```console |
| 87 | +$ docker compose --profile dev up |
| 88 | +``` |
| 89 | + |
| 90 | +This command starts only the services associated with the `dev` profile, |
| 91 | +leaving the rest inactive. |
| 92 | + |
| 93 | +For more information on using profiles, see [Using profiles with |
| 94 | +Compose](/compose/profiles/). |
| 95 | + |
| 96 | +### How can I enforce the database service to start up before the frontend service? |
| 97 | + |
| 98 | +Docker Compose ensures services start in a specific order by using the |
| 99 | +`depends_on` property. This tells Compose to start the database service before |
| 100 | +even attempting to launch the frontend service. This is crucial since |
| 101 | +applications often rely on databases being ready for connections. |
| 102 | + |
| 103 | +However, `depends_on` only guarantees the order, not that the database is fully |
| 104 | +initialized. For a more robust approach, especially if your application relies |
| 105 | +on a prepared database (e.g., after migrations), consider [health |
| 106 | +checks](/reference/compose-file/services.md#healthcheck). Here, you can |
| 107 | +configure the frontend to wait until the database passes its health check |
| 108 | +before starting. This ensures the database is not only up but also ready to |
| 109 | +handle requests. |
| 110 | + |
| 111 | +For more information on setting the startup order of your services, see |
| 112 | +[Control startup and shutdown order in Compose](/compose/startup-order/). |
| 113 | + |
| 114 | +### Can I use Compose to build a Docker image? |
| 115 | + |
| 116 | +Yes, you can use Docker Compose to build Docker images. Docker Compose is a |
| 117 | +tool for defining and running multi-container applications. Even if your |
| 118 | +application isn't a multi-container application, Docker Compose can make it |
| 119 | +easier to run by defining all the `docker run` options in a file. |
| 120 | + |
| 121 | +To use Compose, you need a `compose.yml` file. In this file, you can specify |
| 122 | +the build context and Dockerfile for each service. When you run the command |
| 123 | +`docker compose up --build`, Docker Compose will build the images for each |
| 124 | +service and then start the containers. |
| 125 | + |
| 126 | +For more information on building Docker images using Compose, see the [Compose |
| 127 | +Build Specification](/compose/compose-file/build/). |
| 128 | + |
| 129 | +### What is the difference between Docker Compose and Dockerfile? |
| 130 | + |
| 131 | +A Dockerfile provides instructions to build a container image while a Compose |
| 132 | +file defines your running containers. Quite often, a Compose file references a |
| 133 | +Dockerfile to build an image to use for a particular service. |
| 134 | + |
| 135 | +### What is the difference between the `docker compose up` and `docker compose run` commands? |
| 136 | + |
| 137 | +The `docker compose up` command creates and starts all your services. It's |
| 138 | +perfect for launching your development environment or running the entire |
| 139 | +application. The `docker compose run` command focuses on individual services. |
| 140 | +It starts a specified service along with its dependencies, allowing you to run |
| 141 | +tests or perform one-off tasks within that container. |
| 142 | + |
| 143 | +<!-- vale Docker.HeadingLength = YES --> |
| 144 | + |
| 145 | +{{< /accordion >}} |
| 146 | + |
| 147 | +{{< accordion large=true title=`Resources` icon=`link` >}} |
| 148 | + |
| 149 | +- [Overview of Docker Compose CLI](/compose/reference/) |
| 150 | +- [Overview of Docker Compose](/compose/) |
| 151 | +- [How Compose works](/compose/compose-application-model/) |
| 152 | +- [Using profiles with Compose](/compose/profiles/) |
| 153 | +- [Control startup and shutdown order with Compose](/compose/startup-order/) |
| 154 | +- [Compose Build Specification](/compose/compose-file/build/) |
| 155 | + |
| 156 | +{{< /accordion >}} |
0 commit comments