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

Commit f88817d

Browse files
#type(scope): description
#WARNING: Cannot parse issue or ticket number from branch name #Signed-off-by: Chi-Sheng Liu <[email protected]> # ======================================== # type: # - build # - chore # - ci # - docs # - feat # - fix # - perf # - refactor # - revert # - style # - test
1 parent 9c87165 commit f88817d

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

pkg/docker/docker_util.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"errors"
77
"fmt"
88
"io"
9+
"io/fs"
910
"os"
11+
"runtime"
1012
"strings"
1113

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

65+
// GetExtraHosts will return extra hosts for the container
66+
func GetExtraHosts() []string {
67+
// Linux machine with native docker engine does not have host.docker.internal and needs to add the hostname mapping manually.
68+
if runtime.GOOS == "linux" {
69+
// 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
70+
if fileInfo, err := os.Lstat("/var/run/docker.sock"); err == nil && fileInfo.Mode().Type() == fs.ModeSocket {
71+
return ExtraHosts
72+
}
73+
}
74+
return []string{}
75+
}
76+
6377
// GetDockerClient will returns the docker client
6478
func GetDockerClient() (Docker, error) {
6579
if Client == nil {
@@ -266,7 +280,7 @@ func StartContainer(ctx context.Context, cli Docker, volumes []mount.Mount, expo
266280
Mounts: volumes,
267281
PortBindings: portBindings,
268282
Privileged: true,
269-
ExtraHosts: ExtraHosts, // add it because linux machine doesn't have this host name by default
283+
ExtraHosts: GetExtraHosts(),
270284
}, nil,
271285
nil, name)
272286

pkg/docker/docker_util_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func TestStartContainer(t *testing.T) {
196196
Mounts: Volumes,
197197
PortBindings: p2,
198198
Privileged: true,
199-
ExtraHosts: ExtraHosts,
199+
ExtraHosts: GetExtraHosts(),
200200
}, nil, nil, mock.Anything).Return(container.ContainerCreateCreatedBody{
201201
ID: "Hello",
202202
}, nil)
@@ -226,7 +226,7 @@ func TestStartContainer(t *testing.T) {
226226
Mounts: Volumes,
227227
PortBindings: p2,
228228
Privileged: true,
229-
ExtraHosts: ExtraHosts,
229+
ExtraHosts: GetExtraHosts(),
230230
}, nil, nil, mock.Anything).Return(container.ContainerCreateCreatedBody{
231231
ID: "Hello",
232232
}, nil)
@@ -253,7 +253,7 @@ func TestStartContainer(t *testing.T) {
253253
Mounts: Volumes,
254254
PortBindings: p2,
255255
Privileged: true,
256-
ExtraHosts: ExtraHosts,
256+
ExtraHosts: GetExtraHosts(),
257257
}, nil, nil, mock.Anything).Return(container.ContainerCreateCreatedBody{
258258
ID: "",
259259
}, fmt.Errorf("error"))
@@ -279,7 +279,7 @@ func TestStartContainer(t *testing.T) {
279279
Mounts: Volumes,
280280
PortBindings: p2,
281281
Privileged: true,
282-
ExtraHosts: ExtraHosts,
282+
ExtraHosts: GetExtraHosts(),
283283
}, nil, nil, mock.Anything).Return(container.ContainerCreateCreatedBody{
284284
ID: "Hello",
285285
}, nil)

0 commit comments

Comments
 (0)