Skip to content

Commit

Permalink
Merge pull request #8239 from medyagh/verify_docker_env
Browse files Browse the repository at this point in the history
docker-env: restart dockerd inside minikube on failure
  • Loading branch information
medyagh authored May 26, 2020
2 parents 1bfdc4b + 6ff8c9a commit 69d679f
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions cmd/minikube/cmd/docker-env.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (
"io"
"net"
"os"
"os/exec"
"strconv"
"strings"

"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/drivers/kic/oci"
"k8s.io/minikube/pkg/minikube/command"
Expand Down Expand Up @@ -119,6 +121,12 @@ func isDockerActive(r command.Runner) bool {
return sysinit.New(r).Active("docker")
}

func mustRestartDocker(name string, runner command.Runner) {
if err := sysinit.New(runner).Restart("docker"); err != nil {
exit.WithCodeT(exit.Unavailable, `The Docker service within '{{.name}}' is not active`, out.V{"name": name})
}
}

// dockerEnvCmd represents the docker-env command
var dockerEnvCmd = &cobra.Command{
Use: "docker-env",
Expand All @@ -138,14 +146,15 @@ var dockerEnvCmd = &cobra.Command{
out.V{"runtime": co.Config.KubernetesConfig.ContainerRuntime})
}

if ok := isDockerActive(co.CP.Runner); !ok {
exit.WithCodeT(exit.Unavailable, `The docker service within '{{.name}}' is not active`, out.V{"name": cname})
}

sh := shell.EnvConfig{
Shell: shell.ForceShell,
}

if ok := isDockerActive(co.CP.Runner); !ok {
glog.Warningf("dockerd is not active will try to restart it...")
mustRestartDocker(cname, co.CP.Runner)
}

var err error
port := constants.DockerDaemonPort
if driver.NeedsPortForward(driverName) {
Expand All @@ -172,6 +181,13 @@ var dockerEnvCmd = &cobra.Command{
}
}

out, err := tryDockerConnectivity("docker", ec)
if err != nil { // docker might be up but been loaded with wrong certs/config
// to fix issues like this #8185
glog.Warningf("couldn't connect to docker inside minikube. will try to restart dockerd service... output: %s error: %v", string(out), err)
mustRestartDocker(cname, co.CP.Runner)
}

if dockerUnset {
if err := dockerUnsetScript(ec, os.Stdout); err != nil {
exit.WithError("Error generating unset output", err)
Expand Down Expand Up @@ -238,6 +254,23 @@ func dockerEnvVars(ec DockerEnvConfig) map[string]string {
return env
}

// dockerEnvVarsList gets the necessary docker env variables to allow the use of minikube's docker daemon to be used in a exec.Command
func dockerEnvVarsList(ec DockerEnvConfig) []string {
return []string{
fmt.Sprintf("%s=%s", constants.DockerTLSVerifyEnv, "1"),
fmt.Sprintf("%s=%s", constants.DockerHostEnv, dockerURL(ec.hostIP, ec.port)),
fmt.Sprintf("%s=%s", constants.DockerCertPathEnv, ec.certsDir),
fmt.Sprintf("%s=%s", constants.MinikubeActiveDockerdEnv, ec.profile),
}
}

// tryDockerConnectivity will try to connect to docker env from user's POV to detect the problem if it needs reset or not
func tryDockerConnectivity(bin string, ec DockerEnvConfig) ([]byte, error) {
c := exec.Command(bin, "version", "--format={{.Server}}")
c.Env = append(os.Environ(), dockerEnvVarsList(ec)...)
return c.CombinedOutput()
}

func init() {
defaultNoProxyGetter = &EnvNoProxyGetter{}
dockerEnvCmd.Flags().BoolVar(&noProxy, "no-proxy", false, "Add machine IP to NO_PROXY environment variable")
Expand Down

0 comments on commit 69d679f

Please sign in to comment.