Repository: nektos/act (language: go)

Issue:
`Error: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?` on macOS while docker commands are working
### Bug report info

```plain text
act version:            0.2.43
GOOS:                   darwin
GOARCH:                 amd64
NumCPU:                 8
Docker host:            DOCKER_HOST environment variable is unset/empty.
Sockets found:
	/Users/lena/.docker/run/docker.sock
Config files:           
	/Users/lena/.actrc:
		-P ubuntu-latest=catthehacker/ubuntu:act-latest
		-P ubuntu-22.04=catthehacker/ubuntu:act-22.04
		-P ubuntu-20.04=catthehacker/ubuntu:act-20.04
		-P ubuntu-18.04=catthehacker/ubuntu:act-18.04
Build info:
	Go version:            go1.20.1
	Module path:           command-line-arguments
	Main version:          
	Main path:             
	Main checksum:         
	Build settings:
		-buildmode:           exe
		-compiler:            gc
		-ldflags:             -X main.version=0.2.43
		CGO_ENABLED:          1
		CGO_CFLAGS:           
		CGO_CPPFLAGS:         
		CGO_CXXFLAGS:         
		CGO_LDFLAGS:          
		GOARCH:               amd64
		GOOS:                 darwin
		GOAMD64:              v1

Error: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
```


### Command used with act

```sh
act
```


### Describe issue

I expected it to not print `Error: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?`, because commands like `docker ps` do work:
```
CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS         PORTS                NAMES
b55271169ff4   docker/getting-started   "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes   0.0.0.0:80->80/tcp   funny_euclid
```


### Link to GitHub repository

https://github.com/LenaWil/Squire/commit/d038cc03cd0b075071a2ab001ed226c1b3953225

### Workflow content

```yml
name: Lint Black (official)

# Without review-dog… maybe it’s a bit of waste to make them run twice, but anyway.
on: [push, pull_request]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: psf/black@stable
```


### Relevant log output

```sh
DEBU[0000] Loading environment from /Users/lena/Documents/Knights/Squire/.env 
DEBU[0000] Loading action inputs from /Users/lena/Documents/Knights/Squire/.input 
DEBU[0000] Loading secrets from /Users/lena/Documents/Knights/Squire/.secrets 
DEBU[0000] Loading workflows from '/Users/lena/Documents/Knights/Squire/.github/workflows' 
DEBU[0000] Loading workflows recursively                
DEBU[0000] Found workflow 'black-review-dog.yml' in '/Users/lena/Documents/Knights/Squire/.github/workflows/black-review-dog.yml' 
DEBU[0000] Found workflow 'black.yml' in '/Users/lena/Documents/Knights/Squire/.github/workflows/black.yml' 
DEBU[0000] Found workflow 'run_tests.yml' in '/Users/lena/Documents/Knights/Squire/.github/workflows/run_tests.yml' 
DEBU[0000] Reading workflow '/Users/lena/Documents/Knights/Squire/.github/workflows/black-review-dog.yml' 
DEBU[0000] Conditional GET for notices etag=89b55634-e474-4cff-a075-2c41abcfb8c3 
DEBU[0000] Reading workflow '/Users/lena/Documents/Knights/Squire/.github/workflows/black.yml' 
DEBU[0000] Reading workflow '/Users/lena/Documents/Knights/Squire/.github/workflows/run_tests.yml' 
DEBU[0000] Preparing plan with all jobs                 
DEBU[0000] Using the only detected workflow event: pull_request 
DEBU[0000] Planning jobs for event: pull_request        
DEBU[0000] Found revision: d038cc03cd0b075071a2ab001ed226c1b3953225 
DEBU[0000] Found revision: d038cc03cd0b075071a2ab001ed226c1b3953225 
DEBU[0000] Found revision: d038cc03cd0b075071a2ab001ed226c1b3953225 
DEBU[0000] Found revision: d038cc03cd0b075071a2ab001ed226c1b3953225 
DEBU[0000]                                              
DEBU[0000] Found revision: d038cc03cd0b075071a2ab001ed226c1b3953225 
ERRO[0000] failed to obtain container engine info: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? 
[black-reviewdog/lint      ] [DEBUG] evaluating expression 'success()'
[black-reviewdog/lint      ] [DEBUG] expression 'success()' evaluated to 'true'
[black-reviewdog/lint      ] 🚀  Start image=catthehacker/ubuntu:act-latest
[black-reviewdog/lint      ]   🐳  docker pull image=catthehacker/ubuntu:act-latest platform= username= forcePull=true
[black-reviewdog/lint      ] [DEBUG]   🐳  docker pull catthehacker/ubuntu:act-latest
[black-reviewdog/lint      ] [DEBUG] pulling image 'docker.io/catthehacker/ubuntu:act-latest' ()
[Lint Black (official)/lint] [DEBUG] evaluating expression 'success()'
[Lint Black (official)/lint] [DEBUG] expression 'success()' evaluated to 'true'
[Lint Black (official)/lint] 🚀  Start image=catthehacker/ubuntu:act-latest
[Lint Black (official)/lint]   🐳  docker pull image=catthehacker/ubuntu:act-latest platform= username= forcePull=true
[Lint Black (official)/lint] [DEBUG]   🐳  docker pull catthehacker/ubuntu:act-latest
[Lint Black (official)/lint] [DEBUG] pulling image 'docker.io/catthehacker/ubuntu:act-latest' ()
[Test Squire/Test Code     ] [DEBUG] evaluating expression 'success()'
[Test Squire/Test Code     ] [DEBUG] expression 'success()' evaluated to 'true'
[Test Squire/Test Code     ] [DEBUG] expression '${{ matrix.ubuntu }}' rewritten to 'format('{0}', matrix.ubuntu)'
[Test Squire/Test Code     ] [DEBUG] evaluating expression 'format('{0}', matrix.ubuntu)'
[Test Squire/Test Code     ] [DEBUG] expression 'format('{0}', matrix.ubuntu)' evaluated to '%!t(string=ubuntu-20.04)'
[Test Squire/Test Code     ] [DEBUG] expression '${{ matrix.ubuntu }}' rewritten to 'format('{0}', matrix.ubuntu)'
[Test Squire/Test Code     ] [DEBUG] evaluating expression 'format('{0}', matrix.ubuntu)'
[Test Squire/Test Code     ] [DEBUG] expression 'format('{0}', matrix.ubuntu)' evaluated to '%!t(string=ubuntu-20.04)'
[Test Squire/Test Code     ] [DEBUG] expression '${{ matrix.ubuntu }}' rewritten to 'format('{0}', matrix.ubuntu)'
[Test Squire/Test Code     ] [DEBUG] evaluating expression 'format('{0}', matrix.ubuntu)'
[Test Squire/Test Code     ] [DEBUG] expression 'format('{0}', matrix.ubuntu)' evaluated to '%!t(string=ubuntu-20.04)'
[Test Squire/Test Code     ] 🚀  Start image=catthehacker/ubuntu:act-20.04
[Test Squire/Test Code     ]   🐳  docker pull image=catthehacker/ubuntu:act-20.04 platform= username= forcePull=true
[Test Squire/Test Code     ] [DEBUG]   🐳  docker pull catthehacker/ubuntu:act-20.04
[Test Squire/Test Code     ] [DEBUG] pulling image 'docker.io/catthehacker/ubuntu:act-20.04' ()
Error: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
```


### Additional information

(On a repo with GitHub workflow actions, this one: https://github.com/LenaWil/Squire/commit/d038cc03cd0b075071a2ab001ed226c1b3953225 )

There are more workflows, and looking at the debug log it might run some of them? But without verbose mode it prints two error messages.


Discussion / hints:
I don't think this is necessarily an act specific problem, but I've got around this by doing the following:
```
ln -s ~/.docker/run/docker.sock /var/run/docker.sock
```

This isn't guaranteed to fix your problem long-term. However it looks like an upgrade to a  `v4.17.0` of docker for mac has meant docker has moved away from using this global .sock file
> `ln -s ~/.docker/run/docker.sock /var/run/docker.sock`

That worked, thanks!  

Thank you @culshaw.
I got into the problem too. I use ubuntu, how can I solve it?
> I got into the problem too. I use ubuntu, how can I solve it?

So do I (fedora) 
I also have this failure on Ubuntu-22.04:
```
$ docker --version
Docker version 23.0.2, build 569dd73
```

```
gh act -v
DEBU[0000] Loading environment from /home/kintel/code/OpenGL/offscreen/.env 
DEBU[0000] Loading action inputs from /home/kintel/code/OpenGL/offscreen/.input 
DEBU[0000] Loading secrets from /home/kintel/code/OpenGL/offscreen/.secrets 
DEBU[0000] Evaluated matrix inclusions: map[]           
DEBU[0000] Loading workflows from '/home/kintel/code/OpenGL/offscreen/.github/workflows' 
DEBU[0000] Loading workflows recursively                
DEBU[0000] Found workflow 'cmake.yml' in '/home/kintel/code/OpenGL/offscreen/.github/workflows/cmake.yml' 
DEBU[0000] Reading workflow '/home/kintel/code/OpenGL/offscreen/.github/workflows/cmake.yml' 
DEBU[0000] Conditional GET for notices etag=f82d34a0-80c7-4533-902c-b80db8d0947f 
DEBU[0000] Preparing plan with all jobs                 
DEBU[0000] Using default workflow event: push           
DEBU[0000] Planning jobs for event: push                
DEBU[0000] Final matrix after applying user inclusions '[map[]]' 
DEBU[0000] Loading revision from git directory          
DEBU[0000] Found revision: 7eff393d11d435b16ddc48cbdd643a7bfdab2e74 
DEBU[0000] HEAD points to '7eff393d11d435b16ddc48cbdd643a7bfdab2e74' 
DEBU[0000] using github ref: refs/heads/master          
DEBU[0000] Found revision: 7eff393d11d435b16ddc48cbdd643a7bfdab2e74 
ERRO[0000] failed to obtain container engine info: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/info": dial unix /var/run/docker.sock: connect: permission denied 
[CMake/build] [DEBUG] evaluating expression 'success()'
[CMake/build] [DEBUG] expression 'success()' evaluated to 'true'
[CMake/build] 🚀  Start image=catthehacker/ubuntu:act-latest
[CMake/build]   🐳  docker pull image=catthehacker/ubuntu:act-latest platform= username= forcePull=true
[CMake/build] [DEBUG]   🐳  docker pull catthehacker/ubuntu:act-latest
[CMake/build] [DEBUG] pulling image 'docker.io/catthehacker/ubuntu:act-latest' ()
Error: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/images/create?fromImage=catthehacker%2Fubuntu&tag=act-latest": dial unix /var/run/docker.sock: connect: permission denied
```

The user running this is added to the `docker` group and can run docker commands without sudo.
Original issue was regarding Docker on macOS, if you have similar problem on non macOS platform, create new issue and attach required info.
See #1744
> Original issue was regarding Docker on macOS, if you have similar problem on non macOS platform, create new issue and attach required info.

Why was this closed? Last time I tested I still had the issue.

I can use the work around, but for some reason I have to do that on every boot.

Fix patch:
diff --git a/cmd/root.go b/cmd/root.go
index 1742e8a1ed9..55b55bb27ee 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -80,7 +80,7 @@ func Execute(ctx context.Context, version string) {
 	rootCmd.PersistentFlags().StringVarP(&input.envfile, "env-file", "", ".env", "environment file to read and use as env in the containers")
 	rootCmd.PersistentFlags().StringVarP(&input.inputfile, "input-file", "", ".input", "input file to read and use as action input")
 	rootCmd.PersistentFlags().StringVarP(&input.containerArchitecture, "container-architecture", "", "", "Architecture which should be used to run containers, e.g.: linux/amd64. If not specified, will use host default architecture. Requires Docker server API Version 1.41+. Ignored on earlier Docker server platforms.")
-	rootCmd.PersistentFlags().StringVarP(&input.containerDaemonSocket, "container-daemon-socket", "", "/var/run/docker.sock", "Path to Docker daemon socket which will be mounted to containers")
+	rootCmd.PersistentFlags().StringVarP(&input.containerDaemonSocket, "container-daemon-socket", "", "", "URI to Docker Engine socket (e.g.: unix://~/.docker/run/docker.sock)")
 	rootCmd.PersistentFlags().StringVarP(&input.containerOptions, "container-options", "", "", "Custom docker container options for the job container without an options property in the job definition")
 	rootCmd.PersistentFlags().StringVarP(&input.githubInstance, "github-instance", "", "github.com", "GitHub instance to use. Don't use this if you are not using GitHub Enterprise Server.")
 	rootCmd.PersistentFlags().StringVarP(&input.artifactServerPath, "artifact-server-path", "", "", "Defines the path where the artifact server stores uploads and retrieves downloads from. If not specified the artifact server will not start.")
@@ -118,6 +118,33 @@ func configLocations() []string {
 	}
 }
 
+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://" + os.ExpandEnv(p), true
+			}
+			return "unix://" + os.ExpandEnv(p), true
+		}
+	}
+
+	return "", false
+}
+
 func args() []string {
 	actrc := configLocations()
 
@@ -131,15 +158,6 @@ func args() []string {
 }
 
 func bugReport(ctx context.Context, version string) error {
-	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",
-	}
-
 	sprintf := func(key, val string) string {
 		return fmt.Sprintf("%-24s%s\n", key, val)
 	}
@@ -150,19 +168,20 @@ func bugReport(ctx context.Context, version string) error {
 	report += sprintf("NumCPU:", fmt.Sprint(runtime.NumCPU()))
 
 	var dockerHost string
-	if dockerHost = os.Getenv("DOCKER_HOST"); dockerHost == "" {
-		dockerHost = "DOCKER_HOST environment variable is unset/empty."
+	var exists bool
+	if dockerHost, exists = os.LookupEnv("DOCKER_HOST"); !exists {
+		dockerHost = "DOCKER_HOST environment variable is not set"
+	} else if dockerHost == "" {
+		dockerHost = "DOCKER_HOST environment variable is empty."
 	}
 
 	report += sprintf("Docker host:", dockerHost)
 	report += fmt.Sprintln("Sockets found:")
 	for _, p := range commonSocketPaths {
-		if strings.HasPrefix(p, `$`) {
-			v := strings.Split(p, `/`)[0]
-			p = strings.Replace(p, v, os.Getenv(strings.TrimPrefix(v, `$`)), 1)
-		}
-		if _, err := os.Stat(p); err != nil {
+		if _, err := os.Lstat(os.ExpandEnv(p)); err != nil {
 			continue
+		} else if _, err := os.Stat(os.ExpandEnv(p)); err != nil {
+			report += fmt.Sprintf("\t%s(broken)\n", p)
 		} else {
 			report += fmt.Sprintf("\t%s\n", p)
 		}
@@ -342,6 +361,19 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
 			return bugReport(ctx, cmd.Version)
 		}
 
+		var socketPath string
+		if input.containerDaemonSocket != "" {
+			socketPath = input.containerDaemonSocket
+		} else {
+			socket, found := socketLocation()
+			if !found && input.containerDaemonSocket == "" {
+				log.Errorln("daemon Docker Engine socket not found and containerDaemonSocket option was not set")
+			} else {
+				socketPath = socket
+			}
+		}
+		os.Setenv("DOCKER_HOST", socketPath)
+
 		if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" && input.containerArchitecture == "" {
 			l := log.New()
 			l.SetFormatter(&log.TextFormatter{
@@ -533,7 +565,7 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
 			Privileged:                         input.privileged,
 			UsernsMode:                         input.usernsMode,
 			ContainerArchitecture:              input.containerArchitecture,
-			ContainerDaemonSocket:              input.containerDaemonSocket,
+			ContainerDaemonSocket:              socketPath,
 			ContainerOptions:                   input.containerOptions,
 			UseGitIgnore:                       input.useGitIgnore,
 			GitHubInstance:                     input.githubInstance,
diff --git a/pkg/container/docker_run.go b/pkg/container/docker_run.go
index 1b194eac9ae..df296e0d083 100644
--- a/pkg/container/docker_run.go
+++ b/pkg/container/docker_run.go
@@ -189,9 +189,6 @@ type containerReference struct {
 }
 
 func GetDockerClient(ctx context.Context) (cli client.APIClient, err error) {
-	// TODO: this should maybe need to be a global option, not hidden in here?
-	//       though i'm not sure how that works out when there's another Executor :D
-	//		 I really would like something that works on OSX native for eg
 	dockerHost := os.Getenv("DOCKER_HOST")
 
 	if strings.HasPrefix(dockerHost, "ssh://") {


Test patch: