com.github.dockerjava.api.DockerClient Java Examples

The following examples show how to use com.github.dockerjava.api.DockerClient. 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: DockerClientOperation.java    From redis-manager with Apache License 2.0 6 votes vote down vote up
/**
 * redis.conf 由程序生成并修改
 * <p>
 * Start docker container  with expose port
 * sudo docker run \
 * --name redis-instance-8000 \
 * --net host \
 * -d -v /data/redis/docker/8000:/data \
 * redis:4.0.14 \
 * redis-server /data/redis.conf
 *
 * @param ip
 * @param port
 * @param image
 * @return
 */
public String createContainer(String ip, int port, String image, String containerName) {
    DockerClient dockerClient = getDockerClient(ip);
    String bind = String.format(VOLUME, port);
    CreateContainerResponse container = dockerClient.createContainerCmd(image)
            // container name
            .withName(CommonUtil.replaceSpace(containerName))
            // host 模式启动
            .withHostConfig(new HostConfig().withNetworkMode("host"))
            // 挂载
            .withBinds(Bind.parse(bind))
            // 自动启动
            .withRestartPolicy(RestartPolicy.alwaysRestart())
            // 配置文件
            .withCmd(REDIS_DEFAULT_WORK_DIR + REDIS_CONF)
            .exec();
    return container.getId();
}
 
Example #2
Source File: LeaveSwarmCmdExecIT.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Test
public void leaveSwarmAsMaster() throws DockerException {
    DockerClient dockerClient = startSwarm();

    Info info = dockerClient.infoCmd().exec();
    LOG.info("Inspected docker: {}", info.toString());

    assertThat(info.getSwarm().getLocalNodeState(), is(LocalNodeState.ACTIVE));

    dockerClient.leaveSwarmCmd()
            .withForceEnabled(true)
            .exec();
    LOG.info("Left swarm");

    info = dockerClient.infoCmd().exec();
    LOG.info("Inspected docker: {}", info.toString());

    assertThat(info.getSwarm().getLocalNodeState(), is(LocalNodeState.INACTIVE));

}
 
Example #3
Source File: DockerClientConnector.java    From jbpm-work-items with Apache License 2.0 6 votes vote down vote up
public DockerClient getDockerClient(String registryUsername,
                                    String registryPassword,
                                    String registryEmail,
                                    String dockerCertPath,
                                    String dockerConfig,
                                    String dockerTlsVerify,
                                    String dockerHost) {
    DefaultDockerClientConfig config
            = DefaultDockerClientConfig.createDefaultConfigBuilder()
            .withRegistryEmail(registryEmail)
            .withRegistryPassword(registryPassword)
            .withRegistryUsername(registryUsername)
            .withDockerCertPath(dockerCertPath)
            .withDockerConfig(dockerConfig)
            .withDockerTlsVerify(dockerTlsVerify)
            .withDockerHost(dockerHost).build();

    return DockerClientBuilder.getInstance(config).build();
}
 
Example #4
Source File: TagImageCommand.java    From cubeai with Apache License 2.0 6 votes vote down vote up
@Override
public void execute() throws DockerException {
	if (image == null || image.isEmpty()) {
		throw new IllegalArgumentException("Please provide an image name");
	} else if (repository == null || repository.isEmpty()) {
		throw new IllegalArgumentException("Please provide a repository");
	} else if (tag == null || tag.isEmpty()) {
		throw new IllegalArgumentException("Please provide a tag for the image");
	}
	DockerClient client = getClient();
	try {
		logger.debug("start tagging image " + image + " in " + repository + " as " + tag);
		client.tagImageCmd(image, repository, tag).withForce(withForce).exec();
		logger.debug("Tagged image " + image + " in " + repository + " as " + tag);
	} catch (NotFoundException e) {
		if (!ignoreIfNotFound) {
			logger.error(String.format("image '%s' not found ", image));
			throw e;
		} else {
			logger.error(String.format(
					"image '%s' not found, but skipping this error is turned on, let's continue ... ", image));
		}
	}
}
 
Example #5
Source File: StartupCheckStrategy.java    From testcontainers-java with MIT License 6 votes vote down vote up
public boolean waitUntilStartupSuccessful(DockerClient dockerClient, String containerId) {
    final Boolean[] startedOK = {null};
    Unreliables.retryUntilTrue((int) timeout.toMillis(), TimeUnit.MILLISECONDS, () -> {
        //noinspection CodeBlock2Expr
        return DOCKER_CLIENT_RATE_LIMITER.getWhenReady(() -> {
            StartupStatus state = checkStartupState(dockerClient, containerId);
            switch (state) {
                case SUCCESSFUL:    startedOK[0] = true;
                                    return true;
                case FAILED:        startedOK[0] = false;
                                    return true;
                default:            return false;
            }
        });
    });
    return startedOK[0];
}
 
Example #6
Source File: DockerCloud.java    From docker-plugin with MIT License 6 votes vote down vote up
/**
 * for publishers/builders. Simply runs container in docker cloud
 */
public static String runContainer(DockerTemplateBase dockerTemplateBase,
                                  DockerClient dockerClient) {
    CreateContainerCmd containerConfig = dockerClient.createContainerCmd(dockerTemplateBase.getImage());

    dockerTemplateBase.fillContainerConfig(containerConfig);

    // create
    CreateContainerResponse response = containerConfig.exec();
    String containerId = response.getId();

    // start
    StartContainerCmd startCommand = dockerClient.startContainerCmd(containerId);
    startCommand.exec();

    return containerId;
}
 
Example #7
Source File: LogUtils.java    From testcontainers-java with MIT License 6 votes vote down vote up
private static Closeable attachConsumer(
    DockerClient dockerClient,
    String containerId,
    Consumer<OutputFrame> consumer,
    boolean followStream,
    OutputFrame.OutputType... types
) {

    final LogContainerCmd cmd = dockerClient.logContainerCmd(containerId)
        .withFollowStream(followStream)
        .withSince(0);

    final FrameConsumerResultCallback callback = new FrameConsumerResultCallback();
    for (OutputFrame.OutputType type : types) {
        callback.addConsumer(type, consumer);
        if (type == STDOUT) cmd.withStdOut(true);
        if (type == STDERR) cmd.withStdErr(true);
    }

    return cmd.exec(callback);
}
 
Example #8
Source File: DockerClientOperation.java    From redis-manager with Apache License 2.0 6 votes vote down vote up
/**
 * @param ip
 * @param imageName
 * @return repoTags, imageId
 */
public Map<String, String> getImages(String ip, String imageName) {
    DockerClient dockerClient = getDockerClient(ip);

    ListImagesCmd listImagesCmd = dockerClient.listImagesCmd();
    if (!Strings.isNullOrEmpty(imageName)) {
        listImagesCmd.withImageNameFilter(imageName);
    }
    List<Image> images = listImagesCmd.exec();
    Map<String, String> imageMap = new HashMap<>();
    Iterator<Image> iterator = images.iterator();
    while (iterator.hasNext()) {
        Image next = iterator.next();
        String[] repoTags = next.getRepoTags();
        for (String repoTag : repoTags) {
            imageMap.put(repoTag, next.getId());
        }
    }
    return imageMap;
}
 
Example #9
Source File: DockerTemplate.java    From docker-plugin with MIT License 6 votes vote down vote up
@Restricted(NoExternalUse.class)
public DockerTransientNode provisionNode(DockerAPI api, TaskListener listener) throws IOException, Descriptor.FormException, InterruptedException {
    try {
        final InspectImageResponse image = pullImage(api, listener);
        final String effectiveRemoteFsDir = getEffectiveRemoteFs(image);
        try(final DockerClient client = api.getClient()) {
            return doProvisionNode(api, client, effectiveRemoteFsDir, listener);
        }
    } catch (IOException | Descriptor.FormException | InterruptedException | RuntimeException ex) {
        final DockerCloud ourCloud = DockerCloud.findCloudForTemplate(this);
        final long milliseconds = ourCloud == null ? 0L : ourCloud.getEffectiveErrorDurationInMilliseconds();
        if (milliseconds > 0L) {
            // if anything went wrong, disable ourselves for a while
            final String reason = "Template provisioning failed.";
            final DockerDisabled reasonForDisablement = getDisabled();
            reasonForDisablement.disableBySystem(reason, milliseconds, ex);
            setDisabled(reasonForDisablement);
        }
        throw ex;
    }
}
 
Example #10
Source File: ValueTransferWithHashicorpAcceptanceTest.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
@BeforeAll
public static void setUpBase() {

  Runtime.getRuntime()
      .addShutdownHook(new Thread(ValueTransferWithHashicorpAcceptanceTest::tearDownBase));

  final DockerClient docker = new DockerClientFactory().create();
  hashicorpNode = HashicorpHelpers.createLoadedHashicorpVault(docker, false);

  final NodeConfiguration nodeConfig = new NodeConfigurationBuilder().build();

  ethNode = new BesuNode(docker, nodeConfig);
  ethNode.start();
  ethNode.awaitStartupCompletion();

  final SignerConfiguration signerConfig =
      new SignerConfigurationBuilder().withHashicorpSigner(hashicorpNode).build();

  ethSigner = new Signer(signerConfig, nodeConfig, ethNode.ports());
  ethSigner.start();
  ethSigner.awaitStartupCompletion();
}
 
Example #11
Source File: PullImageCommand.java    From cubeai with Apache License 2.0 6 votes vote down vote up
@Override
public void execute() throws DockerException {
	AuthConfig authConfig = new AuthConfig()
		       .withUsername("docker")
		       .withPassword("docker")
		       .withEmail("[email protected]")
		       .withRegistryAddress("nexus3.acumos.org");
	String imageFullName = "nexus3.acumos.org:10004/onboarding-base-r";
	logger.debug("Full Image Name: " + imageFullName);
	final DockerClient client = getClient();

	logger.debug("Auth Config started: " + authConfig.toString());
	client.authCmd().withAuthConfig(authConfig).exec(); // WORKS

	logger.debug("Pull Command started");
	client.pullImageCmd(imageFullName) // FAILS
	        .withTag("1.0")
	        .withAuthConfig(authConfig)
	        .exec(new PullImageResultCallback()).awaitSuccess();
	logger.debug("Pull Command end");

}
 
Example #12
Source File: DockerContainerManagementService.java    From wecube-platform with Apache License 2.0 6 votes vote down vote up
@Override
public void startItem(ResourceItem item) {
    String containerName = item.getName();
    DockerClient dockerClient = newDockerClient(item.getResourceServer().getHost());
    List<Container> containers = dockerClient.listContainersCmd().withShowAll(true)
            .withFilter("name", Arrays.asList(containerName)).exec();
    if (containers.isEmpty()) {
        throw new WecubeCoreException(String
                .format("Failed to start container with name [%s] : Container is not exists.", containerName));
    }

    Container container = containers.get(0);
    if (!container.getState().equals("running")) {
        dockerClient.startContainerCmd(containerName).exec();
    } else {
        log.warn("The container {} is already running.", containerName);
    }
}
 
Example #13
Source File: Main.java    From logstash with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
        DockerClient dockerClient = DockerClientFactory.build();

        MesosCluster cluster = new MesosCluster(ClusterUtil.withAgent(1, zooKeeper -> new LogstashMesosSlave(dockerClient, zooKeeper)).withMaster().build());

        cluster.start();

/*
        LOGGER.info("Starting scheduler");
        LogstashSchedulerContainer scheduler = new LogstashSchedulerContainer(dockerClient, cluster.getZkContainer().getIpAddress());
        scheduler.start();
        LOGGER.info("Scheduler started at http://" + scheduler.getIpAddress() + ":9092");
*/

        LOGGER.info("Type CTRL-C to quit");
        while (true) {
            Thread.sleep(1000);
        }
    }
 
Example #14
Source File: DockerAPI.java    From docker-plugin with MIT License 6 votes vote down vote up
/** Obtains a {@link DockerClient} from the cache, or makes one and puts it in the cache, implicitly telling the cache we need it. */
private static DockerClient getOrMakeClient(final String dockerUri, final String credentialsId,
        final int readTimeout, final int connectTimeout) {
    final Integer readTimeoutInMillisecondsOrNull = readTimeout > 0 ? readTimeout * 1000 : null;
    final Integer connectTimeoutInMillisecondsOrNull = connectTimeout > 0 ? connectTimeout * 1000 : null;
    final DockerClientParameters cacheKey = new DockerClientParameters(dockerUri, credentialsId, readTimeoutInMillisecondsOrNull, connectTimeoutInMillisecondsOrNull);
    synchronized(CLIENT_CACHE) {
        SharableDockerClient client = CLIENT_CACHE.getAndIncrementUsage(cacheKey);
        if ( client==null ) {
            client = makeClient(dockerUri, credentialsId, readTimeoutInMillisecondsOrNull,
                    connectTimeoutInMillisecondsOrNull);
            LOGGER.info("Cached connection {} to {}", client, cacheKey);
            CLIENT_CACHE.cacheAndIncrementUsage(cacheKey, client);
        }
        return client;
    }
}
 
Example #15
Source File: DockerClientProviderStrategy.java    From testcontainers-java with MIT License 6 votes vote down vote up
protected DockerClient getClientForConfig(DockerClientConfig config) {
    config = new AuthDelegatingDockerClientConfig(config);
    final DockerHttpClient dockerHttpClient;

    String transportType = TestcontainersConfiguration.getInstance().getTransportType();
    switch (transportType) {
        case "okhttp":
            dockerHttpClient = new OkDockerHttpClient.Builder()
                .dockerHost(config.getDockerHost())
                .sslConfig(config.getSSLConfig())
                .build();
            break;
        case "httpclient5":
            dockerHttpClient = new ZerodepDockerHttpClient.Builder()
                .dockerHost(config.getDockerHost())
                .sslConfig(config.getSSLConfig())
                .build();
            break;
        default:
            throw new IllegalArgumentException("Unknown transport type '" + transportType + "'");
    }

    return DockerClientImpl.getInstance(config, dockerHttpClient);
}
 
Example #16
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 #17
Source File: JoinSwarmCmdExecIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
private SwarmJoinTokens initSwarmOnDocker(DockerClient docker) {
    SwarmSpec swarmSpec = new SwarmSpec();
    docker.initializeSwarmCmd(swarmSpec)
            .exec();
    LOG.info("Initialized swarm docker1: {}", swarmSpec.toString());

    Swarm swarm = docker.inspectSwarmCmd().exec();
    LOG.info("Inspected swarm on docker1: {}", swarm.toString());
    return swarm.getJoinTokens();
}
 
Example #18
Source File: DockerManagementServer.java    From docker-plugin with MIT License 5 votes vote down vote up
public Collection getImages(){
    if ( !Jenkins.getInstance().hasPermission(Jenkins.ADMINISTER) ) {
        return Collections.emptyList();
    }
    final DockerAPI dockerApi = theCloud.getDockerApi();
    try(final DockerClient client = dockerApi.getClient()) {
        return client.listImagesCmd().exec();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example #19
Source File: ListSecretCmdExecIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test
public void tesListSecret() throws DockerException {
    DockerClient dockerClient = startSwarm();
    int length = 10;
    boolean useLetters = true;
    boolean useNumbers = false;
    String secretName = RandomStringUtils.random(length, useLetters, useNumbers);
    CreateSecretResponse exec = dockerClient.createSecretCmd(new SecretSpec().withName(secretName).withData("mon secret en clair")).exec();
    assertThat(exec, notNullValue());
    assertThat(exec.getId(), notNullValue());
    LOG.info("Secret created with ID {}", exec.getId());


    List<Secret> secrets = dockerClient.listSecretsCmd()
            .withNameFilter(Lists.newArrayList(secretName))
            .exec();

    assertThat(secrets, hasSize(1));

    dockerClient.removeSecretCmd(secrets.get(0).getId())
            .exec();
    LOG.info("Secret removed with ID {}", exec.getId());
    List<Secret> secretsAfterRemoved = dockerClient.listSecretsCmd()
            .withNameFilter(Lists.newArrayList(secretName))
            .exec();

    assertThat(secretsAfterRemoved, hasSize(0));

}
 
Example #20
Source File: MinimumDurationRunningStartupCheckStrategy.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Override
public StartupStatus checkStartupState(DockerClient dockerClient, String containerId) {
    // record "now" before fetching status; otherwise the time to fetch the status
    // will contribute to how long the container has been running.
    Instant now = Instant.now();
    InspectContainerResponse.ContainerState state = getCurrentState(dockerClient, containerId);

    if (DockerStatus.isContainerRunning(state, minimumRunningDuration, now)) {
        return StartupStatus.SUCCESSFUL;
    } else if (DockerStatus.isContainerStopped(state)) {
        return StartupStatus.FAILED;
    }
    return StartupStatus.NOT_YET_KNOWN;
}
 
Example #21
Source File: DockerJavaUtil.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
/**
 * Docker remove Image
 * @param client
 * @param imageName
 */
public static void removeImage(DockerClient client, String imageName){
    List<String> filterName = new ArrayList<>();
    filterName.add(imageName);
    List<Container> containers = client.listContainersCmd().withNameFilter(filterName).exec();
    for(Container container : containers){
        client.removeContainerCmd(container.getId()).exec();
    }

    List<Image> images = client.listImagesCmd().withImageNameFilter(imageName).exec();
    for(Image image : images){
        client.removeImageCmd(image.getId()).exec();
    }
}
 
Example #22
Source File: AttachContainerCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test
public void attachContainerWithTTY() throws Exception {
    DockerClient dockerClient = dockerRule.getClient();

    File baseDir = new File(Thread.currentThread().getContextClassLoader()
            .getResource("attachContainerTestDockerfile").getFile());

    String imageId = dockerRule.buildImage(baseDir);

    CreateContainerResponse container = dockerClient.createContainerCmd(imageId).withTty(true).exec();

    LOG.info("Created container: {}", container.toString());
    assertThat(container.getId(), not(is(emptyString())));

    dockerClient.startContainerCmd(container.getId()).exec();

    AttachContainerTestCallback callback = new AttachContainerTestCallback() {
        @Override
        public void onNext(Frame frame) {
            assertThat(frame.getStreamType(), equalTo(StreamType.RAW));
            super.onNext(frame);
        };
    };

    dockerClient.attachContainerCmd(container.getId())
            .withStdErr(true)
            .withStdOut(true)
            .withFollowStream(true)
            .exec(callback)
            .awaitCompletion(15, TimeUnit.SECONDS);
    callback.close();

    LOG.debug("log: {}", callback.toString());

    // HexDump.dump(collectFramesCallback.toString().getBytes(), 0, System.out, 0);
    assertThat(callback.toString(), containsString("stdout\r\nstderr"));
}
 
Example #23
Source File: DockerContainerWatchdog.java    From docker-plugin with MIT License 5 votes vote down vote up
private void cleanUpSuperfluousContainers(DockerClient client, Map<String, Node> nodeMap, ContainerNodeNameMap csm, DockerCloud dc, Instant snapshotInstant) {
    Collection<Container> allContainers = csm.getAllContainers();

    for (Container container : allContainers) {
        String nodeName = csm.getNodeName(container.getId());

        Node node = nodeMap.get(nodeName);
        if (node != null) {
            // the node and the container still have a proper mapping => ok
            continue;
        }

        /*
         * During startup it may happen temporarily that a container exists, but the
         * corresponding node isn't there yet.
         * That is why we have to have a grace period for pulling up containers.
         */
        if (isStillTooYoung(container.getCreated(), snapshotInstant)) {
            continue;
        }

        checkForTimeout(snapshotInstant);

        // this is a container, which is missing a corresponding node with us
        LOGGER.info("Container {}, which is reported to be assigned to node {}, "
                + "is no longer associated (node might be gone already?). "
                + "The container's last status is {}; it was created on {}", 
                container.getId(), nodeName, container.getStatus(), container.getCreated());

        try {
            terminateContainer(dc, client, container);
        } catch (Exception e) {
            // Graceful termination failed; we need to use some force
            LOGGER.warn("Graceful termination of Container {} failed; terminating directly via API - this may cause remnants to be left behind", container.getId(), e);
        }
    }
}
 
Example #24
Source File: DockerHandler.java    From roboconf-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void terminateMachine( TargetHandlerParameters parameters, String machineId )
throws TargetException {

	this.logger.fine( "Terminating machine " + machineId );
	try {
		cancelMachineConfigurator( machineId );
		DockerClient dockerClient = DockerUtils.createDockerClient( parameters.getTargetProperties());
		Container container = DockerUtils.findContainerByIdOrByName( machineId, dockerClient );

		// The case "container == null" is possible.
		// Indeed, it may have been killed manually. This method will then
		// just mark the Roboconf instance as "not deployed" without throwing an exception.
		if( container != null ) {

			// Stop the container
			ContainerState state = DockerUtils.getContainerState( machineId, dockerClient );
			if( state != null
					&& ( extractBoolean( state.getRunning()) || extractBoolean( state.getPaused())))
				dockerClient.killContainerCmd( container.getId()).exec();

			dockerClient.removeContainerCmd( container.getId()).withForce( true ).exec();

			// Delete the volume we used to share user data
			// (container names are prefixed by their parent, here "/").
			// See https://github.com/moby/moby/issues/6705
			String containerName = container.getNames()[ 0 ].substring( 1 );
			File userDataDir = this.containerIdToVolume.remove( containerName );
			Utils.deleteFilesRecursivelyAndQuietly( userDataDir );
		}

	} catch( Exception e ) {
		throw new TargetException( e );
	}
}
 
Example #25
Source File: UpdateSwarmNodeIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateSwarmNode() throws Exception {
    DockerClient dockerClient = startSwarm();
    List<SwarmNode> nodes = dockerClient.listSwarmNodesCmd().exec();
    assertThat(1, is(nodes.size()));
    SwarmNode node = nodes.get(0);
    assertThat(SwarmNodeState.READY, is(node.getStatus().getState()));
    //update the node availability
    SwarmNodeSpec nodeSpec = node.getSpec().withAvailability(SwarmNodeAvailability.PAUSE);
    dockerClient.updateSwarmNodeCmd().withSwarmNodeId(node.getId()).withVersion(node.getVersion().getIndex())
            .withSwarmNodeSpec(nodeSpec).exec();
    nodes = dockerClient.listSwarmNodesCmd().exec();
    assertThat(nodes.size(), is(1));
    assertThat(nodes.get(0).getSpec().getAvailability(), is(SwarmNodeAvailability.PAUSE));
}
 
Example #26
Source File: DockerTemplate.java    From docker-plugin with MIT License 5 votes vote down vote up
@Nonnull
InspectImageResponse pullImage(DockerAPI api, TaskListener listener) throws IOException, InterruptedException {
    final String image = getFullImageId();

    final boolean shouldPullImage;
    try(final DockerClient client = api.getClient()) {
        shouldPullImage = getPullStrategy().shouldPullImage(client, image);
    }
    if (shouldPullImage) {
        // TODO create a FlyWeightTask so end-user get visibility on pull operation progress
        LOGGER.info("Pulling image '{}'. This may take awhile...", image);

        long startTime = System.currentTimeMillis();

        try(final DockerClient client = api.getClient(pullTimeout)) {
            final PullImageCmd cmd =  client.pullImageCmd(image);
            final DockerRegistryEndpoint registry = getRegistry();
            DockerCloud.setRegistryAuthentication(cmd, registry, Jenkins.getInstance());
            cmd.exec(new PullImageResultCallback() {
                @Override
                public void onNext(PullResponseItem item) {
                    super.onNext(item);
                    listener.getLogger().println(item.getStatus());
                }
            }).awaitCompletion();
        }

        long pullTime = System.currentTimeMillis() - startTime;
        LOGGER.info("Finished pulling image '{}', took {} ms", image, pullTime);
    }

    final InspectImageResponse result;
    try(final DockerClient client = api.getClient()) {
        result = client.inspectImageCmd(image).exec();
    } catch (NotFoundException e) {
        throw new DockerClientException("Could not pull image: " + image, e);
    }
    return result;
}
 
Example #27
Source File: DockerUtil.java    From julongchain with Apache License 2.0 5 votes vote down vote up
/**
 * 创建Docker镜像
 *
 * @param dockerFilePath dockerFile文件路径
 * @param tag 镜像的标签
 * @return
 */
public static String buildImage(String dockerFilePath, String tag) {
  DockerClient dockerClient = getDockerClient();

  BuildImageResultCallback callback =
      new BuildImageResultCallback() {
        @Override
        public void onNext(BuildResponseItem item) {
          logger.info(item.toString());
          super.onNext(item);
        }
      };

  String imageId =
      dockerClient
          .buildImageCmd()
          .withDockerfile(new File(dockerFilePath))
          .withTags(Sets.newHashSet(tag))
          .exec(callback)
          .awaitImageId();

  closeDockerClient(dockerClient);

  logger.info("build image success, imageId:" + imageId);

  return imageId;
}
 
Example #28
Source File: ListSwarmNodesCmdExecIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testListSwarmNodes() throws Exception {
    DockerClient dockerClient = startSwarm();

    List<SwarmNode> nodes = dockerClient.listSwarmNodesCmd().exec();
    assertThat(nodes.size(), is(1));
}
 
Example #29
Source File: DockerClientFactory.java    From Core with MIT License 5 votes vote down vote up
static DockerClient getByConfiguration(Configuration configuration) {
    return DockerClientFactory.get(
            configuration.getString("docker.host"),
            configuration.getBoolean("docker.tsl-verify"),
            configuration.getString("docker.cert-path"),
            configuration.getString("docker.registry.username"),
            configuration.getString("docker.registry.password"),
            configuration.getString("docker.registry.email"),
            configuration.getString("docker.registry.url")
    );
}
 
Example #30
Source File: DockerBuilderControlOptionStop.java    From docker-plugin with MIT License 5 votes vote down vote up
@Override
public void execute(Run<?, ?> build, Launcher launcher, TaskListener listener)
        throws DockerException {
    final PrintStream llog = listener.getLogger();
    LOG.info("Stopping container " + containerId);
    llog.println("Stopping container " + containerId);

    final DockerCloud cloud = getCloud(build, launcher);
    final DockerAPI dockerApi = cloud.getDockerApi();
    try(final DockerClient client = dockerApi.getClient()) {
        executeOnDocker(build, llog, client);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}