當前位置: 首頁>>代碼示例>>Java>>正文


Java DockerClient.createContainerCmd方法代碼示例

本文整理匯總了Java中com.github.dockerjava.api.DockerClient.createContainerCmd方法的典型用法代碼示例。如果您正苦於以下問題:Java DockerClient.createContainerCmd方法的具體用法?Java DockerClient.createContainerCmd怎麽用?Java DockerClient.createContainerCmd使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.github.dockerjava.api.DockerClient的用法示例。


在下文中一共展示了DockerClient.createContainerCmd方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: runInsideDocker

import com.github.dockerjava.api.DockerClient; //導入方法依賴的package包/類
private <T> T runInsideDocker(DockerClient client, Consumer<CreateContainerCmd> createContainerCmdConsumer, BiFunction<DockerClient, String, T> block) {
    checkAndPullImage(client, TINY_IMAGE);
    CreateContainerCmd createContainerCmd = client.createContainerCmd(TINY_IMAGE);
    createContainerCmdConsumer.accept(createContainerCmd);
    String id = createContainerCmd.exec().getId();

    try {
        client.startContainerCmd(id).exec();
        return block.apply(client, id);
    } finally {
        try {
            client.removeContainerCmd(id).withRemoveVolumes(true).withForce(true).exec();
        } catch (NotFoundException | InternalServerErrorException ignored) {
            log.debug("", ignored);
        }
    }
}
 
開發者ID:testcontainers,項目名稱:testcontainers-java,代碼行數:18,代碼來源:DockerClientFactory.java

示例2: runContainer

import com.github.dockerjava.api.DockerClient; //導入方法依賴的package包/類
/**
 * 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;
}
 
開發者ID:jenkinsci,項目名稱:docker-plugin,代碼行數:20,代碼來源:DockerCloud.java

示例3: createInstance

import com.github.dockerjava.api.DockerClient; //導入方法依賴的package包/類
private ServiceInstance createInstance(TestDescriptor descriptor, DockerClient client, int i) {
      CreateContainerCmd cmd = client.createContainerCmd(descriptor.getImage().value());
      cmd = computeContainerName(descriptor, i, cmd);
      cmd = executeOptionBuilders(descriptor, cmd);
      if (descriptor.getCustomisationHook() != null) {
          cmd = executeCustomisationHook(descriptor.getCustomisationHook(), descriptor.getInstance(), cmd);
      }
      String containerId = null;
      Status status = null;
      String statusDetails = null;
try {
	containerId = createAndStartContainer(cmd, descriptor.getImage().pull(),  client);
	status = Status.STARTED;
	statusDetails = "Started.";
} catch (Throwable t) {
	if(t instanceof CompletionException) {
		if(t.getCause() != null && t.getCause() instanceof ContainerException) {
			containerId = ((ContainerException) t.getCause()).getContainerId();
			statusDetails = t.getCause().getCause() != null ? t.getCause().getCause().getMessage() : null;
		} else {
			statusDetails = t.getCause() != null ? t.getCause().getMessage() : null;
		}	
	} else {
		statusDetails = t.getMessage();
	}
	status = Status.ABORTED;
}
return ServiceInstance.builder()
              .containerName(cmd.getName())
              .containerId(containerId)
              .status(status)
              .statusDetails(statusDetails)
              .build();
  }
 
開發者ID:qzagarese,項目名稱:dockerunit,代碼行數:35,代碼來源:DefaultServiceBuilder.java

示例4: testConfigureOptions

import com.github.dockerjava.api.DockerClient; //導入方法依賴的package包/類
@Test
public void testConfigureOptions() throws Exception {

	Map<String,String> options = new HashMap<> ();
	options.put( "cap-drop", "AUDIT_CONTROL" );
	options.put( "cap-add", "SYS_PTRACE, SYS_NICE" );
	options.put( "hostname", "hello" );
	options.put( "AttachStdin", "true" );
	options.put( "MemorySwap", "50" );
	options.put( "CpuShares", "2" );
	options.put( "Memory", "1024" );
	options.put( "env", "env1, env2, env3" );

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

	DockerClient dockerClient = DockerUtils.createDockerClient( targetProperties );
	CreateContainerCmd cmd = dockerClient.createContainerCmd( "whatever, we won't execute it" );

	DockerUtils.configureOptions( options, cmd );

	Assert.assertTrue( cmd.isAttachStdin());
	Assert.assertEquals( "hello", cmd.getHostName());
	Assert.assertTrue( Arrays.deepEquals(
			new Capability[] { Capability.SYS_PTRACE, Capability.SYS_NICE },
			cmd.getCapAdd()));

	Assert.assertTrue( Arrays.deepEquals(
			new Capability[] { Capability.AUDIT_CONTROL },
			cmd.getCapDrop()));

	Assert.assertEquals( Long.valueOf( 50L ), cmd.getMemorySwap());
	Assert.assertEquals( Long.valueOf( 1024L ), cmd.getMemory());
	Assert.assertEquals( Integer.valueOf( 2 ), cmd.getCpuShares());
	Assert.assertTrue( Arrays.deepEquals(
			new String[] { "env1", "env2", "env3" },
			cmd.getEnv()));
}
 
開發者ID:roboconf,項目名稱:roboconf-platform,代碼行數:39,代碼來源:DockerUtilsTest.java

示例5: testConfigureOptions_unknownOption

import com.github.dockerjava.api.DockerClient; //導入方法依賴的package包/類
@Test( expected = TargetException.class )
public void testConfigureOptions_unknownOption() throws Exception {

	Map<String,String> options = new HashMap<> ();
	options.put( "what-is-this", "AUDIT_CONTROL" );

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

	DockerClient dockerClient = DockerUtils.createDockerClient( targetProperties );
	CreateContainerCmd cmd = dockerClient.createContainerCmd( "whatever, we won't execute it" );

	DockerUtils.configureOptions( options, cmd );
}
 
開發者ID:roboconf,項目名稱:roboconf-platform,代碼行數:15,代碼來源:DockerUtilsTest.java

示例6: testConfigureOptions_unsupportedOption

import com.github.dockerjava.api.DockerClient; //導入方法依賴的package包/類
@Test( expected = TargetException.class )
public void testConfigureOptions_unsupportedOption() throws Exception {

	Map<String,String> options = new HashMap<> ();
	options.put( "ExposedPorts", "24, 25" );

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

	DockerClient dockerClient = DockerUtils.createDockerClient( targetProperties );
	CreateContainerCmd cmd = dockerClient.createContainerCmd( "whatever, we won't execute it" );

	DockerUtils.configureOptions( options, cmd );
}
 
開發者ID:roboconf,項目名稱:roboconf-platform,代碼行數:15,代碼來源:DockerUtilsTest.java

示例7: testConfigureOptions_synonyms

import com.github.dockerjava.api.DockerClient; //導入方法依賴的package包/類
@Test
public void testConfigureOptions_synonyms() throws Exception {

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

	DockerClient dockerClient = DockerUtils.createDockerClient( targetProperties );
	CreateContainerCmd cmd = dockerClient.createContainerCmd( "whatever, we won't execute it" );

	// First try
	Map<String,String> options = new HashMap<>( 1 );
	options.put( "cap-drop", "AUDIT_CONTROL" );

	DockerUtils.configureOptions( options, cmd );
	Assert.assertTrue( Arrays.deepEquals(
			new Capability[] { Capability.AUDIT_CONTROL },
			cmd.getCapDrop()));

	// Second try
	options = new HashMap<>( 1 );
	options.put( "CapDrop", "SYS_PTRACE" );

	DockerUtils.configureOptions( options, cmd );
	Assert.assertTrue( Arrays.deepEquals(
			new Capability[] { Capability.SYS_PTRACE },
			cmd.getCapDrop()));

	// Third try
	options = new HashMap<>( 1 );
	options.put( "--cap-drop", "SYS_NICE" );

	DockerUtils.configureOptions( options, cmd );
	Assert.assertTrue( Arrays.deepEquals(
			new Capability[] { Capability.SYS_NICE },
			cmd.getCapDrop()));
}
 
開發者ID:roboconf,項目名稱:roboconf-platform,代碼行數:37,代碼來源:DockerUtilsTest.java

示例8: call

import com.github.dockerjava.api.DockerClient; //導入方法依賴的package包/類
public String call() throws Exception {
    DockerClient client = DockerCommand.getClient(descriptor, cfgData.dockerUrlRes, cfgData.dockerVersionRes, cfgData.dockerCertPathRes, null);
    
    CreateContainerCmd cfgCmd = client.createContainerCmd(imageRes);
    if (commandRes != null) {
        cfgCmd.withCmd(commandRes);
    }
    cfgCmd.withHostName(hostNameRes);
    cfgCmd.withName(containerNameRes);
    HostConfig hc = new HostConfig();
    cfgCmd.withLinks(LinkUtils.parseLinks(linksRes).getLinks());
    if (envVarsRes != null) {
        cfgCmd.withEnv(envVarsRes);
    }
    if (exposedPortsRes != null && !exposedPortsRes.isEmpty()) {
    	final ExposedPort[] ports;
        String[] exposedPortsSplitted = exposedPortsRes.split(",");
        ports = new ExposedPort[exposedPortsSplitted.length];
        for (int i = 0; i < ports.length; i++) {
            ports[i] = ExposedPort.parse(exposedPortsSplitted[i]);
        }

    	cfgCmd.withExposedPorts(ports);
    }
    if (cpuSharesRes != null) {
        cfgCmd.withCpuShares(cpuSharesRes);
    }
    if (memoryLimitRes != null) {
        cfgCmd.withMemory(memoryLimitRes);
    }
    if (dnsRes != null) {
        cfgCmd.withDns(dnsRes);
    }
    if (extraHostsRes != null) {
        cfgCmd.withExtraHosts(extraHostsRes);
    }
    if (portBindingsRes != null) {
        cfgCmd.withPortBindings(PortBindingParser.parse(portBindingsRes));
    }
    if (bindMountsRes != null) {
        cfgCmd.withBinds(BindParser.parse(bindMountsRes));
    }
    if (alwaysRestart) {
        cfgCmd.withRestartPolicy(RestartPolicy.alwaysRestart());
    }
    
    CreateContainerResponse resp = cfgCmd.withPublishAllPorts(publishAllPorts).withPrivileged(privileged).exec();
    InspectContainerResponse inspectResp = client.inspectContainerCmd(resp.getId()).exec();
    
    ObjectMapper mapper = new ObjectMapper();
    String serialized = mapper.writeValueAsString(inspectResp);
    return serialized;
}
 
開發者ID:jenkinsci,項目名稱:docker-build-step-plugin,代碼行數:54,代碼來源:CreateContainerRemoteCallable.java

示例9: provisionNode

import com.github.dockerjava.api.DockerClient; //導入方法依賴的package包/類
@Restricted(NoExternalUse.class)
public DockerTransientNode provisionNode(DockerAPI api, TaskListener listener) throws IOException, Descriptor.FormException, InterruptedException {

    final DockerComputerConnector connector = getConnector();
    pullImage(api, listener);

    LOGGER.info("Trying to run container for {}", getImage());

    final DockerClient client = api.getClient();

    CreateContainerCmd cmd = client.createContainerCmd(getImage());
    fillContainerConfig(cmd);

    // Unique ID we use both as Node identifier and container Name
    // See how DockerComputerJNLPConnector.beforeContainerCreated() is building the JNLP jar command
    final String uid = Long.toHexString(System.nanoTime());
    cmd.withName(uid);

    connector.beforeContainerCreated(api, remoteFs, cmd);

    LOGGER.info("Trying to run container {} from image: {}", uid, getImage());
    String containerId = cmd.exec().getId();

    try {
        connector.beforeContainerStarted(api, remoteFs, containerId);
        client.startContainerCmd(containerId).exec();
        connector.afterContainerStarted(api, remoteFs, containerId);
    } catch (DockerException e) {
        // if something went wrong, cleanup aborted container
        client.removeContainerCmd(containerId).withForce(true).exec();
    }

    final ComputerLauncher launcher = connector.createLauncher(api, containerId, remoteFs, listener);

    DockerTransientNode node = new DockerTransientNode(uid, containerId, remoteFs, launcher);
    node.setNodeDescription("Docker Agent [" + getImage() + " on "+ api.getDockerHost().getUri() + "]");
    node.setMode(mode);
    node.setLabelString(labelString);
    node.setRetentionStrategy(retentionStrategy);
    node.setNodeProperties(nodeProperties);
    node.setRemoveVolumes(removeVolumes);
    node.setDockerAPI(api);
    return node;
}
 
開發者ID:jenkinsci,項目名稱:docker-plugin,代碼行數:45,代碼來源:DockerTemplate.java


注:本文中的com.github.dockerjava.api.DockerClient.createContainerCmd方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。