Skip to content
This repository has been archived by the owner on Sep 26, 2021. It is now read-only.

Commit

Permalink
swap docker client
Browse files Browse the repository at this point in the history
remove github.com/samalba/dockerclient in favor of
github.com/docker/docker/client
  • Loading branch information
bsdlp committed Nov 7, 2020
1 parent b170508 commit 2944d6d
Show file tree
Hide file tree
Showing 485 changed files with 80,728 additions and 6,182 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.12.9
FROM golang:1.15

RUN apt-get update && apt-get install -y --no-install-recommends \
openssh-client \
Expand Down
189 changes: 174 additions & 15 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
version = "1.4.10"

[[constraint]]
branch = "master"
version = "v19.03.13"
name = "github.com/docker/docker"

[[constraint]]
Expand All @@ -34,6 +34,10 @@
name = "github.com/intel-go/cpuid"
branch = "master"

[[override]]
name = "github.com/golang/protobuf"
version = "v1.0.0"

[prune]
go-tests = true
unused-packages = true
56 changes: 43 additions & 13 deletions libmachine/mcndockerclient/docker_client.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,75 @@
package mcndockerclient

import (
"context"
"fmt"
"io"
"io/ioutil"

"github.com/docker/machine/libmachine/cert"
"github.com/samalba/dockerclient"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
)

// DockerClient creates a docker client for a given host.
func DockerClient(dockerHost DockerHost) (*dockerclient.DockerClient, error) {
func DockerClient(dockerHost DockerHost) (*client.Client, error) {
url, err := dockerHost.URL()
if err != nil {
return nil, err
}

tlsConfig, err := cert.ReadTLSConfig(url, dockerHost.AuthOptions())
if err != nil {
return nil, fmt.Errorf("Unable to read TLS config: %s", err)
}
authOptions := dockerHost.AuthOptions()
return client.NewClientWithOpts(client.WithHost(url), client.WithTLSClientConfig(authOptions.CaCertPath, authOptions.ClientCertPath, authOptions.ClientKeyPath))
}

return dockerclient.NewDockerClient(url, tlsConfig)
// ContainerConfig contains options needed to create and start a container
type ContainerConfig struct {
Image string
Env []string
ExposedPorts map[string]struct{}
Cmd []string
HostConfig *container.HostConfig
}

// CreateContainer creates a docker container.
func CreateContainer(dockerHost DockerHost, config *dockerclient.ContainerConfig, name string) error {
func CreateContainer(ctx context.Context, dockerHost DockerHost, config *ContainerConfig, name string) error {
docker, err := DockerClient(dockerHost)
if err != nil {
return err
}

if err = docker.PullImage(config.Image, nil); err != nil {
rc, err := docker.ImagePull(ctx, config.Image, types.ImagePullOptions{})
if err != nil {
return fmt.Errorf("Unable to pull image: %s", err)
}

var authConfig *dockerclient.AuthConfig
containerID, err := docker.CreateContainer(config, name, authConfig)
_, err = io.Copy(ioutil.Discard, rc)
if err != nil {
return fmt.Errorf("Unable to read image pull status: %s", err)
}

err = rc.Close()
if err != nil {
return fmt.Errorf("Unable to close image pull status: %s", err)
}

containerConfig := &container.Config{
Env: config.Env,
Cmd: strslice.StrSlice(config.Cmd),
ExposedPorts: map[nat.Port]struct{}{},
}
for k := range config.ExposedPorts {
containerConfig.ExposedPorts[nat.Port(k)] = struct{}{}
}

createdContainer, err := docker.ContainerCreate(ctx, containerConfig, config.HostConfig, nil, name)
if err != nil {
return fmt.Errorf("Error while creating container: %s", err)
}

if err = docker.StartContainer(containerID, &config.HostConfig); err != nil {
if err = docker.ContainerStart(ctx, createdContainer.ID, types.ContainerStartOptions{}); err != nil {
return fmt.Errorf("Error while starting container: %s", err)
}

Expand Down
Loading

0 comments on commit 2944d6d

Please sign in to comment.