func initDocker() error { if !(util.CmdExists("docker-machine") && util.CmdExists("docker")) { return errors.New("docker not installed") } if status := string(util.Command("docker-machine", "status", "default").Output()); status != "Running" { log.Println("docker-machine start ...") if err := util.Command("docker-machine", "start", "default").Run(); err != nil { return err } } if os.Getenv("DOCKER_HOST") == "" || os.Getenv("DOCKER_CERT_PATH") == "" || os.Getenv("DOCKER_TLS_VERIFY") == "" { os.Setenv("SHELL", "/bin/bash") envSettings := strings.Split(string(util.Command("docker-machine", "env", "default").Output()), "\n") for _, line := range envSettings { line = strings.TrimSpace(line) if line == "" || strings.HasPrefix(line, "#") { continue } line = strings.TrimPrefix(line, "export ") keyVal := strings.Split(line, "=") os.Setenv(keyVal[0], keyVal[1]) } } return nil }
func dockerImageExists(image string) (bool, error) { cmd := util.Command("docker", "images", "--quiet=true", image) if len(cmd.Output()) > 0 && cmd.Err() == nil { return true, nil } return false, cmd.Err() }
func dockerRun(args []string) (string, error) { args = append([]string{"run"}, args...) cmd := util.Command("docker", args...) containerID := string(cmd.Output()) if cmd.Err() != nil { return "", cmd.Err() } return containerID, nil }
func dockerPS(filter string) (string, error) { args := []string{ "ps", "--all=true", "--no-trunc=true", "--quiet=true", "--filter=" + filter, } cmd := util.Command("docker", args...) containerID := string(cmd.Output()) if cmd.Err() != nil { return "", cmd.Err() } if containerID == "" { return "", fmt.Errorf("cannot find container with condition %s", filter) } return containerID, nil }
func (c *Container) ports() (map[int]int, error) { result := make(map[int]int) cmd := util.Command("docker", "port", c.ID) out := cmd.Output() lines := bytes.Split(out, []byte("\n")) for _, line := range lines { m := rxPortMapping.FindSubmatch(line) if len(m) != 3 { return nil, fmt.Errorf("unexpected input %s", string(line)) } from, err := strconv.Atoi(string(m[1])) if err != nil { return nil, err } to, err := strconv.Atoi(string(m[2])) if err != nil { return nil, err } result[from] = to } return result, nil }
func (c *Container) Command(cmd string, args ...string) *util.Cmd { args = append([]string{"exec", c.ID, cmd}, args...) return util.Command("docker", args...) }
func (c *Container) Log() string { return string(util.Command("docker", "logs", c.ID).Output()) }
func (c *Container) Remove() error { return util.Command("docker", "rm", c.ID).Run() }
func (c *Container) Kill() error { return util.Command("docker", "kill", c.ID).Run() }
func dockerStart(id string) error { return util.Command("docker", "start", id).Run() }
func dockerPull(image string) error { return util.Command("docker", "pull", image).Run() }
func (c Container) ip() (string, error) { out := util.Command("docker-machine", "ip", "default").Output() return string(bytes.TrimSpace(out)), nil }