Configure Docker host in build plugins

This commit adds the ability to configure the Maven and Gradle
plugins to use a remote Docker daemon using build file
configuration, as an alternative to setting environment variables
to specify remote host connection details.

Fixes gh-23400
This commit is contained in:
Scott Frederick
2020-09-17 14:25:18 -05:00
parent 1c6e37b2ac
commit 54288678d1
18 changed files with 436 additions and 76 deletions

View File

@@ -57,6 +57,24 @@ The following table shows the environment variables and their values:
On Linux and macOS, these environment variables can be set using the command `eval $(minikube docker-env)` after minikube has been started.
Docker daemon connection information can also be provided using `docker` parameters in the plugin configuration.
The following table summarizes the available parameters:
|===
| Parameter | Description
| `host`
| URL containing the host and port for the Docker daemon - e.g. `tcp://192.168.99.100:2376`
| `tlsVerify`
| Enable secure HTTPS protocol when set to `true` (optional)
| `certPath`
| Path to certificate and key files for HTTPS (required if `tlsVerify` is `true`, ignored otherwise)
|===
For more details, see also <<build-image-example-docker,examples>>.
[[build-image-docker-registry]]
@@ -287,7 +305,31 @@ The image name can be specified on the command line as well, as shown in this ex
[[build-image-example-docker]]
==== Docker Configuration
If the builder or run image are stored in a private Docker registry that supports user authentication, authentication details can be provided as shown in the following example:
If you need the plugin to communicate with the Docker daemon using a remote connection instead of the default local connection, the connection details can be provided using `docker` parameters as shown in the following example:
[source,xml,indent=0,subs="verbatim,attributes"]
----
<project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>{gradle-project-version}</version>
<configuration>
<docker>
<host>tcp://192.168.99.100:2376</host>
<tlsVerify>true</tlsVerify>
<certPath>/home/user/.minikube/certs</certPath>
</docker>
</configuration>
</plugin>
</plugins>
</build>
</project>
----
If the builder or run image are stored in a private Docker registry that supports user authentication, authentication details can be provided using `docker.registry` parameters as shown in the following example:
[source,xml,indent=0,subs="verbatim,attributes"]
----
@@ -314,7 +356,7 @@ If the builder or run image are stored in a private Docker registry that support
</project>
----
If the builder or run image is stored in a private Docker registry that supports token authentication, the token value can be provided as shown in the following example:
If the builder or run image is stored in a private Docker registry that supports token authentication, the token value can be provided using `docker.registry` parameters as shown in the following example:
[source,xml,indent=0,subs="verbatim,attributes"]
----

View File

@@ -27,8 +27,38 @@ import org.springframework.boot.buildpack.platform.docker.configuration.DockerCo
*/
public class Docker {
private String host;
private boolean tlsVerify;
private String certPath;
private DockerRegistry registry;
public String getHost() {
return this.host;
}
public void setHost(String host) {
this.host = host;
}
public boolean isTlsVerify() {
return this.tlsVerify;
}
public void setTlsVerify(boolean tlsVerify) {
this.tlsVerify = tlsVerify;
}
public String getCertPath() {
return this.certPath;
}
public void setCertPath(String certPath) {
this.certPath = certPath;
}
/**
* Sets the {@link DockerRegistry} that configures registry authentication.
* @param registry the registry configuration
@@ -44,17 +74,30 @@ public class Docker {
* @return the Docker configuration
*/
DockerConfiguration asDockerConfiguration() {
DockerConfiguration dockerConfiguration = new DockerConfiguration();
dockerConfiguration = customizeHost(dockerConfiguration);
dockerConfiguration = customizeAuthentication(dockerConfiguration);
return dockerConfiguration;
}
private DockerConfiguration customizeHost(DockerConfiguration dockerConfiguration) {
if (this.host != null) {
return dockerConfiguration.withHost(this.host, this.tlsVerify, this.certPath);
}
return dockerConfiguration;
}
private DockerConfiguration customizeAuthentication(DockerConfiguration dockerConfiguration) {
if (this.registry == null || this.registry.isEmpty()) {
return null;
return dockerConfiguration;
}
if (this.registry.hasTokenAuth() && !this.registry.hasUserAuth()) {
return DockerConfiguration.withRegistryTokenAuthentication(this.registry.getToken());
return dockerConfiguration.withRegistryTokenAuthentication(this.registry.getToken());
}
if (this.registry.hasUserAuth() && !this.registry.hasTokenAuth()) {
return DockerConfiguration.withRegistryUserAuthentication(this.registry.getUsername(),
return dockerConfiguration.withRegistryUserAuthentication(this.registry.getUsername(),
this.registry.getPassword(), this.registry.getUrl(), this.registry.getEmail());
}
throw new IllegalArgumentException(
"Invalid Docker registry configuration, either token or username/password must be provided");
}

View File

@@ -19,6 +19,7 @@ package org.springframework.boot.maven;
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.DockerRegistryAuthentication;
import org.springframework.util.Base64Utils;
@@ -34,17 +35,24 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
public class DockerTests {
@Test
void asDockerConfigurationWithoutRegistry() {
void asDockerConfigurationWithDefaults() {
Docker docker = new Docker();
assertThat(docker.asDockerConfiguration()).isNull();
assertThat(docker.asDockerConfiguration().getHost()).isNull();
assertThat(docker.asDockerConfiguration().getRegistryAuthentication()).isNull();
}
@Test
void asDockerConfigurationWithEmptyRegistry() {
Docker.DockerRegistry dockerRegistry = new Docker.DockerRegistry();
void asDockerConfigurationWithHostConfiguration() {
Docker docker = new Docker();
docker.setRegistry(dockerRegistry);
assertThat(docker.asDockerConfiguration()).isNull();
docker.setHost("docker.example.com");
docker.setTlsVerify(true);
docker.setCertPath("/tmp/ca-cert");
DockerConfiguration dockerConfiguration = docker.asDockerConfiguration();
DockerHost host = dockerConfiguration.getHost();
assertThat(host.getAddress()).isEqualTo("docker.example.com");
assertThat(host.isSecure()).isEqualTo(true);
assertThat(host.getCertificatePath()).isEqualTo("/tmp/ca-cert");
assertThat(docker.asDockerConfiguration().getRegistryAuthentication()).isNull();
}
@Test