使用docker-java创建并启动特定端口、分离模式的Docker容器

5

我想使用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();

实际上我不知道如何指定-d(以后台运行)和-p参数。请帮帮我,抱歉我是Docker的新手。

4个回答

7

docker-java 3.1.0版本将CreateContainerCmd类中的withPortBindings方法移动到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存储库中的单元测试,以找到如何做这件事。似乎那是寻找可行示例的地方。


3
您可以使用 hostconfig 像下面这样绑定端口 1234 (相当于 -p 1234:1234)。
HostConfig hostConfig = HostConfig.newHostConfig().withPortBindings(PortBinding.parse("1234:1234"));
dockerClient.createContainerCmd(..).withHostConfig(hostConfig);

2

docker-java在https://github.com/docker-java/docker-java/wiki上有一个很好的维基页面。搜索“port”可以找到以下内容:

Create new Docker container and start it with exposed ports

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(); - Hendrione
1
看起来这又过时了。withPortBindings已从创建容器命令中移除。你知道新的语法吗? - Noah Solomon

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

2
看起来这又过时了。withPortBindings 已从创建容器命令中移除。你知道新的语法吗? - Noah Solomon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接