com.github.dockerjava.api.command.CreateContainerCmd Java Examples
The following examples show how to use
com.github.dockerjava.api.command.CreateContainerCmd.
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: DockerCloud.java From docker-plugin with MIT License | 6 votes |
/** * 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 #2
Source File: DockerJavaUtil.java From super-cloudops with Apache License 2.0 | 6 votes |
/** * 创建容器 * * @param client * @return */ public static CreateContainerResponse createContainers(DockerClient client, String containerName, String imageName, Map<Integer, Integer> ports) {//TODO 优化 CreateContainerCmd createContainerCmd = client.createContainerCmd(imageName).withName(containerName); //TODO 处理端口映射 if(!CollectionUtils.isEmpty(ports)){ List<ExposedPort> exposedPorts = new ArrayList<>(); Ports portBindings = new Ports(); for (Map.Entry<Integer, Integer> entry : ports.entrySet()) { Integer key = entry.getKey(); Integer value = entry.getValue(); ExposedPort exposedPort = ExposedPort.tcp(key); exposedPorts.add(exposedPort); portBindings.bind(exposedPort, Ports.Binding.bindPort(value)); } HostConfig hostConfig = newHostConfig().withPortBindings(portBindings); createContainerCmd .withHostConfig(hostConfig).withExposedPorts(exposedPorts); } return createContainerCmd.exec(); }
Example #3
Source File: DockerManager.java From flow-platform-x with Apache License 2.0 | 6 votes |
public String createAndStartContainer(Option option) { StringVars defaultEnv = new StringVars(4); defaultEnv.put(Variables.App.Url, serverUrl); defaultEnv.put(Variables.Agent.Token, ApiAuth.LocalTaskToken); defaultEnv.put(Variables.Agent.Workspace, "/ws/"); defaultEnv.put(Variables.Agent.PluginDir, "/ws/.plugins"); CreateContainerCmd createContainerCmd = client.createContainerCmd(option.image) .withEnv(defaultEnv.merge(option.inputs).toList()) .withCmd("/bin/bash", "-c", option.script); if (option.hasPlugin()) { createContainerCmd.withBinds( new Bind(option.pluginDir, new Volume("/ws/.plugins/" + option.plugin))); } CreateContainerResponse container = createContainerCmd.exec(); String containerId = container.getId(); client.startContainerCmd(container.getId()).exec(); log.debug("Container {} been started", containerId); return containerId; }
Example #4
Source File: DockerEnvUtilsTest.java From docker-plugin with MIT License | 6 votes |
@Test public void addEnvToCmdGivenExistingOtherEnvsThenAddsEnv() { // Given final CreateContainerCmd cmd = mock(CreateContainerCmd.class); final String[] existingEnvs = new String[]{ "foo=bar", "flibble", "x=" }; when(cmd.getEnv()).thenReturn(existingEnvs); final String envName = "name"; final String envValue = "value"; // When DockerEnvUtils.addEnvToCmd(envName, envValue, cmd); // Then verify(cmd, times(1)).withEnv("foo=bar", "flibble", "x=", "name=value"); }
Example #5
Source File: DockerTemplateBaseTest.java From docker-plugin with MIT License | 6 votes |
private static void testFillContainerEnvironmentVariable(final String imageName, final String environmentStringToSet, final boolean envsIsExpectedToBeSet, final String... expectedEnvsSet) { // Given final CreateContainerCmd mockCmd = mock(CreateContainerCmd.class); final DockerTemplateBase instanceUnderTest = new DockerTemplateBase(imageName); instanceUnderTest.setEnvironmentsString(environmentStringToSet); // When instanceUnderTest.fillContainerConfig(mockCmd); // Then if (envsIsExpectedToBeSet) { verify(mockCmd, times(1)).withEnv(expectedEnvsSet); verify(mockCmd, never()).withEnv(anyListOf(String.class)); } else { verify(mockCmd, never()).withEnv((String[]) anyVararg()); verify(mockCmd, never()).withEnv(anyListOf(String.class)); } }
Example #6
Source File: DockerEnvUtils.java From docker-plugin with MIT License | 6 votes |
/** * Adds (or updates) an environment variable to the list of environment * variables being passed into a create-container command. * * @param envName * The name of the environment variable to set. * @param envValue * The value to set it to. * @param cmd * The {@link CreateContainerCmd} whose environment settings are * to be adjusted. */ @Restricted(NoExternalUse.class) public static void addEnvToCmd(String envName, String envValue, CreateContainerCmd cmd) { final String[] oldEnvsOrNull = cmd.getEnv(); final String[] oldEnvs = oldEnvsOrNull == null ? new String[0] : oldEnvsOrNull; final List<String> envs = new ArrayList<>(oldEnvs.length); for (final String oldEnv : oldEnvs) { final int equalsIndex = oldEnv.indexOf('='); if (equalsIndex < 0) { envs.add(oldEnv); } else { final String oldEnvName = oldEnv.substring(0, equalsIndex); if (!oldEnvName.equals(envName)) { envs.add(oldEnv); } } } envs.add(envName + '=' + envValue); final String[] newEnvs = envs.toArray(new String[envs.size()]); cmd.withEnv(newEnvs); }
Example #7
Source File: DockerComputerJNLPConnectorTest.java From docker-plugin with MIT License | 6 votes |
@Test public void testAddingVmargsInBeforeContainerCreated() throws IOException, InterruptedException { // Given final String vmargs = "-Dhttp.proxyPort=8080"; final DockerComputerJNLPConnector connector = new DockerComputerJNLPConnector(new JNLPLauncher(null, vmargs)); final CreateContainerCmd createCmd = mock(CreateContainerCmd.class); final Map<String, String> containerLabels = new TreeMap<>(); when(createCmd.getLabels()).thenReturn(containerLabels); DockerTemplate.setNodeNameInContainerConfig(createCmd, "nodeName"); // When connector.beforeContainerCreated(null, null, createCmd); // Then verify(createCmd, times(1)).withEnv(new String[]{ "JAVA_OPT=" + vmargs }); }
Example #8
Source File: MesosMasterContainer.java From minimesos with Apache License 2.0 | 6 votes |
@Override protected CreateContainerCmd dockerCommand() { int port = getServicePort(); ExposedPort exposedPort = ExposedPort.tcp(port); Ports portBindings = new Ports(); if (getCluster().isMapPortsToHost()) { portBindings.bind(exposedPort, Ports.Binding.bindPort(port)); } CreateContainerCmd cmd = DockerClientFactory.build().createContainerCmd(getImageName() + ":" + getImageTag()) .withName(getName()) .withExposedPorts(new ExposedPort(getServicePort())) .withEnv(newEnvironment() .withValues(getMesosMasterEnvVars()) .withValues(getSharedEnvVars()) .createEnvironment()) .withPortBindings(portBindings); MesosDns mesosDns = getCluster().getMesosDns(); if (mesosDns != null) { cmd.withDns(mesosDns.getIpAddress()); } return cmd; }
Example #9
Source File: DockerTemplateBaseTest.java From docker-plugin with MIT License | 6 votes |
private static void testFillContainerGroupAdd( String imageName, String extraGroupsStringToSet, boolean groupAddIsExpectedToBeSet, String... expectedGroupsSet) { // Given final CreateContainerCmd mockCmd = mock(CreateContainerCmd.class); final HostConfig mockHostConfig = mock(HostConfig.class); when(mockCmd.getHostConfig()).thenReturn(mockHostConfig); final DockerTemplateBase instanceUnderTest = new DockerTemplateBase(imageName); instanceUnderTest.setExtraGroupsString(extraGroupsStringToSet); // When instanceUnderTest.fillContainerConfig(mockCmd); // Then if (groupAddIsExpectedToBeSet) { verify(mockHostConfig, times(1)).withGroupAdd(Arrays.asList(expectedGroupsSet)); } else { verify(mockHostConfig, never()).withGroupAdd(anyListOf(String.class)); } }
Example #10
Source File: DockerEnvUtilsTest.java From docker-plugin with MIT License | 6 votes |
@Test public void addEnvToCmdGivenExistingClashingEnvsThenReplacesEnv() { // Given final CreateContainerCmd cmd = mock(CreateContainerCmd.class); final String[] existingEnvs = new String[]{ "foo=bar", "name=oldvalue", "x=" }; when(cmd.getEnv()).thenReturn(existingEnvs); final String envName = "name"; final String envValue = "value"; // When DockerEnvUtils.addEnvToCmd(envName, envValue, cmd); // Then verify(cmd, times(1)).withEnv("foo=bar", "x=", "name=value"); }
Example #11
Source File: DockerTemplate.java From docker-plugin with MIT License | 6 votes |
public CreateContainerCmd fillContainerConfig(CreateContainerCmd containerConfig) { final CreateContainerCmd result = dockerTemplateBase.fillContainerConfig(containerConfig); final String templateName = getName(); final Map<String, String> existingLabels = containerConfig.getLabels(); final Map<String, String> labels; if (existingLabels == null) { labels = new HashMap<>(); containerConfig.withLabels(labels); } else { labels = existingLabels; } labels.put(DockerContainerLabelKeys.REMOVE_VOLUMES, Boolean.toString(isRemoveVolumes())); labels.put(DockerContainerLabelKeys.TEMPLATE_NAME, templateName); containerConfig.withLabels(labels); final String nodeName = calcUnusedNodeName(templateName); setNodeNameInContainerConfig(result, nodeName); return result; }
Example #12
Source File: ConsulContainer.java From minimesos with Apache License 2.0 | 6 votes |
@Override protected CreateContainerCmd dockerCommand() { int port = getServicePort(); ExposedPort exposedPort = ExposedPort.tcp(port); Ports portBindings = new Ports(); if (getCluster().isMapPortsToHost()) { portBindings.bind(exposedPort, Ports.Binding.bindPort(port)); } ExposedPort consulHTTPPort = ExposedPort.tcp(ConsulConfig.CONSUL_HTTP_PORT); ExposedPort consulDNSPort = ExposedPort.udp(ConsulConfig.CONSUL_DNS_PORT); return DockerClientFactory.build().createContainerCmd(config.getImageName() + ":" + config.getImageTag()) .withName(getName()) .withCmd("agent", "-server", "-bootstrap", "-client", "0.0.0.0") .withExposedPorts(consulHTTPPort, consulDNSPort) .withPortBindings(portBindings); }
Example #13
Source File: ElasticsearchAuthSystemTest.java From elasticsearch with Apache License 2.0 | 6 votes |
@Override protected CreateContainerCmd dockerCommand() { CreateContainerCmd createContainerCmd = super.dockerCommand(); createContainerCmd.withBinds(new Bind(SECRET_FOLDER, new Volume(SECRET_FOLDER))); createContainerCmd.withCmd( ZookeeperCLIParameter.ZOOKEEPER_MESOS_URL, getZookeeperMesosUrl(), ElasticsearchCLIParameter.ELASTICSEARCH_NODES, Integer.toString(TEST_CONFIG.getElasticsearchNodesCount()), org.apache.mesos.elasticsearch.scheduler.Configuration.ELASTICSEARCH_RAM, "256", org.apache.mesos.elasticsearch.scheduler.Configuration.ELASTICSEARCH_DISK, "10", org.apache.mesos.elasticsearch.scheduler.Configuration.USE_IP_ADDRESS, "true", org.apache.mesos.elasticsearch.scheduler.Configuration.FRAMEWORK_ROLE, "testRole", org.apache.mesos.elasticsearch.scheduler.Configuration.FRAMEWORK_PRINCIPAL, "testRole", org.apache.mesos.elasticsearch.scheduler.Configuration.FRAMEWORK_SECRET_PATH, SECRET_FOLDER + FRAMEWORKPASSWD, org.apache.mesos.elasticsearch.scheduler.Configuration.ELASTICSEARCH_CPU, "0.2" ); return createContainerCmd; }
Example #14
Source File: DockerTemplateBaseTest.java From docker-plugin with MIT License | 6 votes |
private static void testFillContainerCapabilitiesToDrop(final String imageName, final List<String> capabilitiesToSet, final boolean capabilitiesIsExpectedToBeSet, final List<Capability> expectedCapabilities) { // Given final CreateContainerCmd mockCmd = mock(CreateContainerCmd.class); final DockerTemplateBase instanceUnderTest = new DockerTemplateBase(imageName); instanceUnderTest.setCapabilitiesToDrop(capabilitiesToSet); // When instanceUnderTest.fillContainerConfig(mockCmd); // Then if (capabilitiesIsExpectedToBeSet) { verify(mockCmd, times(1)).withCapDrop(expectedCapabilities); } else { verify(mockCmd, never()).withCapDrop(anyList()); } }
Example #15
Source File: CustomSettingsDockerSystemTest.java From elasticsearch with Apache License 2.0 | 6 votes |
@Override protected CreateContainerCmd dockerCommand() { return dockerClient .createContainerCmd(TEST_CONFIG.getSchedulerImageName()) .withName(TEST_CONFIG.getSchedulerName() + "_" + clusterId + "_" + new SecureRandom().nextInt()) .withBinds(new Bind(CUSTOM_CONFIG_PATH, new Volume(CUSTOM_CONFIG_PATH), AccessMode.ro)) .withEnv("JAVA_OPTS=-Xms128m -Xmx256m") .withCmd( ElasticsearchCLIParameter.ELASTICSEARCH_SETTINGS_LOCATION, configPath, ZookeeperCLIParameter.ZOOKEEPER_MESOS_URL, getZookeeperMesosUrl(), ELASTICSEARCH_CPU, "0.25", ELASTICSEARCH_RAM, "256", ELASTICSEARCH_DISK, "10", USE_IP_ADDRESS, "true" ); }
Example #16
Source File: DockerClientFactory.java From testcontainers-java with MIT License | 6 votes |
private <T> T runInsideDocker(DockerClient client, Consumer<CreateContainerCmd> createContainerCmdConsumer, BiFunction<DockerClient, String, T> block) { checkAndPullImage(client, TINY_IMAGE); CreateContainerCmd createContainerCmd = client.createContainerCmd(TINY_IMAGE) .withLabels(DEFAULT_LABELS); 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 e) { log.debug("Swallowed exception while removing container", e); } } }
Example #17
Source File: MesosAgentContainer.java From minimesos with Apache License 2.0 | 5 votes |
@Override protected CreateContainerCmd dockerCommand() { ArrayList<ExposedPort> exposedPorts = new ArrayList<>(); exposedPorts.add(new ExposedPort(getServicePort())); ArrayList<Integer> resourcePorts = ResourceUtil.parsePorts(getResources()); for (Integer port : resourcePorts) { exposedPorts.add(new ExposedPort(port)); } return getBaseCommand() .withExposedPorts(exposedPorts.toArray(new ExposedPort[exposedPorts.size()])); }
Example #18
Source File: SchedulerMainSystemTest.java From elasticsearch with Apache License 2.0 | 5 votes |
@Test public void ensureMainFailsIfNoHeap() throws Exception { CreateContainerCmd createCommand = getCreateContainerCmd(""); String containerId = startContainer(createCommand); waitForSchedulerStartStop(containerId); String containerLog = containerLog(containerId); LOGGER.debug(containerLog); assertTrue(containerLog.contains("Exception")); assertTrue(containerLog.contains("heap")); }
Example #19
Source File: SchedulerMainSystemTest.java From elasticsearch with Apache License 2.0 | 5 votes |
@Test public void ensureMainFailsIfInvalidHeap() throws Exception { CreateContainerCmd createCommand = getCreateContainerCmd("-Xms128s1m -Xmx256f5m"); String containerId = startContainer(createCommand); waitForSchedulerStartStop(containerId); String containerLog = containerLog(containerId); LOGGER.debug(containerLog); assertTrue(containerLog.contains("Invalid initial heap size")); }
Example #20
Source File: AlpineContainer.java From elasticsearch with Apache License 2.0 | 5 votes |
@Override protected CreateContainerCmd dockerCommand() { return dockerClient .createContainerCmd(ALPINE_IMAGE_NAME) .withName("Alpine" + "_" + new SecureRandom().nextInt()) .withBinds(new Bind(hostVolume, new Volume(containerVolume))) .withAttachStdout(true) .withAttachStderr(true) .withCmd(cmd); }
Example #21
Source File: DifferentESVersionSystemTest.java From elasticsearch with Apache License 2.0 | 5 votes |
@Override protected CreateContainerCmd dockerCommand() { return dockerClient .createContainerCmd(TEST_CONFIG.getSchedulerImageName()) .withName(TEST_CONFIG.getSchedulerName() + "_" + new SecureRandom().nextInt()) .withEnv("JAVA_OPTS=-Xms128m -Xmx256m") .withCmd( ZookeeperCLIParameter.ZOOKEEPER_MESOS_URL, getZookeeperMesosUrl(), ELASTICSEARCH_RAM, Integer.toString(TEST_CONFIG.getElasticsearchMemorySize()), ELASTICSEARCH_CPU, "0.1", ELASTICSEARCH_DISK, "150", EXECUTOR_IMAGE, image, FRAMEWORK_USE_DOCKER, "true"); }
Example #22
Source File: DockerTemplateBase.java From docker-plugin with MIT License | 5 votes |
@Nonnull private static HostConfig hostConfig(CreateContainerCmd containerConfig) { final HostConfig hc = containerConfig.getHostConfig(); if (hc == null) { throw new IllegalStateException("Can't find " + HostConfig.class.getCanonicalName() + " within " + CreateContainerCmd.class.getCanonicalName() + " " + containerConfig); } return hc; }
Example #23
Source File: CreateContainerCmdExec.java From docker-java with Apache License 2.0 | 5 votes |
@Override protected CreateContainerResponse execute(CreateContainerCmd command) { WebTarget webResource = getBaseResource().path("/containers/create"); if (command.getName() != null) { webResource = webResource.queryParam("name", command.getName()); } LOGGER.trace("POST: {} ", webResource); return resourceWithOptionalAuthConfig(command.getAuthConfig(), webResource.request()) .accept(MediaType.APPLICATION_JSON) .post(command, new TypeReference<CreateContainerResponse>() { }); }
Example #24
Source File: DummyFrameworkContainer.java From logstash with Apache License 2.0 | 5 votes |
@Override protected CreateContainerCmd dockerCommand() { return dockerClient .createContainerCmd(BUSYBOX_IMAGE + ":" + TAG) .withName(name) .withTty(true) .withCmd("sh"); }
Example #25
Source File: DockerImageExecutor.java From hawkular-apm with Apache License 2.0 | 5 votes |
@Override public List<String> run(TestEnvironment testEnvironment) { String hostOsMountDir = System.getProperties().getProperty("buildDirectory"); CreateContainerCmd containerBuilder = dockerClient.createContainerCmd(testEnvironment.getImage()) .withBinds(new Bind(hostOsMountDir, new Volume(Constants.HAWKULAR_APM_AGENT_DIRECTORY), AccessMode.ro, SELContext.shared), new Bind(scenarioDirectory, new Volume(Constants.HAWKULAR_APM_TEST_DIRECTORY), AccessMode.ro, SELContext.shared)) .withExtraHosts(Constants.HOST_ADDED_TO_ETC_HOSTS + ":" + apmBindAddress); if (userDefinedNetwork) { if (network == null) { throw new IllegalStateException("Create network before running environment"); } containerBuilder.withNetworkMode(network.getName()); } containerBuilder.withEnv(apmEnvVariables(testEnvironment.getType())); if (testEnvironment.isPull()) { log.info("Pulling image..."); dockerClient.pullImageCmd(testEnvironment.getImage()).exec(new PullImageResultCallback()).awaitSuccess(); } CreateContainerResponse containerResponse = containerBuilder.exec(); log.info(String.format("Starting docker container: %s", containerResponse)); try { dockerClient.startContainerCmd(containerResponse.getId()).exec(); } catch (DockerException ex) { log.severe(String.format("Could not create or start docker container: %s", containerResponse)); throw new EnvironmentException("Could not create or start docker container.", ex); } return Arrays.asList(containerResponse.getId()); }
Example #26
Source File: DockerMachineConfiguratorTest.java From roboconf-platform with Apache License 2.0 | 5 votes |
@Test( expected = TargetException.class ) public void testCreateContainer_exception() throws Exception { CreateContainerCmd ccc = Mockito.mock( CreateContainerCmd.class ); Mockito.when( ccc.withEnv( Mockito.anyListOf( String.class ))).thenReturn( ccc ); Mockito.when( ccc.withName( Mockito.anyString())).thenReturn( ccc ); Mockito.when( this.dockerClient.createContainerCmd( Mockito.anyString())).thenThrow( new RuntimeException( "for test" )); final String imageId = "toto"; this.configurator.createContainer( imageId ); }
Example #27
Source File: DockerUtilsTest.java From roboconf-platform with Apache License 2.0 | 5 votes |
@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())); }
Example #28
Source File: DockerComputerConnector.java From docker-plugin with MIT License | 5 votes |
/** * Ensure container is already set with a command, or set one to make it wait indefinitely */ protected void ensureWaiting(@Nonnull CreateContainerCmd cmd) { final String[] cmdAlreadySet = cmd.getCmd(); if (cmdAlreadySet == null || cmdAlreadySet.length == 0) { // no command has been set, we need one that will just hang. Typically "sh" waiting for stdin cmd.withCmd("/bin/sh") .withTty(true) .withAttachStdin(false); } }
Example #29
Source File: DockerUtilsTest.java From roboconf-platform with Apache License 2.0 | 5 votes |
@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 ); }
Example #30
Source File: SchedulerMainSystemTest.java From elasticsearch with Apache License 2.0 | 5 votes |
private CreateContainerCmd getCreateContainerCmd(String heapString) { return getDockerClient() .createContainerCmd(getTestConfig().getSchedulerImageName()) .withEnv("JAVA_OPTS=" + heapString) .withCmd( ZookeeperCLIParameter.ZOOKEEPER_MESOS_URL, "zk://" + "noIP" + ":2181/mesos" ); }