org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of testcontainers Show documentation
Show all versions of testcontainers Show documentation
Isolated container management for Java code testing
package org.testcontainers.dockerclient;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.core.DockerClientConfig;
/**
* Use environment variables and system properties (as supported by the underlying DockerClient DefaultConfigBuilder)
* to try and locate a docker environment.
*/
public class EnvironmentAndSystemPropertyClientProviderStrategy extends DockerClientProviderStrategy {
@Override
public void test() throws InvalidConfigurationException {
try {
// Try using environment variables
config = DockerClientConfig.createDefaultConfigBuilder().build();
DockerClient client = getClientForConfig(config);
ping(client, 1);
} catch (Exception e) {
throw new InvalidConfigurationException("ping failed");
}
LOGGER.info("Found docker client settings from environment");
LOGGER.info("Docker host IP address is {}", DockerClientConfigUtils.getDockerHostIpAddress(config));
}
@Override
public String getDescription() {
return "Environment variables, system properties and defaults. Resolved: \n" + stringRepresentation(config);
}
private String stringRepresentation(DockerClientConfig config) {
if (config == null) {
return "";
}
return " dockerHost=" + config.getDockerHost() + "\n" +
" dockerCertPath='" + config.getDockerCertPath() + "'\n" +
" dockerTlsVerify='" + config.getDockerTlsVerify() + "'\n" +
" apiVersion='" + config.getApiVersion() + "'\n" +
" registryUrl='" + config.getRegistryUrl() + "'\n" +
" registryUsername='" + config.getRegistryUsername() + "'\n" +
" registryPassword='" + config.getRegistryPassword() + "'\n" +
" registryEmail='" + config.getRegistryEmail() + "'\n" +
" dockerConfig='" + config.getDockerConfig() + "'\n";
}
}