我无法在docker中为容器设置publicPort和privatePort。
我为java创建了带有java库docker-client
的docker容器
https://github.com/spotify/docker-client
我已经在本地创建了一个docker图像“mobillogix / selenium-chrome”。
DockerClient docker = DefaultDockerClient.fromEnv()。build();
// Bind container ports to host ports
final String[] ports = {"44444", "59004"};
final String[] ports1 = {"4444", "5900"};
final Map<String, List<PortBinding>> portBindings = new HashMap<String, List<PortBinding>>();
for (String port : ports) {
List<PortBinding> hostPorts = new ArrayList<PortBinding>();
hostPorts.add(PortBinding.of("0.0.0.0", port));
portBindings.put(port, hostPorts);
}
final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();
final ContainerConfig containerConfig = ContainerConfig.builder()
.hostConfig(hostConfig)
.image("mobillogix/selenium-chrome").exposedPorts(ports1)
.build();
final ContainerCreation creation = docker.createContainer(containerConfig);
final String id = creation.id();
// Start container
docker.startContainer(id);
final List<Container> containers = docker.listContainers();
webDriver();
// Kill container
docker.killContainer(id);
// Remove container
docker.removeContainer(id);
此代码创建容器:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6beec961dfcf mobillogix/selenium-chrome "/opt/bin/entry_point" 4 seconds ago Up 2 seconds 4444/tcp, 5900/tcp elegant_kare
带端口
4444 / tcp,5900 / tcp
但我想创建带端口的容器:
0.0.0.0:44444->4444/tcp,0.0.0.0:59004->5900/tc
答案 0 :(得分:3)
根据documentation,这里的正确语法是:
undefined
你在循环中执行的是:
hostPorts.add(PortBinding.of("0.0.0.0", 44444));
portBindings.put(4444, hostPorts);
hostPorts.add(PortBinding.of("0.0.0.0", 59004));
portBindings.put(5900, hostPorts);