Use Docker CLI context to determine daemon host address for image building

Configuration files managed by the Docker CLI are now used to determine
the host address of the Docker daemon used when building images using
buildpacks when a host address is not configured with environment
variables or build tool plugin configuration.

Closes gh-36445
This commit is contained in:
Scott Frederick
2023-07-13 14:27:14 -06:00
parent 283dc37db3
commit 4393a2244c
26 changed files with 660 additions and 86 deletions

View File

@@ -28,7 +28,8 @@ The `spring-boot-devtools` and `spring-boot-docker-compose` modules are automati
[[build-image.docker-daemon]]
== Docker Daemon
The `build-image` goal requires access to a Docker daemon.
By default, it will communicate with a Docker daemon over a local connection.
The goal will inspect local Docker CLI https://docs.docker.com/engine/reference/commandline/cli/#configuration-files[configuration files] to determine the current https://docs.docker.com/engine/context/working-with-contexts/[context] and use the context connection information to communicate with a Docker daemon.
If the current context can not be determined or the context does not have connection information, then the goal will use a default local connection.
This works with https://docs.docker.com/install/[Docker Engine] on all supported platforms without configuration.
Environment variables can be set to configure the `build-image` goal to use an alternative local or remote connection.
@@ -37,6 +38,12 @@ The following table shows the environment variables and their values:
|===
| Environment variable | Description
| DOCKER_CONFIG
| Location of Docker CLI https://docs.docker.com/engine/reference/commandline/cli/#configuration-files[configuration files] used to determine the current context (defaults to `$HOME/.docker`)
| DOCKER_CONTEXT
| Name of a https://docs.docker.com/engine/context/working-with-contexts/[context] that should be used to retrieve host information from Docker CLI configuration files (overrides `DOCKER_HOST`)
| DOCKER_HOST
| URL containing the host and port for the Docker daemon - for example `tcp://192.168.99.100:2376`
@@ -53,6 +60,9 @@ The following table summarizes the available parameters:
|===
| Parameter | Description
| `context`
| Name of a https://docs.docker.com/engine/context/working-with-contexts/[context] that should be used to retrieve host information from Docker CLI https://docs.docker.com/engine/reference/commandline/cli/#configuration-files[configuration files]
| `host`
| URL containing the host and port for the Docker daemon - for example `tcp://192.168.99.100:2376`

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,6 +29,8 @@ public class Docker {
private String host;
private String context;
private boolean tlsVerify;
private String certPath;
@@ -51,6 +53,18 @@ public class Docker {
this.host = host;
}
/**
* The Docker context to use to retrieve host configuration.
* @return the Docker context
*/
public String getContext() {
return this.context;
}
public void setContext(String context) {
this.context = context;
}
/**
* Whether the Docker daemon requires TLS communication.
* @return {@code true} to enable TLS
@@ -138,6 +152,13 @@ public class Docker {
}
private DockerConfiguration customizeHost(DockerConfiguration dockerConfiguration) {
if (this.context != null && this.host != null) {
throw new IllegalArgumentException(
"Invalid Docker configuration, either context or host can be provided but not both");
}
if (this.context != null) {
return dockerConfiguration.withContext(this.context);
}
if (this.host != null) {
return dockerConfiguration.withHost(this.host, this.tlsVerify, this.certPath);
}

View File

@@ -21,7 +21,7 @@ import java.util.Base64;
import org.junit.jupiter.api.Test;
import org.springframework.boot.buildpack.platform.docker.configuration.DockerConfiguration;
import org.springframework.boot.buildpack.platform.docker.configuration.DockerHost;
import org.springframework.boot.buildpack.platform.docker.configuration.DockerConfiguration.DockerHostConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -54,10 +54,11 @@ class DockerTests {
docker.setTlsVerify(true);
docker.setCertPath("/tmp/ca-cert");
DockerConfiguration dockerConfiguration = docker.asDockerConfiguration();
DockerHost host = dockerConfiguration.getHost();
DockerHostConfiguration host = dockerConfiguration.getHost();
assertThat(host.getAddress()).isEqualTo("docker.example.com");
assertThat(host.isSecure()).isTrue();
assertThat(host.getCertificatePath()).isEqualTo("/tmp/ca-cert");
assertThat(host.getContext()).isNull();
assertThat(dockerConfiguration.isBindHostToBuilder()).isFalse();
assertThat(docker.asDockerConfiguration().getBuilderRegistryAuthentication()).isNull();
assertThat(decoded(dockerConfiguration.getPublishRegistryAuthentication().getAuthHeader()))
@@ -67,6 +68,34 @@ class DockerTests {
.contains("\"serveraddress\" : \"\"");
}
@Test
void asDockerConfigurationWithContextConfiguration() {
Docker docker = new Docker();
docker.setContext("test-context");
DockerConfiguration dockerConfiguration = docker.asDockerConfiguration();
DockerHostConfiguration host = dockerConfiguration.getHost();
assertThat(host.getContext()).isEqualTo("test-context");
assertThat(host.getAddress()).isNull();
assertThat(host.isSecure()).isFalse();
assertThat(host.getCertificatePath()).isNull();
assertThat(dockerConfiguration.isBindHostToBuilder()).isFalse();
assertThat(docker.asDockerConfiguration().getBuilderRegistryAuthentication()).isNull();
assertThat(decoded(dockerConfiguration.getPublishRegistryAuthentication().getAuthHeader()))
.contains("\"username\" : \"\"")
.contains("\"password\" : \"\"")
.contains("\"email\" : \"\"")
.contains("\"serveraddress\" : \"\"");
}
@Test
void asDockerConfigurationWithHostAndContextFails() {
Docker docker = new Docker();
docker.setContext("test-context");
docker.setHost("docker.example.com");
assertThatIllegalArgumentException().isThrownBy(docker::asDockerConfiguration)
.withMessageContaining("Invalid Docker configuration");
}
@Test
void asDockerConfigurationWithBindHostToBuilder() {
Docker docker = new Docker();
@@ -75,7 +104,7 @@ class DockerTests {
docker.setCertPath("/tmp/ca-cert");
docker.setBindHostToBuilder(true);
DockerConfiguration dockerConfiguration = docker.asDockerConfiguration();
DockerHost host = dockerConfiguration.getHost();
DockerHostConfiguration host = dockerConfiguration.getHost();
assertThat(host.getAddress()).isEqualTo("docker.example.com");
assertThat(host.isSecure()).isTrue();
assertThat(host.getCertificatePath()).isEqualTo("/tmp/ca-cert");