-
Notifications
You must be signed in to change notification settings - Fork 8.1k
lifecycle hooks #21061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
lifecycle hooks #21061
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| --- | ||
| title: Using lifecycle hooks with Compose | ||
| linkTitle: Use lifecycle hooks | ||
| weight: 20 | ||
| desription: How to use lifecycle hooks with Docker Compose | ||
| keywords: cli, compose, lifecycle, hooks reference | ||
| --- | ||
|
|
||
| ## Services lifecycle hooks | ||
|
|
||
| When Docker Compose runs a container, it uses two elements, | ||
| [ENTRYPOINT and COMMAND](https://github.com/manuals//engine/containers/run.md#default-command-and-options), | ||
| to manage what happens when the container starts and stops. | ||
|
|
||
| However, it can sometimes be easier to handle these tasks separately with lifecycle hooks - | ||
| commands that run right after the container starts or just before it stops. | ||
|
|
||
| Lifecycle hooks are particularly useful because they can have special privileges | ||
| (like running as the root user), even when the container itself runs with lower privileges | ||
| for security. This means that certain tasks requiring higher permissions can be done without | ||
| compromising the overall security of the container. | ||
|
|
||
ndeloof marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ### Post-start hooks | ||
|
|
||
| Post-start hooks are commands that run after the container has started, but there's no | ||
| set time for when exactly they will execute. The hook execution timing is not assured during | ||
| the execution of the container's `entrypoint`. | ||
|
|
||
| In the example provided: | ||
|
|
||
| - The hook is used to change the ownership of a volume to a non-root user (because volumes | ||
| are created with root ownership by default). | ||
| - After the container starts, the `chown` command changes the ownership of the `/data` directory to user `1001`. | ||
|
|
||
| ```yaml | ||
| services: | ||
| app: | ||
| image: backend | ||
| user: 1001 | ||
| volumes: | ||
| - data:/data | ||
| post_start: | ||
| - command: chown -R /data 1001:1001 | ||
| user: root | ||
|
|
||
| volumes: | ||
| data: {} # a Docker volume is created with root ownership | ||
| ``` | ||
|
|
||
| ### Pre-stop hooks | ||
|
|
||
ndeloof marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Pre-stop hooks are commands that run before the container is stopped by a specific | ||
| command (like `docker compose down` or stopping it manually with `Ctrl+C`). | ||
| These hooks won't run if the container stops by itself or gets killed suddenly. | ||
|
|
||
| In the following example, before the container stops, the `./data_flush.sh` script is | ||
| run to perform any necessary cleanup. | ||
|
|
||
| ```yaml | ||
| services: | ||
| app: | ||
| image: backend | ||
| pre_stop: | ||
| - command: ./data_flush.sh | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.