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

fix: Use ExtraHosts only for linux machines with native docker engine #461

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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