-
Notifications
You must be signed in to change notification settings - Fork 119
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
Fix: use container name consistent with Docker Compose API #633
Fix: use container name consistent with Docker Compose API #633
Conversation
* Docker compose v1 (python) and v2 (Go) use different service naming conventions. Set service name based on the version.
❕ Build Aborted
Expand to view the summary
Build stats
Test stats 🧪
Steps errorsExpand to view the steps failures
|
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.
Thanks for your contribution, Sai! I left a few comments, so let me know what do you think :)
internal/compose/compose.go
Outdated
@@ -340,7 +345,29 @@ func (p *Project) runDockerComposeCmd(opts dockerComposeOptions) error { | |||
return cmd.Run() | |||
} | |||
|
|||
func (p *Project) DockerComposeVersion() (string, error) { |
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.
As this method is used only by ContainerName
, do you think we could make it package-visible and access it only in ContainerName
?
This way you would prevent adjusting all calling code. If we need this condition in other places, we can refactor it then.
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.
Sounds good. I have refactored this to make it package-visible. To avoid calling docker-compose --version
each time ContainerName
is invoked I've stored the value in a project variable.
BTW CI reported some problems with this PR. |
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.
Hey @r00tu53r, did you forget to push your branch? I don't see any changes applied, but comments were marked as resolved.
@mtojek sorry I forgot to push the changes. I've resolved all of the above issues. |
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.
I added a few suggestions, let me know what do you think.
internal/compose/compose.go
Outdated
@@ -340,7 +343,38 @@ func (p *Project) runDockerComposeCmd(opts dockerComposeOptions) error { | |||
return cmd.Run() | |||
} | |||
|
|||
func (p *Project) dockerComposeVersion() (*semver.Version, error) { | |||
|
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.
nit: empty line (cane be removed)
internal/compose/compose.go
Outdated
// ContainerName method the container name for the service. | ||
func (p *Project) ContainerName(serviceName string) string { | ||
return fmt.Sprintf("%s_%s_1", p.name, serviceName) | ||
if p.dcMajorVersion == "" { |
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.
It might be cleaner to keep the ContainerName
function as simple as possible:
if p.dockerComposeV1 {
return fmt.Sprintf("%s_%s_1", p.name, serviceName)
}
return fmt.Sprintf("%s-%s-1", p.name, serviceName)
Then, the logic determining p.dockerComposeV1
can be moved to the NewProject
.
dockerComposeV1
can be bool, as the Compose V2 seems to deprecate V1 in the future.
internal/compose/compose.go
Outdated
name, | ||
paths, | ||
name: name, | ||
dcMajorVersion: "", |
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.
As suggested in the other comment, dcMajorVersion
can be replaced with dockerComposeV1
(bool).
Then in the NewProject
, you can evaluate its value.
BTW I hope that the Project
isn't created somewhere just with {}
.
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.
Two small nit-picks are left, but it looks fine now!
Let me know what do you think about them and then I will merge this PR.
internal/compose/compose.go
Outdated
name, | ||
paths, | ||
name: name, | ||
dockerComposeV1: true, |
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.
nit: You could also skip this property here and set it only when ve.Major
== 1 or undefined. But it would the same outcome :)
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.
Sorry I didn't quite get what you meant. Did you mean we could use the semver.Version
(*Version
) inside the Project
and use ver.Major()
in ContainerName
instead ?
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.
Currently, you're setting the property dockerComposeV1: true
by default. But you could adjust it to be the other way around, so just assume:
c := Project{
name: name,
composeFilePaths: paths
}
and then just enable it for two cases:
if ver.Major() == 1 {
c.dockerComposeV1 = true
}
if err != nil {
logger.Errorf("Unable to determine docker-compose version: %v. Defaulting to 1.x", err)
c.dockerComposeV1 = true
return &c, nil
}
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.
Let's see if I have permissions to push it here, so that we don't need to bother you anymore :)
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.
Done
/test |
/test |
Error seems to be unrelated. I will merge this PR. |
💔 Build Failed
Expand to view the summary
Build stats
Test stats 🧪
Steps errorsExpand to view the steps failures
|
different service naming conventions. Set
service name based on the version.
Confirmed with docker compose source and tests:
https://github.com/docker/compose/blob/v2/pkg/compose/compose.go#L36