Skip to content
Closed
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
3 changes: 2 additions & 1 deletion container.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ type ContainerRequest struct {
ReaperImage string // alternative reaper image
AutoRemove bool // if set to true, the container will be removed from the host when stopped
NetworkMode container.NetworkMode
AlwaysPullImage bool // Always pull image
AlwaysPullImage bool // Always pull image
ImagePlatform string // specify a different platform for the image
}

// ProviderType is an enum for the possible providers
Expand Down
33 changes: 29 additions & 4 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"github.com/pkg/errors"

"github.com/testcontainers/testcontainers-go/wait"

specs "github.com/opencontainers/image-spec/specs-go/v1"
)

// Implement interfaces
Expand Down Expand Up @@ -598,34 +600,46 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
}

var tag string

var customOS, customArch string
if req.ShouldBuildImage() {
tag, err = p.BuildImage(ctx, &req)
if err != nil {
return nil, err
}
} else {
tag = req.Image

if len(req.ImagePlatform) != 0 {
customOS = strings.Split(req.ImagePlatform, "/")[0]
customArch = strings.Split(req.ImagePlatform, "/")[1]
Comment on lines +614 to +615
Copy link
Member

Choose a reason for hiding this comment

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

This is correct, as all platforms are formed by a string in this format: "os/arch"

👍

Copy link
Contributor

Choose a reason for hiding this comment

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

A platform may also have 3 parts (os/arch/variant) in some cases. It's probably safest to use containerd's platforms.Parse func for this.

}
var shouldPullImage bool

if req.AlwaysPullImage {
shouldPullImage = true // If requested always attempt to pull image
} else {
_, _, err = p.client.ImageInspectWithRaw(ctx, tag)
image, _, err := p.client.ImageInspectWithRaw(ctx, tag)
if err != nil {
if client.IsErrNotFound(err) {
shouldPullImage = true
} else {
return nil, err
}
}
if image.Architecture != customArch || image.Os != customOS {
shouldPullImage = true
}
}

if shouldPullImage {
pullOpt := types.ImagePullOptions{}

if len(req.ImagePlatform) != 0 {
pullOpt.Platform = req.ImagePlatform
}
if req.RegistryCred != "" {
pullOpt.RegistryAuth = req.RegistryCred
}

if err := p.attemptToPullImage(ctx, tag, pullOpt); err != nil {
return nil, err
}
Expand Down Expand Up @@ -692,7 +706,18 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
EndpointsConfig: endpointConfigs,
}

resp, err := p.client.ContainerCreate(ctx, dockerInput, hostConfig, &networkingConfig, nil, req.Name)
var platSpec *specs.Platform
if len(req.ImagePlatform) != 0 {
platSpec = &specs.Platform{
Architecture: customArch,
OS: customOS,
}
} else {
platSpec = nil
}

resp, err := p.client.ContainerCreate(ctx, dockerInput, hostConfig, &networkingConfig, platSpec, req.Name)

if err != nil {
return nil, err
}
Expand Down
23 changes: 22 additions & 1 deletion docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"math/rand"
"net/http"
Expand All @@ -16,6 +15,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/docker/docker/errdefs"

"github.com/docker/docker/api/types/volume"
Expand Down Expand Up @@ -1382,6 +1383,26 @@ func TestContainerNonExistentImage(t *testing.T) {
})
}

func TestContainerCustomPlatformImage(t *testing.T) {
t.Run("Use a non-existent custom image platform", func(t *testing.T) {
nonExistentPlatform := "windows/arm12"
c, err := GenericContainer(context.Background(), GenericContainerRequest{
ContainerRequest: ContainerRequest{
Image: "redis:latest",
SkipReaper: true,
ImagePlatform: nonExistentPlatform,
},
Started: true,
})
if c != nil {
defer c.Terminate(context.Background())
}
if err == nil {
t.Fatalf("Expected non-nil error with a non-existent platform: %s", nonExistentPlatform)
}
})
}

func TestContainerWithCustomHostname(t *testing.T) {
ctx := context.Background()
name := fmt.Sprintf("some-nginx-%s-%d", t.Name(), rand.Int())
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/moby/sys/mount v0.2.0 // indirect
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210324051608-47abb6519492
Expand Down