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

Recreate on volume change #12223

Closed
wants to merge 1 commit into from

Conversation

jhrotko
Copy link
Contributor

@jhrotko jhrotko commented Oct 21, 2024

What I did
Add volumes on configuration hash in order to recreate container when volumes changes

Related issue

(not mandatory) A picture of a cute animal, if possible in relation to what you did
image

@jhrotko jhrotko requested review from ndeloof and glours October 21, 2024 11:04
@jhrotko jhrotko self-assigned this Oct 21, 2024
@ndeloof
Copy link
Contributor

ndeloof commented Oct 21, 2024

IMHO the right way to address this issue is for volumes/networks to have a config-hash label just like services, so we detect those need to be recreated, and then in cascade we re-create the attached containers

@jhrotko jhrotko force-pushed the recreate-on-volume-change branch 3 times, most recently from 4032d1b to dd57c3d Compare October 22, 2024 15:40
if o.Driver == "" { // (TODO: jhrotko) This probably should be fixed in compose-go
o.Driver = "local"
}
o.External = false // the name can change. Need to think about this case
Copy link
Contributor

Choose a reason for hiding this comment

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

if volume is external you should just never enter this func

Copy link
Contributor Author

@jhrotko jhrotko Oct 25, 2024

Choose a reason for hiding this comment

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

there is an edge case where you can change the name of an external volume and we need to update the service. The current code does not comtemplate this case. I need to implement this

Copy link
Contributor

Choose a reason for hiding this comment

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

by nature, external resources are not managed by compose. There's nothing we can and should do to cover this scenario

Copy link
Contributor

Choose a reason for hiding this comment

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

I better understand your point, the issue you refer to is a special case for a feature we don't support (yet): recreate services when referred resource is reconfigured. I agree we could kill 2 birds with one stone here, but this makes many changes at once.
Maybe split this into 2 separate PRs ?

  • 1 to recreate volumes on config changes
  • 1 to detect named resource changed (ensureService to inspect mounted volumes for container and check name matches expectation)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I applied both solutions to this PR

Copy link
Contributor

Choose a reason for hiding this comment

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

sure, I mean it could be simpler to review if we split this effort into two separate PRs

@jhrotko jhrotko force-pushed the recreate-on-volume-change branch from fb72af3 to 2823c25 Compare October 24, 2024 15:11
@jhrotko
Copy link
Contributor Author

jhrotko commented Oct 24, 2024

Added --recreate-volumes in up command. Missing e2e tests @ndeloof @glours . I will clean up the commits after the approvals :)

I still need to not ignore labels to the VolumeHash!

I am also wondering if I should add this flag to create command.

@@ -148,6 +148,7 @@ func upCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *c
flags.BoolVar(&create.noBuild, "no-build", false, "Don't build an image, even if it's policy")
flags.StringVar(&create.Pull, "pull", "policy", `Pull image before running ("always"|"missing"|"never")`)
flags.BoolVar(&create.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file")
flags.BoolVar(&create.recreateVolumes, "recreate-volumes", false, "Recreate volumes when volume configuration in the Compose file changes.")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we can rename it :P

Copy link
Contributor

Choose a reason for hiding this comment

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

I also wonder this could be ambiguous, considering we have --renew-anon-volumes. Need to noun that make it clear we recreate volumes when config has diverged

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how about update-volumes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ndeloof the recreate-volumes makes somewhat sense to me, also given the 'pattern' for force-recreate flag. I don't think it is ambiguous with the flag --renew-anon-volumes

Copy link
Contributor

Choose a reason for hiding this comment

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

it is, as we only recreate diverged volumes. Not all volumes (and not anonymous ones)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ndeloof I can add that at the flag description: that this will ignore anonymous volumes. As for the names maybe? If you have any other ideas please feel free to suggest

--recreate-volumes-if-changed
--force-recreate-volumes
--sync-volumes
--update-volumes
--refresh-named-volumes
--rebuild-volumes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or do you prefer to make this the default behaviour with a prompt?

@jhrotko jhrotko force-pushed the recreate-on-volume-change branch from c430b99 to 0b886e9 Compare October 25, 2024 13:51
Copy link
Contributor

@ndeloof ndeloof left a comment

Choose a reason for hiding this comment

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

ensureService doesn't need to be updated for this feature AFAICT, as we remove containers mounting a volume before we recreate volume, so ensureService will start from scratch - possibly just requires observedState to be updated after Remove?

@@ -148,6 +148,7 @@ func upCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *c
flags.BoolVar(&create.noBuild, "no-build", false, "Don't build an image, even if it's policy")
flags.StringVar(&create.Pull, "pull", "policy", `Pull image before running ("always"|"missing"|"never")`)
flags.BoolVar(&create.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file")
flags.BoolVar(&create.recreateVolumes, "recreate-volumes", false, "Recreate volumes when volume configuration in the Compose file changes.")
Copy link
Contributor

Choose a reason for hiding this comment

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

I also wonder this could be ambiguous, considering we have --renew-anon-volumes. Need to noun that make it clear we recreate volumes when config has diverged

@jhrotko jhrotko force-pushed the recreate-on-volume-change branch from 0b886e9 to f9a8185 Compare October 25, 2024 14:03
@jhrotko jhrotko force-pushed the recreate-on-volume-change branch 10 times, most recently from 52a5d2e to e83964b Compare October 30, 2024 13:57
if !shouldRecreateVolume {
continue
}
if !recreateVolumes && shouldRecreateVolume {
Copy link
Contributor

Choose a reason for hiding this comment

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

we could rely on user interaction to confirm recreation, the way we do on https://github.com/docker/compose/blob/main/pkg/compose/remove.go#L88

Copy link
Contributor Author

Choose a reason for hiding this comment

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

looks really nice, but I will defer it for a follow-up PR

@jhrotko jhrotko force-pushed the recreate-on-volume-change branch from e83964b to b80222b Compare November 6, 2024 14:26
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.

[BUG] docker compose up does not recreate containers when named volume configuration is changed
2 participants