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

415 views Asked by At

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

1

There are 1 answers

0
Ecki On BEST ANSWER

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