Java Code Examples for com.github.dockerjava.core.DefaultDockerClientConfig#Builder

The following examples show how to use com.github.dockerjava.core.DefaultDockerClientConfig#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DockerOpt.java    From dew with Apache License 2.0 6 votes vote down vote up
/**
 * Instantiates a new Docker opt.
 *
 * @param log              日志对象
 * @param host             DOCKER_HOST, e.g. tcp://10.200.131.182:2375
 * @param registryUrl      registry地址, e.g. https://harbor.dew.env/v2
 * @param registryUsername registry用户名
 * @param registryPassword registry密码
 * @see <a href="https://docs.docker.com/install/linux/linux-postinstall/#configure-where-the-docker-daemon-listens-for-connections">The Docker Daemon Listens For Connections</a>
 */
protected DockerOpt(Logger log, String host, String registryUrl, String registryUsername, String registryPassword) {
    this.log = log;
    this.registryUsername = registryUsername;
    this.registryPassword = registryPassword;
    DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder();
    if (host != null && !host.isEmpty()) {
        builder.withDockerHost(host);
    }
    if (registryUrl != null) {
        registryUrl = registryUrl.endsWith("/") ? registryUrl.substring(0, registryUrl.length() - 1) : registryUrl;
        registryApiUrl = registryUrl.substring(0, registryUrl.lastIndexOf("/") + 1) + "api/v2.0";
        defaultAuthConfig = new AuthConfig()
                .withRegistryAddress(registryUrl)
                .withUsername(registryUsername)
                .withPassword(registryPassword);
    }
    docker = DockerClientBuilder.getInstance(builder.build()).build();
}
 
Example 2
Source File: DockerHandler.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Initialize the docker client. 
 * 
 * @return an error message if there were issues, <code>null</code> if everyhtin gwent smooth.
 */
public String initDocker() {
    try {
        DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder();
        if (OsCheck.getOperatingSystemType() == OSType.Windows) {
            builder.withDockerHost("tcp://localhost:2375");
        }
        dockerClient = DockerClientBuilder.getInstance(builder).build();

        dockerClient.versionCmd().exec();
    } catch (Exception e) {
        String msg = MSG_NOTRUNNING;
        if (OsCheck.getOperatingSystemType() == OSType.Windows) {
            msg += "\n" + MSG_WIN_SOCKET;
        }
        return msg;
    }

    return null;
}
 
Example 3
Source File: DockerClientFactory.java    From Core with MIT License 5 votes vote down vote up
private static DockerClient get(
        String host,
        Boolean tlsVerify,
        String certPath,
        String registryUsername,
        String registryPass,
        String registryMail,
        String registryUrl
) {
    DefaultDockerClientConfig.Builder configBuilder = DefaultDockerClientConfig.createDefaultConfigBuilder()
            .withDockerHost(host);

    configBuilder.withDockerTlsVerify(tlsVerify);

    if (!certPath.equals("")) {
        configBuilder.withDockerCertPath(certPath);
    }

    if(!registryUrl.equals("")) {
        configBuilder.withRegistryUrl(registryUrl);
        if (!registryUsername.equals("")) {
            configBuilder.withRegistryUsername(registryUsername);
        }

        if (!registryMail.equals("")) {
            configBuilder.withRegistryEmail(registryMail);
        }

        if (!registryPass.equals("")) {
            configBuilder.withRegistryPassword(registryPass);
        }
    }

    DockerClientConfig config = configBuilder.build();

    return DockerClientBuilder.getInstance(config).build();
}
 
Example 4
Source File: DockerTestUtils.java    From module-ballerina-docker with Apache License 2.0 5 votes vote down vote up
public static DockerClient getDockerClient() {
    DefaultDockerClientConfig.Builder dockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder();
    // if windows, consider DOCKER_HOST as "tcp://localhost:2375"
    if (System.getProperty("os.name").toLowerCase(Locale.getDefault()).contains("win")) {
        dockerClientConfig.withDockerHost("tcp://localhost:2375");
    }
    return DockerClientBuilder.getInstance(dockerClientConfig.build()).build();
}
 
Example 5
Source File: DockerClientOperation.java    From redis-manager with Apache License 2.0 5 votes vote down vote up
/**
 * Get docker client
 *
 * @param ip
 * @return
 */
public DockerClient getDockerClient(String ip) {
    DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder()
            .withDockerHost(String.format(dockerHost, ip));
    DefaultDockerClientConfig config = builder.build();
    DockerClient dockerClient = DockerClientBuilder.getInstance(config).withDockerCmdExecFactory(dockerCmdExecFactory).build();
    return dockerClient;
}
 
Example 6
Source File: DockerClientFactory.java    From minimesos with Apache License 2.0 5 votes vote down vote up
public static DockerClient build() {
    if (dockerClient == null) {
        DefaultDockerClientConfig.Builder builder = new DefaultDockerClientConfig.Builder();
        builder = builder.withApiVersion("1.12");

        String dockerHostEnv = System.getenv("DOCKER_HOST");
        if (StringUtils.isBlank(dockerHostEnv)) {
            builder.withDockerHost("unix:///var/run/docker.sock");
        }

        DockerClientConfig config = builder.build();
        dockerClient = DockerClientBuilder.getInstance(config).build();
    }
    return dockerClient;
}
 
Example 7
Source File: DockerAPI.java    From docker-plugin with MIT License 5 votes vote down vote up
/**
 * Creates a new {@link DockerClient}.
 * It's the caller's responsibility to dispose of the result.
 */
@SuppressWarnings("resource")
private static SharableDockerClient makeClient(final String dockerUri, final String credentialsId,
        final Integer readTimeoutInMillisecondsOrNull, final Integer connectTimeoutInMillisecondsOrNull) {
    NettyDockerCmdExecFactory cmdExecFactory = null;
    DockerClient actualClient = null;
    try {
        cmdExecFactory = new NettyDockerCmdExecFactory()
                .withReadTimeout(readTimeoutInMillisecondsOrNull)
                .withConnectTimeout(connectTimeoutInMillisecondsOrNull);
        final DefaultDockerClientConfig.Builder configBuilder = new DefaultDockerClientConfig.Builder()
                .withDockerHost(dockerUri)
                .withCustomSslConfig(toSSlConfig(credentialsId));
        actualClient = DockerClientBuilder.getInstance(configBuilder)
            .withDockerCmdExecFactory(cmdExecFactory)
            .build();
        final SharableDockerClient multiUsageClient = new SharableDockerClient(actualClient);
        // if we've got this far, we're going to succeed, so we need to ensure that we
        // don't close the resources we're returning.
        cmdExecFactory = null;
        actualClient = null;
        return multiUsageClient;
    } finally {
        // these will no-op if we're successfully returning a value, but in any error
        // cases we should ensure that we don't leak precious resources.
        closeAndLogAnyExceptions(cmdExecFactory);
        closeAndLogAnyExceptions(actualClient);
    }
}
 
Example 8
Source File: DockerClientLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenCreatingDockerClient_thenReturnDefaultInstance() {

    // when
    DefaultDockerClientConfig.Builder config = DefaultDockerClientConfig.createDefaultConfigBuilder();
    DockerClient dockerClient = DockerClientBuilder.getInstance(config).build();

    // then
    assertNotNull(dockerClient);
}
 
Example 9
Source File: KubernetesTestUtils.java    From module-ballerina-kubernetes with Apache License 2.0 4 votes vote down vote up
public static DockerClient getDockerClient() {
    DefaultDockerClientConfig.Builder dockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder();
    return DockerClientBuilder.getInstance(dockerClientConfig.build()).build();
}
 
Example 10
Source File: KnativeTestUtils.java    From module-ballerina-kubernetes with Apache License 2.0 4 votes vote down vote up
public static DockerClient getDockerClient() {
    DefaultDockerClientConfig.Builder dockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder();
    return DockerClientBuilder.getInstance(dockerClientConfig.build()).build();
}
 
Example 11
Source File: DockerArtifactHandler.java    From module-ballerina-docker with Apache License 2.0 4 votes vote down vote up
private DockerClient createClient() {
    DefaultDockerClientConfig.Builder dockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder();

    // if windows, consider DOCKER_HOST as "tcp://localhost:2375"
    if (System.getProperty("os.name").toLowerCase(Locale.getDefault()).contains("win")) {
        dockerClientConfig.withDockerHost("tcp://localhost:2375");
    }

    // set docker host
    if (null != this.dockerModel.getDockerHost()) {
        dockerClientConfig.withDockerHost(this.dockerModel.getDockerHost());
    }

    // set docker cert path
    if (null != this.dockerModel.getDockerCertPath()) {
        dockerClientConfig.withDockerCertPath(this.dockerModel.getDockerCertPath());
    }

    // set docker API version
    if (null != this.dockerModel.getDockerAPIVersion()) {
        dockerClientConfig.withApiVersion(this.dockerModel.getDockerAPIVersion());
    }

    // set docker registry url
    if (null != this.dockerModel.getRegistry()) {
        dockerClientConfig.withRegistryUrl(this.dockerModel.getRegistry());
    }

    // set docker registry username
    if (null != this.dockerModel.getUsername()) {
        dockerClientConfig.withRegistryUsername(this.dockerModel.getUsername());
    }

    // set docker registry password
    if (null != this.dockerModel.getPassword()) {
        dockerClientConfig.withRegistryPassword(this.dockerModel.getPassword());
    }

    if (null != this.dockerModel.getDockerConfig()) {
        dockerClientConfig.withDockerConfig(dockerModel.getDockerConfig());
    }

    this.dockerClientConfig = dockerClientConfig.build();
    printDebug("docker client host: " + this.dockerClientConfig.getDockerHost());

    if (!this.dockerClientConfig.getApiVersion().equals(RemoteApiVersion.unknown())) {
        printDebug("docker client API version: " + this.dockerClientConfig.getApiVersion().getVersion());
    } else {
        printDebug("docker client API version: not-set");
    }

    if (null != this.dockerClientConfig.getSSLConfig() &&
            this.dockerClientConfig.getSSLConfig() instanceof LocalDirectorySSLConfig) {
        LocalDirectorySSLConfig sslConfig = (LocalDirectorySSLConfig) this.dockerClientConfig.getSSLConfig();
        printDebug("docker client certs path: " + sslConfig.getDockerCertPath());
        printDebug("docker client TLS verify: true");
    } else {
        printDebug("docker client TLS verify: false");
    }

    return DockerClientBuilder.getInstance(dockerClientConfig).build();
}
 
Example 12
Source File: DockerServiceImporter.java    From vertx-service-discovery with Apache License 2.0 4 votes vote down vote up
/**
 * Starts the bridge.
 *
 * @param vertx         the vert.x instance
 * @param publisher     the service discovery instance
 * @param configuration the bridge configuration if any
 * @param completion    future to assign with completion status
 */
@Override
public void start(Vertx vertx, ServicePublisher publisher, JsonObject configuration, Promise<Void> completion) {
  this.publisher = publisher;
  this.vertx = vertx;
  DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder();

  String dockerCertPath = configuration.getString("docker-cert-path");
  String dockerCfgPath = configuration.getString("docker-cfg-path");
  String email = configuration.getString("docker-registry-email");
  String password = configuration.getString("docker-registry-password");
  String username = configuration.getString("docker-registry-username");
  String host = configuration.getString("docker-host");
  boolean tlsVerify = configuration.getBoolean("docker-tls-verify", true);
  String registry
      = configuration.getString("docker-registry-url", "https://index.docker.io/v1/");
  String version = configuration.getString("version");

  if (dockerCertPath != null) {
    builder.withDockerCertPath(dockerCertPath);
  }
  if (dockerCfgPath != null) {
    builder.withDockerConfig(dockerCfgPath);
  }
  if (email != null) {
    builder.withRegistryEmail(email);
  }
  if (password != null) {
    builder.withRegistryPassword(password);
  }
  if (username != null) {
    builder.withRegistryUsername(username);
  }
  if (host != null) {
    builder.withDockerHost(host);
  }
  if (registry != null) {
    builder.withRegistryUrl(registry);
  }
  if (version != null) {
    builder.withApiVersion(version);
  }
  builder.withDockerTlsVerify(tlsVerify);


  DockerClientConfig config = builder.build();
  if (config.getDockerHost().getScheme().equalsIgnoreCase("unix")) {
    try {
      this.host = InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
      completion.fail(e);
    }
  } else {
    this.host = config.getDockerHost().getHost();
  }
  client = DockerClientBuilder.getInstance(config).build();

  long period = configuration.getLong("scan-period", 3000L);
  if (period > 0) {
    timer = vertx.setPeriodic(period, l -> {
      scan(null);
    });
  }
  scan(completion);
}