Skip to content

Commit a46cdb4

Browse files
authored
Compose freshness: startup order, profiles, lifecycle hooks, project name (docker#22949)
<!--Delete sections as needed --> ## Description Freshness to a few how-tos pages. also fixes and closes docker#21417 ## Related issues or tickets <!-- Related issues, pull requests, or Jira tickets --> ## Reviews <!-- Notes for reviewers here --> <!-- List applicable reviews (optionally @tag reviewers) --> - [ ] Technical review - [ ] Editorial review - [ ] Product review
1 parent 75160e4 commit a46cdb4

File tree

4 files changed

+22
-68
lines changed

4 files changed

+22
-68
lines changed

content/manuals/compose/how-tos/lifecycle.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
title: Using lifecycle hooks with Compose
33
linkTitle: Use lifecycle hooks
44
weight: 20
5-
desription: How to use lifecycle hooks with Docker Compose
6-
keywords: cli, compose, lifecycle, hooks reference
5+
description: Learn how to use Docker Compose lifecycle hooks like post_start and pre_stop to customize container behavior.
6+
keywords: docker compose lifecycle hooks, post_start, pre_stop, docker compose entrypoint, docker container stop hooks, compose hook commands
77
---
88

99
{{< summary-bar feature_name="Compose lifecycle hooks" >}}

content/manuals/compose/how-tos/profiles.md

Lines changed: 14 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ If you want to enable all profiles at the same time, you can run `docker compose
8585

8686
## Auto-starting profiles and dependency resolution
8787

88+
When you explicitly target a service on the command line that has one or more profiles assigned, you do not need to enable the profile manually as Compose runs that service regardless of whether its profile is activated. This is useful for running one-off services or debugging tools.
89+
90+
Only the targeted service (and any of its declared dependencies via `depends_on`) is started. Other services that share the same profile will not be started unless:
91+
- They are also explicitly targeted, or
92+
- The profile is explicitly enabled using `--profile` or `COMPOSE_PROFILES`.
93+
8894
When a service with assigned `profiles` is explicitly targeted on the command
8995
line its profiles are started automatically so you don't need to start them
9096
manually. This can be used for one-off services and debugging tools.
@@ -108,72 +114,19 @@ services:
108114
```
109115

110116
```sh
111-
# Only start backend and db
117+
# Only start backend and db (no profiles involved)
112118
$ docker compose up -d
113119
114-
# This runs db-migrations (and, if necessary, start db)
115-
# by implicitly enabling the profiles "tools"
120+
# Run the db-migrations service without manually enabling the 'tools' profile
116121
$ docker compose run db-migrations
117122
```
118123

119-
But keep in mind that `docker compose` only automatically starts the
120-
profiles of the services on the command line and not of any dependencies.
121-
122-
This means that any other services the targeted service `depends_on` should either:
123-
- Share a common profile
124-
- Always be started, by omitting `profiles` or having a matching profile started explicitly
125-
126-
```yaml
127-
services:
128-
web:
129-
image: web
130-
131-
mock-backend:
132-
image: backend
133-
profiles: ["dev"]
134-
depends_on:
135-
- db
136-
137-
db:
138-
image: mysql
139-
profiles: ["dev"]
140-
141-
phpmyadmin:
142-
image: phpmyadmin
143-
profiles: ["debug"]
144-
depends_on:
145-
- db
146-
```
147-
148-
```sh
149-
# Only start "web"
150-
$ docker compose up -d
151-
152-
# Start mock-backend (and, if necessary, db)
153-
# by implicitly enabling profiles "dev"
154-
$ docker compose up -d mock-backend
155-
156-
# This fails because profiles "dev" is not enabled
157-
$ docker compose up phpmyadmin
158-
```
159-
160-
Although targeting `phpmyadmin` automatically starts the profiles `debug`, it doesn't automatically start the profiles required by `db` which is `dev`.
124+
In this example, `db-migrations` runs even though it is assigned to the tools profile, because it was explicitly targeted. The `db` service is also started automatically because it is listed in `depends_on`.
161125

162-
To fix this you either have to add the `debug` profile to the `db` service:
163-
164-
```yaml
165-
db:
166-
image: mysql
167-
profiles: ["debug", "dev"]
168-
```
169-
170-
or start the `dev` profile explicitly:
171-
172-
```console
173-
# Profiles "debug" is started automatically by targeting phpmyadmin
174-
$ docker compose --profile dev up phpmyadmin
175-
$ COMPOSE_PROFILES=dev docker compose up phpmyadmin
176-
```
126+
If the targeted service has dependencies that are also gated behind a profile, you must ensure those dependencies are either:
127+
- In the same profile
128+
- Started separately
129+
- Not assigned to any profile so are always enabled
177130

178131
## Stop application and services with specific profiles
179132

@@ -208,6 +161,7 @@ services:
208161
```
209162

210163
if you only want to stop the `phpmyadmin` service, you can run
164+
211165
```console
212166
$ docker compose down phpmyadmin
213167
```

content/manuals/compose/how-tos/project-name.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
---
22
title: Specify a project name
33
weight: 10
4-
description: Understand the different ways you can set a project name in Compose and what the precedence is.
4+
description: Learn how to set a custom project name in Compose and understand the precedence of each method.
55
keywords: name, compose, project, -p flag, name top-level element
66
aliases:
77
- /compose/project-name/
88
---
99

10-
In Compose, the default project name is derived from the base name of the project directory. However, you have the flexibility to set a custom project name.
10+
By default, Compose assigns the project name based on the name of the directory that contains the Compose file. You can override this with several methods.
1111

1212
This page offers examples of scenarios where custom project names can be helpful, outlines the various methods to set a project name, and provides the order of precedence for each approach.
1313

1414
> [!NOTE]
1515
>
1616
> The default project directory is the base directory of the Compose file. A custom value can also be set
17-
> for it using the [`--project-directory` command line option](/reference/cli/docker/compose.md#use--p-to-specify-a-project-name).
17+
> for it using the [`--project-directory` command line option](/reference/cli/docker/compose.md#options).
1818
1919
## Example use cases
2020

content/manuals/compose/how-tos/startup-order.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
description: How to control service startup and shutdown order in Docker Compose
3-
keywords: documentation, docs, docker, compose, startup, shutdown, order
2+
description: Learn how to manage service startup and shutdown order in Docker Compose using depends_on and healthchecks.
3+
keywords: docker compose startup order, compose shutdown order, depends_on, service healthcheck, control service dependencies
44
title: Control startup and shutdown order in Compose
55
linkTitle: Control startup order
66
weight: 30
@@ -13,7 +13,7 @@ You can control the order of service startup and shutdown with the
1313
containers in dependency order, where dependencies are determined by
1414
`depends_on`, `links`, `volumes_from`, and `network_mode: "service:..."`.
1515

16-
A good example of when you might use this is an application which needs to access a database. If both services are started with `docker compose up`, there is a chance this will fail since the application service might start before the database service and won't find a database able to handle its SQL statements.
16+
For example, if your application needs to access a database and both services are started with `docker compose up`, there is a chance this will fail since the application service might start before the database service and won't find a database able to handle its SQL statements.
1717

1818
## Control startup
1919

0 commit comments

Comments
 (0)