func (d *Controller) StartCtr(ctrId, etcdDataDir string) error {
	binds := []string{
		"/sys:/sys:rw",
		"/var/lib/docker:/var/lib/docker",
		"/mnt/sda1/var/lib/docker:/mnt/sda1/var/lib/docker",
		"/var/lib/kubelet:/var/lib/kubelet",
		"/var/run:/var/run:rw",
		"/:/rootfs:ro",
	}
	if len(etcdDataDir) != 0 {
		dataBind := fmt.Sprintf("%s:%s", etcdDataDir, ContainerDataDir)
		binds = append(binds, dataBind)
	}
	hostCfg := &docker.HostConfig{
		Binds:         binds,
		RestartPolicy: docker.AlwaysRestart(),
		NetworkMode:   "host",
		PidMode:       "host",
		Privileged:    true,
	}
	d.log.Println("Starting localkube container...")
	err := d.StartContainer(ctrId, hostCfg)
	if err != nil {
		return fmt.Errorf("could not start container `%s`: %v", ctrId, err)
	}
	return nil
}
StartCtr starts the specified Container with options specific to localkube. If etcdDataDir is empty, no directory will be mounted for etcd.
package localkubectl
import (
	"errors"
	"fmt"
	"io"
	"log"
	"strings"
	"github.com/docker/docker/pkg/jsonmessage"
	"github.com/docker/docker/pkg/term"
	docker "github.com/fsouza/go-dockerclient"
)
var (
	LocalkubeLabel = "rsprd.com/name=localkube"
	LocalkubeContainerName = "localkube"
	LocalkubeImageName = "redspreadapps/localkube"
	ContainerDataDir = "/var/localkube/data"
	RedspreadName = "rsprd.com/name"
)
type Controller struct {
	*docker.Client
	log *log.Logger
	out io.Writer
}
func NewController(client *docker.Client, out io.Writer) (*Controller, error) {
	_, err := client.Version()
	if err != nil {
		return nil, fmt.Errorf("Unable to establish connection with Docker daemon: %v", err)
	}
	logger := log.New(out, "", 0)
	return &Controller{
		Client: client,
		log:    logger,
		out:    out,
	}, nil
}
func NewControllerFromEnv(out io.Writer) (*Controller, error) {
	client, err := docker.NewClientFromEnv()
	if err != nil {
		return nil, fmt.Errorf("could not create Docker client: %v", err)
	}
	return NewController(client, out)
}
func (d *Controller) ListLocalkubeCtrs(all bool) ([]docker.APIContainers, error) {
	ctrs, err := d.ListContainers(docker.ListContainersOptions{
		All: all,
		Filters: map[string][]string{
			"label": {LocalkubeLabel},
		},
	})
	if err != nil {
		return nil, fmt.Errorf("could not list containers: %v", err)
	}
	return ctrs, nil
}
func (d *Controller) OnlyLocalkubeCtr() (ctrId string, running bool, err error) {
	ctrs, err := d.ListLocalkubeCtrs(true)
	if err != nil {
		return "", false, err
	} else if len(ctrs) < 1 {
		return "", false, ErrNoContainer
	} else if len(ctrs) > 1 {
		return "", false, ErrTooManyLocalkubes
	}
	ctr := ctrs[0]
	return ctr.ID, runningStatus(ctr.Status), nil
}
func (d *Controller) CreateCtr(name, imageTag string) (ctrId string, running bool, err error) {
	image := fmt.Sprintf("%s:%s", LocalkubeImageName, imageTag)
	ctrOpts := docker.CreateContainerOptions{
		Name: name,
		Config: &docker.Config{
			Hostname: name,
			Image:    image,
			Env: []string{
				fmt.Sprintf("KUBE_ETCD_DATA_DIRECTORY=%s", ContainerDataDir),
			},
			Labels: map[string]string{
				RedspreadName: name,
			},
			StopSignal: "SIGINT",
		},
	}
	d.log.Println("Creating localkube container...")
	ctr, err := d.CreateContainer(ctrOpts)
	if err != nil {
		if err == docker.ErrNoSuchImage {
			d.log.Printf("Localkube image '%s' not found, pulling now:\n", image)
			if pullErr := d.PullImage(imageTag, false); pullErr != nil {
				return "", false, pullErr
			}
			return d.CreateCtr(name, imageTag)
		}
		return "", false, fmt.Errorf("could not create locakube container: %v", err)
	}
	return ctr.ID, ctr.State.Running, nil
}
func (d *Controller) PullImage(imageTag string, silent bool) error {
	pullOpts := docker.PullImageOptions{
		Repository: LocalkubeImageName,
		Tag:        imageTag,
	}
	in, out := io.Pipe()
	if !silent {
		pullOpts.RawJSONStream = true
		pullOpts.OutputStream = out
		outFd, isTerminal := term.GetFdInfo(d.out)
		go jsonmessage.DisplayJSONMessagesStream(in, d.out, outFd, isTerminal)
	}
	err := d.Client.PullImage(pullOpts, docker.AuthConfiguration{})
	if err != nil {
		return fmt.Errorf("failed to pull localkube image: %v", err)
	}
	return nil
}