shekhargulati / rx-docker-client

RxJava based Docker REST API client for the JVM
MIT License
61 stars 8 forks source link

Port binding with same exposed port and different host port #86

Closed arvindk-egnaroinc closed 8 years ago

arvindk-egnaroinc commented 8 years ago

I am facing trouble while try to bind host and container port with different host port. I want container exposed port should be same but host port should be different.

While trying below code, it's working fine.

RxDockerClient client = RxDockerClient.fromDefaultEnv(); final String[] exposedPorts = new String[]{"8080/tcp"}; final String[] hostPorts = new String[]{"8080/tcp"};

    final Map<String, List<PortBinding>> portBindings = new HashMap<>();
    for (String hostPort : hostPorts) {
        List<PortBinding> hostPortBinding = new ArrayList<>();
        hostPortBinding.add(PortBinding.of("0.0.0.0", hostPort));
        portBindings.put(hostPort, hostPortBinding);
    }
    final HostConfig hostConfig = new HostConfigBuilder().setPortBindings(portBindings).createHostConfig();
    DockerContainerRequest request = new DockerContainerRequestBuilder()
            .setImage("arvindk/ctp-web")
            .setAttachStdin(true)
            .addExposedPort(exposedPorts)
            .setHostConfig(hostConfig)
            .setTty(true)
            .createDockerContainerRequest();
    DockerContainerResponse response = client.createContainer(request, "");
    client.startContainer(response.getId()); 

But while trying below code binding is not happening.

RxDockerClient client = RxDockerClient.fromDefaultEnv(); final String[] exposedPorts = new String[]{"8080/tcp"}; final String[] hostPorts = new String[]{"7080/tcp"};

    final Map<String, List<PortBinding>> portBindings = new HashMap<>();
    for (String hostPort : hostPorts) {
        List<PortBinding> hostPortBinding = new ArrayList<>();
        hostPortBinding.add(PortBinding.of("0.0.0.0", hostPort));
        portBindings.put(hostPort, hostPortBinding);
    }
    final HostConfig hostConfig = new HostConfigBuilder().setPortBindings(portBindings).createHostConfig();
    DockerContainerRequest request = new DockerContainerRequestBuilder()
            .setImage("arvindk/ctp-web")
            .setAttachStdin(true)
            .addExposedPort(exposedPorts)
            .setHostConfig(hostConfig)
            .setTty(true)
            .createDockerContainerRequest();
    DockerContainerResponse response = client.createContainer(request, "");
    client.startContainer(response.getId()); 

I am doing this for run multiple instance of an application on different host port. Later it will be dynamic port available on host. I can't change exposed port since I am accessing my application from docker container through noVnc available in my docker container & it is listening on fixed port(8080 defined in my supervisor file of docker image).

Could you please help me out to resolve this issue?

shekhargulati commented 8 years ago

@arvindk-egnaroinc are you getting some error? I tried the example code shown above and it worked fine. Also, which version of rx-docker-client are you using?