【问题标题】:How To Create And Start Docker Container with specific port, detached mode using docker-java如何使用 docker-java 创建和启动具有特定端口、分离模式的 Docker 容器
【发布时间】:2017-08-25 09:53:35
【问题描述】:

我想使用 docker java 客户端创建和运行 docker。我想运行这样的东西:

docker run -d -p 4444:4444 --name selenium-hub selenium/hub:2.53.0

如何在 docker-java 客户端实现这个命令?到目前为止,这是我的代码:

CreateContainerResponse response = dockerClient.createContainerCmd("selenium/hub")
               .withName(name)
               .exec();

实际上IDK如何指定-d(用于在后台运行)。和-p。 请帮我。抱歉,我是 Docker 新手。

【问题讨论】:

    标签: docker docker-java


    【解决方案1】:

    docker-java 在https://github.com/docker-java/docker-java/wiki 上有一个不错的 wiki。搜索“端口”让我得到了这个:

    创建新的 Docker 容器并使用暴露的端口启动它

    ExposedPort tcp22 = ExposedPort.tcp(22);
    ExposedPort tcp23 = ExposedPort.tcp(23);
    
    Ports portBindings = new Ports();
    portBindings.bind(tcp22, Ports.Binding(11022));
    portBindings.bind(tcp23, Ports.Binding(11023));
    
    CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
       .withCmd("true")
       .withExposedPorts(tcp22, tcp23)
       .withPortBindings(portBindings)
       .exec();
    

    我查看了 docker-java 中的一些测试,看起来你只完成了运行容器的一半工作,因为你只是创建容器而没有启动它。根据我在此测试 (https://github.com/docker-java/docker-java/blob/069987852c842e3bba85ed3325a8877c36f9e87f/src/test/java/com/github/dockerjava/core/command/ExecStartCmdImplTest.java#L69) 中看到的内容,您的代码应如下所示:

    ExposedPort tcp4444 = ExposedPort.tcp(4444);
    
    Ports portBindings = new Ports();
    portBindings.bind(tcp4444, Ports.Binding(4444));
    
    // Create the container (it will not be running)
    CreateContainerResponse container = dockerClient.createContainerCmd("selenium/hub")
        .withName(name)
        .withExposedPorts(tcp4444)
        .withPortBindings(portBindings)
        .exec();
    
    // Actually run the container
    dockerClient.startContainerCmd(container).exec();
    

    据我所知,没有理由在分离模式下显式运行它,因为默认情况下它将异步启动。

    【讨论】:

    • 谢谢你已经尝试过了。我找不到这个方法:withDetach(true)。这就是为什么我很困惑。现在我的代码看起来像这样:ExposedPort tcp4444 = ExposedPort.tcp(4444); Ports portBindings = new Ports(); portBindings.bind(tcp4444,Ports.Binding.bindPort(4444)); CreateContainerResponse response = dockerClient. createContainerCmd("selenium/hub") .withName(name) .withExposedPorts(tcp4444) .exec();
    • 这似乎又过时了。 withPortBindings 已从创建容器命令中删除。你有机会知道新语法吗?
    【解决方案2】:

    找到解决方案...如果有人找到更好的解决方案,请在此处发布。我已经将代码修改为这样:

      ExposedPort tcp4444 = ExposedPort.tcp(4444);
       Ports portBindings = new Ports();
       portBindings.bind(tcp4444,Ports.Binding.bindPort(4444));
    
       CreateContainerResponse response = dockerClient.
               createContainerCmd("selenium/hub")
               .withName(name)
               .withImage("selenium/hub:"+version)
               .withExposedPorts(tcp4444)
               .withPortBindings(portBindings)
               .withAttachStderr(false)
               .withAttachStdin(false)
               .withAttachStdout(false)
               .exec();`
    

    【讨论】:

    • 这似乎又过时了。 withPortBindings 已从创建容器命令中删除。你有机会知道新语法吗?
    【解决方案3】:

    docker-java 3.1.0 版已将withPortBindings 方法从CreateContainerCmd 类移至HostConfig 类。

    这是更新的方法:

    ExposedPort tcp4444 = ExposedPort.tcp(4444);
    Ports portBindings = new Ports();
    portBindings.bind(tcp4444, Ports.Binding.bindPort(4444));
    
    // create container from image
    CreateContainerResponse container = dockerClient.createContainerCmd("selenium/hub:2.53.0")
                .withExposedPorts(tcp4444)
                .withHostConfig(newHostConfig()
                        .withPortBindings(portBindings))
                .withName("selenium-hub")
                .exec();
    
    // start the container
    dockerClient.startContainerCmd(container.getId()).exec();
    

    附带说明一下,我必须查看 docker-java 存储库中的单元测试才能找到如何执行此操作。这似乎是寻找工作示例的地方。

    【讨论】:

      【解决方案4】:

      你可以像下面这样使用hostconfig来绑定端口1234(相当于-p 1234:1234)

      HostConfig hostConfig = HostConfig.newHostConfig().withPortBindings(PortBinding.parse("1234:1234"));
      dockerClient.createContainerCmd(..).withHostConfig(hostConfig);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-14
        • 2018-12-30
        • 1970-01-01
        • 2020-10-25
        • 1970-01-01
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多