Skip to content

Commit

Permalink
Merge pull request #175 from ikolomiyets/force-pull
Browse files Browse the repository at this point in the history
Force testcontainers to always pull images when creating container
  • Loading branch information
gianarb authored Apr 6, 2020
2 parents 90c3f43 + a80d5c8 commit b5ed60c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
37 changes: 19 additions & 18 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,25 @@ type FromDockerfile struct {
// ContainerRequest represents the parameters used to get a running container
type ContainerRequest struct {
FromDockerfile
Image string
Env map[string]string
ExposedPorts []string // allow specifying protocol info
Cmd []string
Labels map[string]string
BindMounts map[string]string
VolumeMounts map[string]string
Tmpfs map[string]string
RegistryCred string
WaitingFor wait.Strategy
Name string // for specifying container name
Privileged bool // for starting privileged container
Networks []string // for specifying network names
NetworkAliases map[string][]string // for specifying network aliases
SkipReaper bool // indicates whether we skip setting up a reaper for this
ReaperImage string // alternative reaper image
AutoRemove bool // if set to true, the container will be removed from the host when stopped
NetworkMode container.NetworkMode
Image string
Env map[string]string
ExposedPorts []string // allow specifying protocol info
Cmd []string
Labels map[string]string
BindMounts map[string]string
VolumeMounts map[string]string
Tmpfs map[string]string
RegistryCred string
WaitingFor wait.Strategy
Name string // for specifying container name
Privileged bool // for starting privileged container
Networks []string // for specifying network names
NetworkAliases map[string][]string // for specifying network aliases
SkipReaper bool // indicates whether we skip setting up a reaper for this
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
}

// ProviderType is an enum for the possible providers
Expand Down
27 changes: 18 additions & 9 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,19 +494,28 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
}
} else {
tag = req.Image
_, _, err = p.client.ImageInspectWithRaw(ctx, tag)
if err != nil {
if client.IsErrNotFound(err) {
pullOpt := types.ImagePullOptions{}
if req.RegistryCred != "" {
pullOpt.RegistryAuth = req.RegistryCred
}
var shouldPullImage bool

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

if shouldPullImage {
pullOpt := types.ImagePullOptions{}
if req.RegistryCred != "" {
pullOpt.RegistryAuth = req.RegistryCred
}

} else {
if err := p.attemptToPullImage(ctx, tag, pullOpt); err != nil {
return nil, err
}
}
Expand Down

0 comments on commit b5ed60c

Please sign in to comment.