本文整理汇总了Java中com.github.dockerjava.api.command.CreateContainerCmd.withCmd方法的典型用法代码示例。如果您正苦于以下问题:Java CreateContainerCmd.withCmd方法的具体用法?Java CreateContainerCmd.withCmd怎么用?Java CreateContainerCmd.withCmd使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.github.dockerjava.api.command.CreateContainerCmd
的用法示例。
在下文中一共展示了CreateContainerCmd.withCmd方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: beforeContainerCreated
import com.github.dockerjava.api.command.CreateContainerCmd; //导入方法依赖的package包/类
@Override
public void beforeContainerCreated(DockerAPI api, String workdir, CreateContainerCmd cmd) throws IOException, InterruptedException {
List<String> args = new ArrayList<>();
if (StringUtils.isNotBlank(jnlpLauncher.tunnel)) {
args.addAll(Arrays.asList("-tunnel", jnlpLauncher.tunnel));
}
String nodeName = DockerTransientNode.nodeName(cmd.getName());
args.addAll(Arrays.asList(
"-url", StringUtils.isEmpty(jenkinsUrl) ? Jenkins.getInstance().getRootUrl() : jenkinsUrl,
JnlpSlaveAgentProtocol.SLAVE_SECRET.mac(nodeName),
nodeName));
cmd.withCmd(args.toArray(new String[args.size()]));
String vmargs = jnlpLauncher.vmargs;
if (StringUtils.isNotBlank(vmargs)) {
cmd.withEnv("JAVA_OPT=" + vmargs.trim());
}
if (StringUtils.isNotBlank(user)) {
cmd.withUser(user);
}
}
示例2: run
import com.github.dockerjava.api.command.CreateContainerCmd; //导入方法依赖的package包/类
@Override
protected String run() throws Exception {
CreateContainerCmd createCmd = dockerClient.createContainerCmd(imageId);
createCmd.withAttachStderr(true).withAttachStdout(true).withName(params.getName());
createCmd.withNetworkMode(bridgeNetworkName.toString());
if (!params.getBinds().isEmpty()) {
createCmd.withBinds(createBinds());
}
if (params.getPortBinds().isEmpty()) {
createCmd.withPublishAllPorts(true);
} else {
createCmd.withExposedPorts(createExposedPorts()).withPortBindings(createPorts());
}
if (params.getCommand().isPresent()) {
createCmd.withCmd(params.getCommand().get());
}
if (!params.getEnvs().isEmpty()) {
String[] envs = params.getEnvs().stream().map(env -> String.format("%s='%s'", env.getName(), env.getValue().get())).toArray(String[]::new);
createCmd.withEnv(envs);
}
try {
return createCmd.exec().getId();
} finally {
createCmd.close();
}
}
示例3: create
import com.github.dockerjava.api.command.CreateContainerCmd; //导入方法依赖的package包/类
@Override
public void create() {
super.create();
String imageId = getImageId();
log.info("Container [" + getId() + "] : Creating container with image " + imageId);
List<ExposedPort> exposedPorts = getExposedPorts();
Map<Integer, Integer> portMappings = getPortsMapping();
Ports portBindings = new Ports();
for (ExposedPort exposedPort : exposedPorts) {
Integer mappedPort = portMappings.get(exposedPort.getPort());
if (mappedPort != null) {
portBindings.bind(exposedPort, Ports.Binding.bindPort(mappedPort));
}
}
String tag = getPropertyAsString("tag", "latest");
log.info("Container [" + getId() + "] : Pulling image " + imageId);
try {
dockerClient.pullImageCmd(imageId).withTag(tag).exec(new PullImageResultCallback()).awaitCompletion();
} catch (InterruptedException e) {
throw new InvalidOperationExecutionException("Pull interrupted", e);
}
log.info("Container [" + getId() + "] : Pulled image " + imageId);
CreateContainerCmd createContainerCmd = dockerClient.createContainerCmd(imageId + ":" + tag)
.withStdinOpen(Boolean.parseBoolean(getPropertyAsString("interactive", "true")))
.withName(DockerUtil.normalizeResourceName(config.getDeploymentName() + "_" + getId()))
.withExposedPorts(exposedPorts.toArray(new ExposedPort[exposedPorts.size()]))
.withPortBindings(portBindings);
if (volumes != null && !volumes.isEmpty()) {
createContainerCmd.withBinds(volumes.stream().map(volume -> new Bind(volume.getVolumeId(), new com.github.dockerjava.api.model.Volume(volume.getLocation()))).collect(Collectors.toList()));
}
List<String> commands = (List<String>) getProperty("commands");
if (commands != null && !commands.isEmpty()) {
createContainerCmd.withCmd(commands);
}
containerId = createContainerCmd.exec().getId();
dockerExecutor = new DockerExecutor(dockerClient, containerId, Boolean.parseBoolean(getPropertyAsString("elevate_privilege")));
log.info("Container [" + getId() + "] : Created container with id " + containerId);
}
示例4: beforeContainerCreated
import com.github.dockerjava.api.command.CreateContainerCmd; //导入方法依赖的package包/类
@Override
public void beforeContainerCreated(DockerAPI api, String workdir, CreateContainerCmd cmd) throws IOException, InterruptedException {
// TODO define a strategy for SSHD process configuration so we support more than openssh's sshd
if (cmd.getCmd() == null || cmd.getCmd().length == 0) {
if (sshKeyStrategy.getInjectedKey() != null) {
cmd.withCmd("/usr/sbin/sshd", "-D", "-p", String.valueOf(port),
// override sshd_config to force retrieval of InstanceIdentity public for as authentication
"-o", "AuthorizedKeysCommand=/root/authorized_key",
"-o", "AuthorizedKeysCommandUser=root"
);
} else {
cmd.withCmd("/usr/sbin/sshd", "-D", "-p", String.valueOf(port));
}
}
cmd.withPortSpecs(port+"/tcp");
final PortBinding sshPortBinding = PortBinding.parse(":" + port);
final Ports portBindings = cmd.getPortBindings();
if(portBindings != null) {
portBindings.add(sshPortBinding);
cmd.withPortBindings(portBindings);
} else {
cmd.withPortBindings(sshPortBinding);
}
cmd.withExposedPorts(ExposedPort.parse(port+"/tcp"));
}
示例5: build
import com.github.dockerjava.api.command.CreateContainerCmd; //导入方法依赖的package包/类
@Override
public CreateContainerCmd build(TestDescriptor td, CreateContainerCmd cmd, Command t) {
return cmd.withCmd(t.value());
}
示例6: runContainer
import com.github.dockerjava.api.command.CreateContainerCmd; //导入方法依赖的package包/类
private Process runContainer() {
try
{
DockerClient client = 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);
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) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
示例7: createContainer
import com.github.dockerjava.api.command.CreateContainerCmd; //导入方法依赖的package包/类
@Override
public Container createContainer(Environment env, String containerName, ImageConfiguration imageConf, Image image, String command) throws ContainerizationException {
CreateContainerResponse containerCreateResponse = null;
InspectContainerResponse containerInspectResponse = null;
try {
CreateContainerCmd createContainerCommand = getDockerClient().createContainerCmd(image.getId());
createContainerCommand.withName(containerName);
// keep the container alive
//containerConfig.setCmd(new String[]{"/bin/bash", "/home/synaptiq/run_tomcat.sh", dockerHost, "" + mysqlPort});
if (command != null) {
createContainerCommand.withCmd(command.split(" "));
}
List<ExposedPort> exposedPorts = new ArrayList<ExposedPort>();
if (imageConf.getPorts() != null) {
for (PortConfiguration port : imageConf.getPorts()) {
exposedPorts.add(new ExposedPort("tcp", port.getContainerPort()));
}
}
createContainerCommand.withExposedPorts(exposedPorts.toArray(new ExposedPort[0]));
createContainerCommand.withTty(false);
List<Volume> volumesList = new ArrayList<Volume>();
if (imageConf.getVolumes() != null) {
for (VolumeConfiguration volumeConfiguration : imageConf.getVolumes()) {
volumesList.add(new Volume(volumeConfiguration.getPath()));
}
}
createContainerCommand.withVolumes(volumesList.toArray(new Volume[0]));
if (imageConf.getEnvironment() != null) {
List<String> environmentVariables = new ArrayList<String>();
for (EnvironmentVariableConfiguration evc : imageConf.getEnvironment()) {
environmentVariables.add(evc.getVariable() + "=" + evc.getValue());
}
createContainerCommand.withEnv(environmentVariables.toArray(new String[0]));
}
containerCreateResponse = createContainerCommand.exec();
containerInspectResponse = getDockerClient().inspectContainerCmd(containerCreateResponse.getId()).exec();
DockerContainer dockerContainer = new DockerContainer(containerCreateResponse, containerInspectResponse, image);
return dockerContainer;
} catch(Exception ex) {
if (containerCreateResponse != null) {
try {
getDockerClient().killContainerCmd(containerCreateResponse.getId()).exec();
getDockerClient().removeContainerCmd(containerCreateResponse.getId()).exec();
}
catch(DockerException e) {
throw new ContainerizationException(e);
}
}
throw new ContainerizationException(ex);
}
}
示例8: call
import com.github.dockerjava.api.command.CreateContainerCmd; //导入方法依赖的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;
}