Java Code Examples for com.github.dockerjava.core.DefaultDockerClientConfig#createDefaultConfigBuilder()

The following examples show how to use com.github.dockerjava.core.DefaultDockerClientConfig#createDefaultConfigBuilder() . 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: 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 4
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 5
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 6
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 7
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 8
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);
}
 
Example 9
Source File: DockerHandlerWithContainerTest.java    From roboconf-platform with Apache License 2.0 4 votes vote down vote up
private static DockerClient buildDockerClient() {

		Builder config = DefaultDockerClientConfig.createDefaultConfigBuilder();
		config.withDockerHost( "tcp://localhost:" + DockerTestUtils.DOCKER_TCP_PORT );
		return DockerClientBuilder.getInstance( config.build()).build();
	}
 
Example 10
Source File: ToRunByHandDockerImageGenerationTest.java    From roboconf-platform with Apache License 2.0 4 votes vote down vote up
@Test
@Ignore
public void createRoboconfImage() throws Exception {

	// Verify no image exists
	final String tag = "roboconf-test-by-hand";
	Builder config = DefaultDockerClientConfig.createDefaultConfigBuilder();
	config.withDockerHost( "tcp://localhost:" + DockerTestUtils.DOCKER_TCP_PORT );

	DockerClient docker = DockerClientBuilder.getInstance( config.build()).build();
	Image img = DockerUtils.findImageByIdOrByTag( tag, docker );
	if( img != null ) {
		docker.removeImageCmd( tag ).exec();
		img = DockerUtils.findImageByIdOrByTag( tag, docker );
	}

	Assert.assertNull( img );

	// Prepare the parameters
	File baseDir = new File( Thread.currentThread().getContextClassLoader().getResource( "./image/roboconf" ).getFile());
	Assert.assertTrue( baseDir.exists());

	Map<String,String> targetProperties = new HashMap<> ();
	targetProperties.put( DockerHandler.IMAGE_ID, tag );
	targetProperties.put( DockerHandler.GENERATE_IMAGE_FROM, "." );

	TargetHandlerParameters parameters= new TargetHandlerParameters();
	parameters.setTargetProperties( targetProperties );
	parameters.setMessagingProperties( new HashMap<String,String>( 0 ));
	parameters.setApplicationName( "applicationName" );
	parameters.setScopedInstancePath( "/vm" );
	parameters.setScopedInstance( new Instance());
	parameters.setTargetPropertiesDirectory( baseDir );

	File tmpFolder = this.folder.newFolder();
	Map<String,File> containerIdToVolume = new HashMap<> ();
	DockerMachineConfigurator configurator = new DockerMachineConfigurator(
			parameters,
			"machineId",
			tmpFolder,
			containerIdToVolume );

	// Test the creation
	Container container = null;
	try {
		configurator.configure();
		img = DockerUtils.findImageByIdOrByTag( tag, docker );
		Assert.assertNotNull( img );

		String containerName = DockerUtils.buildContainerNameFrom(
				parameters.getScopedInstancePath(),
				parameters.getApplicationName());

		container = DockerUtils.findContainerByIdOrByName( containerName, docker );
		Assert.assertNotNull( container );

	} finally {
		if( img != null )
			docker.removeImageCmd( tag ).exec();

		img = DockerUtils.findImageByIdOrByTag( tag, docker );
		Assert.assertNull( img );

		if( container != null ) {
			docker.removeContainerCmd( container.getId()).withForce( true ).exec();
			container = DockerUtils.findContainerByIdOrByName( container.getId(), docker );
			Assert.assertNull( container );
		}

		configurator.close();
	}
}