Skip to content
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: Refactor to support container recreation #1272

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from

Conversation

minor-fixes
Copy link

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

"DNSNames": "None"
}
}
"docker.DockerContainer": {
Copy link
Author

@minor-fixes minor-fixes Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the diff here; the diffs in this fact object should be purely whitespace (I think formatting this JSON does help readability, so I'd prefer to leave it in if possible)

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
@simonhammes
Copy link
Contributor

Just some food for thought: Would supporting docker compose natively in pyinfra (tracked in #1276) make sense instead?

docker compose up -d essentially handles all of the diffing.
Idempotency could be achieved by checking whether the file .yml file to template has changed.

@minor-fixes
Copy link
Author

Just some food for thought: Would supporting docker compose natively in pyinfra (tracked in #1276) make sense instead?

docker compose up -d essentially handles all of the diffing. Idempotency could be achieved by checking whether the file .yml file to template has changed.

Yes, I think that'd make a ton more sense - otherwise the routines here will essentially replicate the functionality of docker compose. Is someone already working on that?

@simonhammes
Copy link
Contributor

Is someone already working on that?

Not that I'm aware of 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants