Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions content/manuals/compose/how-tos/lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ desription: How to use lifecycle hooks with Docker Compose
keywords: cli, compose, lifecycle, hooks reference
---

{{< introduced compose 2.30.0 "../releases/release-notes.md#2300" >}}

## Services lifecycle hooks

When Docker Compose runs a container, it uses two elements,
Expand Down Expand Up @@ -63,3 +65,8 @@ services:
pre_stop:
- command: ./data_flush.sh
```

## Reference information

- [`post_start`](/reference/compose-file/services.md#post_start)
- [`pre_stop`](/reference/compose-file/services.md#pre_stop)
33 changes: 33 additions & 0 deletions content/reference/compose-file/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,39 @@
mode: host
```

## post_start

Check failure on line 1508 in content/reference/compose-file/services.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'post_start'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'post_start'?", "location": {"path": "content/reference/compose-file/services.md", "range": {"start": {"line": 1508, "column": 4}}}, "severity": "ERROR"}

Check warning on line 1508 in content/reference/compose-file/services.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.HeadingSentenceCase] Use sentence case for headings: 'post_start'. Raw Output: {"message": "[Docker.HeadingSentenceCase] Use sentence case for headings: 'post_start'.", "location": {"path": "content/reference/compose-file/services.md", "range": {"start": {"line": 1508, "column": 4}}}, "severity": "WARNING"}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add version it was introduced in

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, added

{{< introduced compose 2.30.0 "../../manuals/compose/releases/release-notes.md#2300" >}}

`post_start` defines a sequence of lifecycle hooks to run after a container has started. The exact timing of when the command is run is not guaranteed.

- `command`: Specifies the command to run once the container starts. This attribute is required, and you can choose to use either the shell form or the exec form.
- `user`: The user to run the command. If not set, the command is run with the same user as the main service command.
- `privileged`: Lets the `post_start` command run with privileged access.
- `working_dir`: The working directory in which to run the command. If not set, it is run in the same working directory as the main service command.
- `environment`: Sets environment variables specifically for the `post_start` command. While the command inherits the environment variables defined for the service’s main command, this section lets you add new variables or override existing ones.

```yaml
services:
test:
post_start:
- command: ./do_something_on_startup.sh
user: root
privileged: true
environment:
- FOO=BAR
```

For more information, see [Use lifecycle hooks](/manuals/compose/how-tos/lifecycle.md).

## pre_stop

Check warning on line 1533 in content/reference/compose-file/services.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.HeadingSentenceCase] Use sentence case for headings: 'pre_stop'. Raw Output: {"message": "[Docker.HeadingSentenceCase] Use sentence case for headings: 'pre_stop'.", "location": {"path": "content/reference/compose-file/services.md", "range": {"start": {"line": 1533, "column": 4}}}, "severity": "WARNING"}

Check failure on line 1533 in content/reference/compose-file/services.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'pre_stop'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'pre_stop'?", "location": {"path": "content/reference/compose-file/services.md", "range": {"start": {"line": 1533, "column": 4}}}, "severity": "ERROR"}

{{< introduced compose 2.30.0 "../../manuals/compose/releases/release-notes.md#2300" >}}

`pre_stop` defines a sequence of lifecycle hooks to run before the container is stopped. These hooks won't run if the container stops by itself or is terminated suddenly.

Configuration is equivalent to [`post_start](#post_start).

### privileged

`privileged` configures the service container to run with elevated privileges. Support and actual impacts are platform specific.
Expand Down
Loading