2

我想使用 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 新手。

4

4 回答 4

5

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 存储库中的单元测试才能找到如何执行此操作。似乎这是寻找工作示例的地方。

于 2019-03-14T17:31:15.243 回答
2

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();

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

于 2017-03-31T07:56:51.417 回答
1

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

  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();`
于 2017-03-31T11:16:17.610 回答
0

您可以使用如下所示的 hostconfig 来绑定端口 1234(相当于 -p 1234:1234)

HostConfig hostConfig = HostConfig.newHostConfig().withPortBindings(PortBinding.parse("1234:1234"));
dockerClient.createContainerCmd(..).withHostConfig(hostConfig);
于 2021-11-19T15:39:11.857 回答