-
-
Notifications
You must be signed in to change notification settings - Fork 398
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
docker.container: Recreate container when args change #1277
Draft
minor-fixes
wants to merge
3
commits into
pyinfra-dev:3.x
Choose a base branch
from
minorhacks:docker_container_reloading
base: 3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
docker.container: Recreate container when args change #1277
minor-fixes
wants to merge
3
commits into
pyinfra-dev:3.x
from
minorhacks:docker_container_reloading
Conversation
This file contains 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
This change refactors the way the `docker.container` operation manages containers, in preparation for work to make recreation more intelligent. This change is (mostly) a pure refactor; future changes will diff the current container against the operation parameters to determine if a container needs to be recreated. This will fix an issue where changing any of the operation arguments does not result in actual container changes upon execution. In this refactor, container parameters are moved to a dedicated class, which centralizes the `docker container` command-line argument generation logic and the future diffing logic. The diff function is roughed in, though it currently reports "no diff" (to match the operation's current behavior). Since conditional recreation complicates the operation's logic on which commands to execute, the decisions are boosted into boolean variables to increase readability. As a side benefit, supporting additional docker container parameters should be more straightforward due to the centralization in said dedicated class (I'm planning on adding support for container args, uid, and other Docker params currently not supported). The only behavioral change is that creating and starting a container is no longer done in one exec (joined by `;`) but rather two separate docker commands. This sidesteps questions about whether `;` is the correct joiner (as opposed to `&&`) and reduces the amount of `kwargs` fishing in the implementation. Tested: * `scripts/dev-test.sh` and `scripts/dev-test-e2e.sh` both pass, save for warnings also present prior to this change
This change adds an `args` parameter to the `docker.container` operation that passes said supplied args to the container at creation time. This allows the operation to support container images that have an entrypoint expecting to receive additional arguments, without needing to build+push a custom image that embeds said arguments.
This PR allows the `docker.container` operation to tear down and recreate the container when operation arguments change, instead of reporting `No change` and doing nothing. This is intended to reduce the possibility for human error/need for manual intervention when changing args to `docker.container` operations. Since it is not possible to extract all operation args from e.g. `docker inspect` output, this PR takes a similar approach to Docker Compose to tackle this issue - it serializes the operation args in a deterministic way, hashes the serialized bytes, and stores this as a label on the container. If the hash differs from a currently-running container, the container is recreated. Tested: Added additional tests for behavior when args are changing/static in different scenarios
5147744
to
8ef9a23
Compare
Fizzadar
reviewed
Mar 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh now I see how this is similar to compose - did not know that's how compose managed this, TIL!
Looks good to me overall, I think this still has value as it allows us to properly handle diff cases without using docker compose..
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR allows the
docker.container
operation to tear down andrecreate the container when operation arguments change, instead of
reporting
No change
and doing nothing. This is intended to reduce thepossibility for human error/need for manual intervention when changing
args to
docker.container
operations.Since it is not possible to extract all operation args from e.g.
docker inspect
output, this PR takes a similar approach to Docker Compose totackle this issue - it serializes the operation args in a deterministic
way, hashes the serialized bytes, and stores this as a label on the
container. If the hash differs from a currently-running container, the
container is recreated.
Tested: Added additional tests for behavior when args are
changing/static in different scenarios