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

fix: prepend substitutors for built images #2577

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,20 @@ func (c *ContainerRequest) BuildOptions() (types.ImageBuildOptions, error) {

// make sure the first tag is the one defined in the ContainerRequest
tag := fmt.Sprintf("%s:%s", c.GetRepo(), c.GetTag())

// apply substitutors to the built image
for _, is := range c.ImageSubstitutors {
modifiedTag, err := is.Substitute(tag)
if err != nil {
return buildOptions, fmt.Errorf("failed to substitute image %s with %s: %w", tag, is.Description(), err)
}

if modifiedTag != tag {
Logger.Printf("✍🏼 Replacing image with %s. From: %s to %s\n", is.Description(), tag, modifiedTag)
tag = modifiedTag
}
}

if len(buildOptions.Tags) > 0 {
// prepend the tag
buildOptions.Tags = append([]string{tag}, buildOptions.Tags...)
Expand Down
24 changes: 12 additions & 12 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,18 +1023,6 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
// always append the hub substitutor after the user-defined ones
req.ImageSubstitutors = append(req.ImageSubstitutors, newPrependHubRegistry(tcConfig.HubImageNamePrefix))

for _, is := range req.ImageSubstitutors {
modifiedTag, err := is.Substitute(imageName)
if err != nil {
return nil, fmt.Errorf("failed to substitute image %s with %s: %w", imageName, is.Description(), err)
}

if modifiedTag != imageName {
p.Logger.Printf("✍🏼 Replacing image with %s. From: %s to %s\n", is.Description(), imageName, modifiedTag)
imageName = modifiedTag
}
}

var platform *specs.Platform

if req.ShouldBuildImage() {
Expand All @@ -1043,6 +1031,18 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
return nil, err
}
} else {
for _, is := range req.ImageSubstitutors {
modifiedTag, err := is.Substitute(imageName)
if err != nil {
return nil, fmt.Errorf("failed to substitute image %s with %s: %w", imageName, is.Description(), err)
}

if modifiedTag != imageName {
Logger.Printf("✍🏼 Replacing image with %s. From: %s to %s\n", is.Description(), imageName, modifiedTag)
imageName = modifiedTag
}
}

if req.ImagePlatform != "" {
p, err := platforms.Parse(req.ImagePlatform)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions image_substitutors_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testcontainers

import (
"context"
"testing"
)

Expand Down Expand Up @@ -86,3 +87,34 @@ func TestPrependHubRegistrySubstitutor(t *testing.T) {
})
})
}

func TestSubstituteBuiltImage(t *testing.T) {
req := GenericContainerRequest{
ContainerRequest: ContainerRequest{
FromDockerfile: FromDockerfile{
Context: "testdata",
Dockerfile: "echo.Dockerfile",
Tag: "my-image",
Repo: "my-repo",
},
ImageSubstitutors: []ImageSubstitutor{newPrependHubRegistry("my-registry")},
},
Started: false,
}

t.Run("should not use the properties prefix on built images", func(t *testing.T) {
c, err := GenericContainer(context.Background(), req)
if err != nil {
t.Fatal(err)
}

json, err := c.Inspect(context.Background())
if err != nil {
t.Fatal(err)
}

if json.Config.Image != "my-registry/my-repo:my-image" {
t.Errorf("expected my-registry/my-repo:my-image, got %s", json.Config.Image)
}
})
}