simplify finding docker socket

This commit is contained in:
Zettat123 2023-06-30 10:53:37 +08:00
parent 06f0ce2f51
commit c6d75e0ab2
2 changed files with 13 additions and 36 deletions

View file

@ -63,10 +63,6 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command,
}
if ls.RequireDocker() {
// TODO: if cfg.Container.DockerHost is a TCP socket like "tcp://127.0.0.1:2375"
// it will pass the envcheck.CheckIfDockerRunning but can't be mounted to the job container.
// In this case, act_runner will mount /var/run/docker.sock by default.
dockerSocketPath, err := getDockerSocketPath(cfg.Container.DockerHost)
if err != nil {
return err
@ -74,11 +70,21 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command,
if err := envcheck.CheckIfDockerRunning(ctx, dockerSocketPath); err != nil {
return err
}
// if dockerSocketPath passes the check, override DOCKER_HOST with dockerSocketPath
os.Setenv("DOCKER_HOST", dockerSocketPath)
// empty cfg.Container.DockerHost means act_runner need to find an available docker host automatically
// and assign the path to cfg.Container.DockerHost
if cfg.Container.DockerHost == "" {
cfg.Container.DockerHost = dockerSocketPath
}
os.Setenv("DOCKER_HOST", dockerSocketPath)
// check the scheme, if the scheme is not npipe or unix
// set cfg.Container.DockerHost to "-" because it can't be mounted to the job conatiner
if protoIndex := strings.Index(cfg.Container.DockerHost, "://"); protoIndex != -1 {
scheme := cfg.Container.DockerHost[:protoIndex]
if !strings.EqualFold(scheme, "npipe") && !strings.EqualFold(scheme, "unix") {
cfg.Container.DockerHost = "-"
}
}
}
cli := client.New(

View file

@ -6,8 +6,6 @@ package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
)
func getDockerSocketPath(configDockerHost string) (string, error) {
@ -17,7 +15,7 @@ func getDockerSocketPath(configDockerHost string) (string, error) {
if configDockerHost != "" && configDockerHost != "-" {
socketPath = configDockerHost
} else {
socket, found := socketLocation()
socket, found := os.LookupEnv("DOCKER_HOST")
if !found {
return "", fmt.Errorf("daemon Docker Engine socket not found and docker_host config was invalid")
} else {
@ -27,30 +25,3 @@ func getDockerSocketPath(configDockerHost string) (string, error) {
return socketPath, nil
}
var commonSocketPaths = []string{
"/var/run/docker.sock",
"/var/run/podman/podman.sock",
"$HOME/.colima/docker.sock",
"$XDG_RUNTIME_DIR/docker.sock",
`\\.\pipe\docker_engine`,
"$HOME/.docker/run/docker.sock",
}
// returns socket path or false if not found any
func socketLocation() (string, bool) {
if dockerHost, exists := os.LookupEnv("DOCKER_HOST"); exists {
return dockerHost, true
}
for _, p := range commonSocketPaths {
if _, err := os.Lstat(os.ExpandEnv(p)); err == nil {
if strings.HasPrefix(p, `\\.\`) {
return "npipe://" + filepath.ToSlash(os.ExpandEnv(p)), true
}
return "unix://" + filepath.ToSlash(os.ExpandEnv(p)), true
}
}
return "", false
}