diff --git a/docker/docker.go b/docker/docker.go index ab1788d9..44060751 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -22,11 +22,26 @@ var K8sDockerVersions = map[string][]string{ } func DoRunContainer(ctx context.Context, dClient *client.Client, imageCfg *container.Config, hostCfg *container.HostConfig, containerName string, hostname string, plane string) error { - isRunning, err := IsContainerRunning(ctx, dClient, hostname, containerName, false) + container, err := dClient.ContainerInspect(ctx, containerName) if err != nil { - return err + if !client.IsErrNotFound(err) { + return err + } + if err := UseLocalOrPull(ctx, dClient, hostname, imageCfg.Image, plane); err != nil { + return err + } + resp, err := dClient.ContainerCreate(ctx, imageCfg, hostCfg, nil, containerName) + if err != nil { + return fmt.Errorf("Failed to create [%s] container on host [%s]: %v", containerName, hostname, err) + } + if err := dClient.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { + return fmt.Errorf("Failed to start [%s] container on host [%s]: %v", containerName, hostname, err) + } + log.Infof(ctx, "[%s] Successfully started [%s] container on host [%s]", plane, containerName, hostname) + return nil } - if isRunning { + // Check for upgrades + if container.State.Running { log.Infof(ctx, "[%s] Container [%s] is already running on host [%s]", plane, containerName, hostname) isUpgradable, err := IsContainerUpgradable(ctx, dClient, imageCfg, containerName, hostname, plane) if err != nil { @@ -38,18 +53,11 @@ func DoRunContainer(ctx context.Context, dClient *client.Client, imageCfg *conta return nil } - err = UseLocalOrPull(ctx, dClient, hostname, imageCfg.Image, plane) - if err != nil { - return err - } - resp, err := dClient.ContainerCreate(ctx, imageCfg, hostCfg, nil, containerName) - if err != nil { - return fmt.Errorf("Failed to create [%s] container on host [%s]: %v", containerName, hostname, err) - } - if err := dClient.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { + // start if not running + log.Infof(ctx, "[%s] Starting stopped container [%s] on host [%s]", plane, containerName, hostname) + if err := dClient.ContainerStart(ctx, container.ID, types.ContainerStartOptions{}); err != nil { return fmt.Errorf("Failed to start [%s] container on host [%s]: %v", containerName, hostname, err) } - logrus.Debugf("[%s] Successfully started [%s] container: [%s]", plane, containerName, resp.ID) log.Infof(ctx, "[%s] Successfully started [%s] container on host [%s]", plane, containerName, hostname) return nil } diff --git a/services/etcd.go b/services/etcd.go index 21af5ed1..5e99da49 100644 --- a/services/etcd.go +++ b/services/etcd.go @@ -8,7 +8,6 @@ import ( etcdclient "github.com/coreos/etcd/client" "github.com/docker/docker/api/types/container" - "github.com/docker/go-connections/nat" "github.com/rancher/rke/docker" "github.com/rancher/rke/hosts" "github.com/rancher/rke/log" @@ -64,20 +63,7 @@ func buildEtcdConfig(host *hosts.Host, etcdService v3.ETCDService, initCluster s RestartPolicy: container.RestartPolicy{Name: "always"}, Binds: []string{ "/var/lib/etcd:/etcd-data"}, - PortBindings: nat.PortMap{ - "2379/tcp": []nat.PortBinding{ - { - HostIP: "0.0.0.0", - HostPort: "2379", - }, - }, - "2380/tcp": []nat.PortBinding{ - { - HostIP: "0.0.0.0", - HostPort: "2380", - }, - }, - }, + NetworkMode: "host", } for arg, value := range etcdService.ExtraArgs { cmd := fmt.Sprintf("--%s=%s", arg, value)