Engdocs 2282 (#21326)

<!--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
This commit is contained in:
Allie Sadler 2024-11-05 09:49:09 +00:00 committed by GitHub
parent 7b1deedb3d
commit 66d0b9998e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 0 deletions

View File

@ -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,
@ -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)

View File

@ -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 services 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.