0

To use host network in a container one can execute docker run --network=host image. How can I achieve it using this API?

4

1 回答 1

2

I used the following code and it worked for me (Version 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);
}
于 2019-05-24T14:55:43.937 回答