Skip to content

Commit

Permalink
docker: use env variables to configure docker client
Browse files Browse the repository at this point in the history
Previously when an user use a different DOCKER_HOST we was not able to
connect because we was not reading the environment variables.

This commit change the docker client initialization to read
configuration from environment variables if they exists.

Fixes #525

Signed-off-by: Matheus Alcantara <matheus.alcantara@zup.com.br>
  • Loading branch information
matheusalcantarazup committed Nov 9, 2021
1 parent b79058f commit 09f708b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/services/docker/client/docker_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

func NewDockerClient() *docker.Client {
dockerClient, err := docker.NewClientWithOpts(docker.WithAPIVersionNegotiation())
dockerClient, err := docker.NewClientWithOpts(docker.FromEnv, docker.WithAPIVersionNegotiation())
if err != nil {
logger.LogPanicWithLevel(messages.MsgPanicNotConnectDocker, err)
}
Expand Down
10 changes: 10 additions & 0 deletions internal/services/docker/client/docker_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package client

import (
"io"
"os"
"strings"
"testing"

Expand All @@ -30,6 +31,15 @@ func TestNewDockerAPI(t *testing.T) {
NewDockerClient()
})
})

t.Run("Should use docker host from environment variable", func(t *testing.T) {
dockerHost := "unix:///path/docker/socket"
os.Setenv("DOCKER_HOST", dockerHost)

c := NewDockerClient()

assert.Equal(t, dockerHost, c.DaemonHost())
})
}

func TestMock(t *testing.T) {
Expand Down

0 comments on commit 09f708b

Please sign in to comment.