Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Manually locate docker socket #1362

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backend_creator

import (
"context"
"fmt"
"github.com/docker/docker/client"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/logs_collector_functions"
Expand All @@ -23,6 +24,10 @@ import (
)

const (
unixSocketPrefix = "unix://"
systemDaemonSocket = "/var/run/docker.sock"
userOwnDaemonSocket = "/.docker/run/docker.sock"

noTempDirPrefix = ""
tempDirNamePattern = "kurtosis_backend_tls_*"
caFileName = "ca.pem"
Expand Down Expand Up @@ -73,10 +78,35 @@ func getLocalDockerKurtosisBackend(
optionalApiContainerModeArgs *APIContainerModeArgs,
) (backend_interface.KurtosisBackend, error) {
dockerClientOpts := []client.Opt{
client.FromEnv,
client.WithAPIVersionNegotiation(),
}

// If the DOCKER_HOST env variable is set, use it. Otherwise, try to locate the daemon socket. Otherwise, fall back
// to env variables as we were doing before
userHomeDir, err := os.UserHomeDir()
gbouv marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
logrus.Debugf("Unable to locate user's home directory, this might affect connection to docker.")
}
userOwnedSocketPath := fmt.Sprintf("%s%s", userHomeDir, userOwnDaemonSocket)
if dockerHostEnvVar := os.Getenv(client.EnvOverrideHost); dockerHostEnvVar != "" {
logrus.Debugf("Connecting to Docker daemon at '%s'", dockerHostEnvVar)
dockerClientOpts = append(dockerClientOpts, client.WithHostFromEnv())
} else if _, err := os.Stat(systemDaemonSocket); err == nil {
logrus.Debugf("Connecting to Docker daemon via unix socket '%s'", systemDaemonSocket)
fullyQualifiedUnixSocket := fmt.Sprintf("%s%s", unixSocketPrefix, systemDaemonSocket)
dockerClientOpts = append(dockerClientOpts, client.WithHost(fullyQualifiedUnixSocket))
} else if _, err := os.Stat(userOwnedSocketPath); err == nil {
logrus.Debugf("Connecting to Docker daemon via unix socket '%s'", userOwnedSocketPath)
fullyQualifiedUnixSocket := fmt.Sprintf("%s%s", unixSocketPrefix, userOwnedSocketPath)
dockerClientOpts = append(dockerClientOpts, client.WithHost(fullyQualifiedUnixSocket))
} else {
logrus.Debugf("Unable to locate Docker daemon socket and '%s' environment variable wasn't set. Falling "+
"back to Docker's own way to connect to locally running daemon. If it fails, make sure docker is running "+
"locally and try setting '%s' to help Kurtosis connect to it.", client.EnvOverrideHost,
client.EnvOverrideHost)
dockerClientOpts = append(dockerClientOpts, client.FromEnv)
gbouv marked this conversation as resolved.
Show resolved Hide resolved
}

localDockerBackend, err := getDockerKurtosisBackend(dockerClientOpts, optionalApiContainerModeArgs)
if err != nil {
return nil, stacktrace.Propagate(err, "Unable to build local Kurtosis Docker backend")
Expand Down