Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
fix: Use ExtraHosts only for linux machines with native docker engine
Browse files Browse the repository at this point in the history
Signed-off-by: Chi-Sheng Liu <[email protected]>
  • Loading branch information
MortalHappiness committed Feb 15, 2024
1 parent 9c87165 commit 7c83a52
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
16 changes: 15 additions & 1 deletion pkg/docker/docker_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"runtime"
"strings"

"github.com/docker/docker/client"
Expand Down Expand Up @@ -60,6 +62,18 @@ var (
ExtraHosts = []string{"host.docker.internal:host-gateway"}
)

// GetExtraHosts will return extra hosts for the container
func GetExtraHosts() []string {
// Linux machine with native docker engine does not have host.docker.internal and needs to add the hostname mapping manually.
if runtime.GOOS == "linux" {
// Here assume that if /var/run/docker.sock exists and is a socket file and is not symbolic link, then the docker engine is native
if fileInfo, err := os.Lstat("/var/run/docker.sock"); err == nil && fileInfo.Mode().Type() == fs.ModeSocket {
return ExtraHosts
}
}
return []string{}
}

// GetDockerClient will returns the docker client
func GetDockerClient() (Docker, error) {
if Client == nil {
Expand Down Expand Up @@ -266,7 +280,7 @@ func StartContainer(ctx context.Context, cli Docker, volumes []mount.Mount, expo
Mounts: volumes,
PortBindings: portBindings,
Privileged: true,
ExtraHosts: ExtraHosts, // add it because linux machine doesn't have this host name by default
ExtraHosts: GetExtraHosts(),
}, nil,
nil, name)

Expand Down
8 changes: 4 additions & 4 deletions pkg/docker/docker_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func TestStartContainer(t *testing.T) {
Mounts: Volumes,
PortBindings: p2,
Privileged: true,
ExtraHosts: ExtraHosts,
ExtraHosts: GetExtraHosts(),
}, nil, nil, mock.Anything).Return(container.ContainerCreateCreatedBody{
ID: "Hello",
}, nil)
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestStartContainer(t *testing.T) {
Mounts: Volumes,
PortBindings: p2,
Privileged: true,
ExtraHosts: ExtraHosts,
ExtraHosts: GetExtraHosts(),
}, nil, nil, mock.Anything).Return(container.ContainerCreateCreatedBody{
ID: "Hello",
}, nil)
Expand All @@ -253,7 +253,7 @@ func TestStartContainer(t *testing.T) {
Mounts: Volumes,
PortBindings: p2,
Privileged: true,
ExtraHosts: ExtraHosts,
ExtraHosts: GetExtraHosts(),
}, nil, nil, mock.Anything).Return(container.ContainerCreateCreatedBody{
ID: "",
}, fmt.Errorf("error"))
Expand All @@ -279,7 +279,7 @@ func TestStartContainer(t *testing.T) {
Mounts: Volumes,
PortBindings: p2,
Privileged: true,
ExtraHosts: ExtraHosts,
ExtraHosts: GetExtraHosts(),
}, nil, nil, mock.Anything).Return(container.ContainerCreateCreatedBody{
ID: "Hello",
}, nil)
Expand Down

0 comments on commit 7c83a52

Please sign in to comment.