当前位置: 首页>>代码示例>>Java>>正文


Java CreateContainerCmd.withVolumes方法代码示例

本文整理汇总了Java中com.github.dockerjava.api.command.CreateContainerCmd.withVolumes方法的典型用法代码示例。如果您正苦于以下问题:Java CreateContainerCmd.withVolumes方法的具体用法?Java CreateContainerCmd.withVolumes怎么用?Java CreateContainerCmd.withVolumes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.github.dockerjava.api.command.CreateContainerCmd的用法示例。


在下文中一共展示了CreateContainerCmd.withVolumes方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: 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);
    }
}
 
开发者ID:ssouporg,项目名称:denv,代码行数:57,代码来源:DockerContainerManager.java


注:本文中的com.github.dockerjava.api.command.CreateContainerCmd.withVolumes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。