From 09f708b557d50fb27278117b890d87d20139ce4e Mon Sep 17 00:00:00 2001 From: Matheus Alcantara Date: Tue, 9 Nov 2021 10:02:00 -0300 Subject: [PATCH] docker: use env variables to configure docker client 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 --- internal/services/docker/client/docker_config.go | 2 +- internal/services/docker/client/docker_config_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/services/docker/client/docker_config.go b/internal/services/docker/client/docker_config.go index 697955a2c..47de024c2 100644 --- a/internal/services/docker/client/docker_config.go +++ b/internal/services/docker/client/docker_config.go @@ -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) } diff --git a/internal/services/docker/client/docker_config_test.go b/internal/services/docker/client/docker_config_test.go index 4c651b93b..0725d2709 100644 --- a/internal/services/docker/client/docker_config_test.go +++ b/internal/services/docker/client/docker_config_test.go @@ -16,6 +16,7 @@ package client import ( "io" + "os" "strings" "testing" @@ -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) {