Docker Java API to create a container, port mapping
Use Docker Java API to operate Docker ( Docker Java API )
Introduced in Maven:
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-transport-httpclient5</artifactId>
<version>3.2.2</version>
</dependency>
Docker Java API to create a container using the time has never been able to host
and container
port mapped success.
After searching a lot of information, after trying many times, the following methods are found to be feasible:
PortBinding portBinding = PortBinding.parse(8016 + ":" + 80);
HostConfig hostConfig = HostConfig.newHostConfig()
.withPortBindings(portBinding);
CreateContainerResponse response = dockerClient.createContainerCmd(containerInfo.getImage())
.withName(containerInfo.getName())
.withHostConfig(hostConfig)
.withExposedPorts(ExposedPort.parse(containerInfo.getPrivatePort() + "/tcp"))
.exec();
View Docker container
successfully host 8016
port with container 80
port mapping success.