Skip to content

Commit

Permalink
Engdocs 2282 (#21326)
Browse files Browse the repository at this point in the history
<!--Delete sections as needed -->

## Description

Add lifecycle attributes to the compose spec

## Related issues or tickets

<!-- Related issues, pull requests, or Jira tickets -->

## Reviews

<!-- Notes for reviewers here -->
<!-- List applicable reviews (optionally @tag reviewers) -->

- [ ] Technical review
- [x] Editorial review
- [ ] Product review
  • Loading branch information
aevesdocker authored Nov 5, 2024
1 parent 7b1deed commit 66d0b99
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
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 @@ ports:
mode: host
```

## post_start

{{< 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

{{< 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

0 comments on commit 66d0b99

Please sign in to comment.