Java Code Examples for com.github.dockerjava.api.command.CreateContainerCmd#withEnv()
The following examples show how to use
com.github.dockerjava.api.command.CreateContainerCmd#withEnv() .
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: 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 2
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 3
Source File: CreateContainerWorkitemHandler.java From jbpm-work-items with Apache License 2.0 | 4 votes |
public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) { Map<String, Object> results = new HashMap<String, Object>(); try { RequiredParameterValidator.validate(this.getClass(), workItem); String containerName = (String) workItem.getParameter("ContainerName"); String containerImageName = (String) workItem.getParameter("ContainerImageName"); String containerCommand = (String) workItem.getParameter("ContainerCommand"); String containerHostName = (String) workItem.getParameter("ContainerHostName"); String containerEnv = (String) workItem.getParameter("ContainerEnv"); String containerPortBindings = (String) workItem.getParameter("ContainerPortBindings"); String containerBinds = (String) workItem.getParameter("ContainerBinds"); if (dockerClient == null) { DockerClientConnector connector = new DockerClientConnector(); dockerClient = connector.getDockerClient(); } CreateContainerCmd createContainerCmd = dockerClient.createContainerCmd(containerImageName).withName(containerName); if (containerCommand != null) { createContainerCmd = createContainerCmd.withCmd(containerCommand); } if (containerHostName != null) { createContainerCmd = createContainerCmd.withHostName(containerHostName); } if (containerEnv != null) { createContainerCmd = createContainerCmd.withEnv(containerEnv); } if (containerPortBindings != null) { createContainerCmd = createContainerCmd.withPortBindings(PortBinding.parse(containerPortBindings)); } if (containerBinds != null) { createContainerCmd = createContainerCmd.withBinds(Bind.parse(containerBinds)); } CreateContainerResponse container = createContainerCmd.exec(); if (container != null && container.getId() != null) { results.put(RESULTS_DOCUMENT, container.getId()); } workItemManager.completeWorkItem(workItem.getId(), results); } catch (Exception e) { logger.error("Unable to create container: " + e.getMessage()); handleException(e); } }
Example 4
Source File: DockerizedExecHandle.java From gradle-dockerized-test-plugin with Apache License 2.0 | 4 votes |
public Process runContainer() { try { DockerClient client = testExtension.getClient(); CreateContainerCmd createCmd = client.createContainerCmd(testExtension.getImage().toString()) .withTty(false) .withStdinOpen(true) .withStdInOnce(true) .withWorkingDir(directory.getAbsolutePath()); createCmd.withEnv(getEnv()); String user = testExtension.getUser(); if (user != null) createCmd.withUser(user); bindVolumes(createCmd); List<String> cmdLine = new ArrayList<String>(); cmdLine.add(command); cmdLine.addAll(arguments); createCmd.withCmd(cmdLine); invokeIfNotNull(testExtension.getBeforeContainerCreate(), createCmd, client); String containerId = createCmd.exec().getId(); invokeIfNotNull(testExtension.getAfterContainerCreate(), containerId, client); maybeCopyJvmOptionFile(containerId, client); invokeIfNotNull(testExtension.getBeforeContainerStart(), containerId, client); client.startContainerCmd(containerId).exec(); invokeIfNotNull(testExtension.getAfterContainerStart(), containerId, client); if (!client.inspectContainerCmd(containerId).exec().getState().getRunning()) { throw new RuntimeException("Container "+containerId+" not running!"); } Process proc = new DockerizedProcess(client, containerId, testExtension.getAfterContainerStop()); return proc; } catch (Exception e) { LOGGER.error("Failed to create container " + displayName, new RuntimeException(e)); throw new RuntimeException(e); } }