diff --git a/content/manuals/compose/how-tos/lifecycle.md b/content/manuals/compose/how-tos/lifecycle.md index aa332877e9e..d044f1bed13 100644 --- a/content/manuals/compose/how-tos/lifecycle.md +++ b/content/manuals/compose/how-tos/lifecycle.md @@ -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) diff --git a/content/reference/compose-file/services.md b/content/reference/compose-file/services.md index 24906a00d3d..798b3ad69e3 100644 --- a/content/reference/compose-file/services.md +++ b/content/reference/compose-file/services.md @@ -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.