Spotify docker 客户端:如何实现 --network=host 功能?

Spotify docker client: How to achieve --network=host functionality?

要在容器中使用主机网络,可以执行 docker run --network=host image。我如何使用此 API?

实现它

我使用了以下代码并且对我有效(版本 8.14.3):

final ContainerConfig containerConfig = ContainerConfig.builder()  
    .hostConfig(HostConfig.builder().networkMode("host").build())
    .image("helloworldjob")
    .build();
final ContainerCreation creation = docker.createContainer(containerConfig, "image");
final String id = creation.id();
try {
    docker.startContainer(id);
    final ContainerExit exit = docker.waitContainer(id);
    assertThat(exit.statusCode()).isEqualTo(0);
} finally {
    docker.removeContainer(id);
}