Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Add Docker Driver configuration options #661

Merged
merged 2 commits into from
Mar 11, 2019
Merged
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
29 changes: 18 additions & 11 deletions pkg/driver/docker_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
Expand All @@ -26,8 +25,9 @@ import (
type DockerDriver struct {
config map[string]string
// If true, this will not actually run Docker
Simulate bool
dockerCli command.Cli
Simulate bool
dockerCli command.Cli
dockerConfigurationOptions []DockerConfigurationOption
}

// Run executes the Docker driver
Expand All @@ -40,6 +40,11 @@ func (d *DockerDriver) Handles(dt string) bool {
return dt == ImageTypeDocker || dt == ImageTypeOCI
}

// AddConfigurationOptions adds configuration callbacks to the driver
func (d *DockerDriver) AddConfigurationOptions(opts ...DockerConfigurationOption) {
d.dockerConfigurationOptions = append(d.dockerConfigurationOptions, opts...)
}

// Config returns the Docker driver configuration options
func (d *DockerDriver) Config() map[string]string {
return map[string]string{
Expand Down Expand Up @@ -127,13 +132,6 @@ func (d *DockerDriver) exec(op *Operation) error {
env = append(env, fmt.Sprintf("%s=%v", k, v))
}

mounts := []mount.Mount{
{
Type: mount.TypeBind,
Source: "/var/run/docker.sock",
Target: "/var/run/docker.sock",
},
}
cfg := &container.Config{
Image: op.Image,
Env: env,
Expand All @@ -142,7 +140,13 @@ func (d *DockerDriver) exec(op *Operation) error {
AttachStdout: true,
}

hostCfg := &container.HostConfig{Mounts: mounts, AutoRemove: true}
hostCfg := &container.HostConfig{AutoRemove: true}

for _, opt := range d.dockerConfigurationOptions {
if err := opt(cfg, hostCfg); err != nil {
return err
}
}

resp, err := cli.Client().ContainerCreate(ctx, cfg, hostCfg, nil, "")
switch {
Expand Down Expand Up @@ -234,3 +238,6 @@ func generateTar(files map[string]string) (io.Reader, error) {
}()
return r, nil
}

// DockerConfigurationOption is an option used to customize docker driver container and host config
type DockerConfigurationOption func(*container.Config, *container.HostConfig) error