|
28 | 28 | ErrSocketNotFoundInPath = errors.New("docker socket not found in " + DockerSocketPath) |
29 | 29 | // ErrTestcontainersHostNotSetInProperties this error is specific to Testcontainers |
30 | 30 | ErrTestcontainersHostNotSetInProperties = errors.New("tc.host not set in ~/.testcontainers.properties") |
| 31 | + ErrDockerSocketNotSetInDockerContext = errors.New("socket not found in docker context") |
31 | 32 | ) |
32 | 33 |
|
33 | 34 | var ( |
@@ -73,7 +74,7 @@ var dockerHostCheck = func(ctx context.Context, host string) error { |
73 | 74 | return nil |
74 | 75 | } |
75 | 76 |
|
76 | | -// MustExtractDockerHost Extracts the docker host from the different alternatives, caching the result to avoid unnecessary |
| 77 | +// MustExtractDockerHost extracts the docker host from the different alternatives, caching the result to avoid unnecessary |
77 | 78 | // calculations. Use this function to get the actual Docker host. This function does not consider Windows containers at the moment. |
78 | 79 | // The possible alternatives are: |
79 | 80 | // |
@@ -118,13 +119,26 @@ func MustExtractDockerSocket(ctx context.Context) string { |
118 | 119 | return dockerSocketPathCache |
119 | 120 | } |
120 | 121 |
|
121 | | -// extractDockerHost Extracts the docker host from the different alternatives, without caching the result. |
122 | | -// This internal method is handy for testing purposes. |
| 122 | +// ExtractDockerHost Extracts the docker host from the different alternatives, without caching the result. |
| 123 | +// Use this function to get the actual Docker host. This function does not consider Windows containers at the moment. |
| 124 | +// The possible alternatives are: |
| 125 | +// |
| 126 | +// 1. Docker host from the "tc.host" property in the ~/.testcontainers.properties file. |
| 127 | +// 2. DOCKER_HOST environment variable. |
| 128 | +// 3. Docker host from context. |
| 129 | +// 4. Docker host from the default docker socket path, without the unix schema. |
| 130 | +// 5. Docker host from the "docker.host" property in the ~/.testcontainers.properties file. |
| 131 | +// 6. Rootless docker socket path. |
| 132 | +func ExtractDockerHost(ctx context.Context) (string, error) { |
| 133 | + return extractDockerHost(ctx) |
| 134 | +} |
| 135 | + |
123 | 136 | func extractDockerHost(ctx context.Context) (string, error) { |
124 | 137 | dockerHostFns := []func(context.Context) (string, error){ |
125 | 138 | testcontainersHostFromProperties, |
126 | 139 | dockerHostFromEnv, |
127 | 140 | dockerHostFromContext, |
| 141 | + dockerHostFromDockerContext, |
128 | 142 | dockerSocketPath, |
129 | 143 | dockerHostFromProperties, |
130 | 144 | rootlessDockerSocketPath, |
@@ -227,12 +241,14 @@ func isHostNotSet(err error) bool { |
227 | 241 | case errors.Is(err, ErrTestcontainersHostNotSetInProperties), |
228 | 242 | errors.Is(err, ErrDockerHostNotSet), |
229 | 243 | errors.Is(err, ErrDockerSocketNotSetInContext), |
| 244 | + errors.Is(err, ErrDockerSocketNotSetInDockerContext), |
230 | 245 | errors.Is(err, ErrDockerSocketNotSetInProperties), |
231 | 246 | errors.Is(err, ErrSocketNotFoundInPath), |
232 | 247 | errors.Is(err, ErrXDGRuntimeDirNotSet), |
233 | 248 | errors.Is(err, ErrRootlessDockerNotFoundHomeRunDir), |
234 | 249 | errors.Is(err, ErrRootlessDockerNotFoundHomeDesktopDir), |
235 | | - errors.Is(err, ErrRootlessDockerNotFoundRunDir): |
| 250 | + errors.Is(err, ErrRootlessDockerNotFoundRunDir), |
| 251 | + errors.Is(err, ErrRootlessDockerNotFound): |
236 | 252 | return true |
237 | 253 | default: |
238 | 254 | return false |
@@ -262,6 +278,17 @@ func dockerHostFromContext(ctx context.Context) (string, error) { |
262 | 278 | return "", ErrDockerSocketNotSetInContext |
263 | 279 | } |
264 | 280 |
|
| 281 | +// dockerHostFromDockerContext returns the docker host from the DOCKER_CONTEXT environment variable, if it's not empty |
| 282 | +func dockerHostFromDockerContext(_ context.Context) (string, error) { |
| 283 | + // exec `docker context inspect -f='{{.Endpoints.docker.Host}}'` |
| 284 | + cmd := exec.Command("docker", "context", "inspect", "-f", "{{.Endpoints.docker.Host}}") |
| 285 | + output, err := cmd.CombinedOutput() |
| 286 | + if err != nil { |
| 287 | + return "", fmt.Errorf("%w: %w", ErrDockerSocketNotSetInDockerContext, err) |
| 288 | + } |
| 289 | + return strings.TrimSpace(string(output)), nil |
| 290 | +} |
| 291 | + |
265 | 292 | // dockerHostFromProperties returns the docker host from the ~/.testcontainers.properties file, if it's not empty |
266 | 293 | func dockerHostFromProperties(_ context.Context) (string, error) { |
267 | 294 | cfg := config.Read() |
|
0 commit comments