5

I want to create and run docker using docker java client. I want running something like this :

docker run -d -p 4444:4444 --name selenium-hub selenium/hub:2.53.0

How to implement this command on docker-java client? Here is my code so far :

CreateContainerResponse response = dockerClient.createContainerCmd("selenium/hub")
               .withName(name)
               .exec();

Actually IDK how to specify -d (for running in background). and -p. please help me. sorry I am new in Docker.

Hendrione
  • 225
  • 1
  • 5
  • 18

4 Answers4

7

docker-java version 3.1.0 has moved the withPortBindings method from CreateContainerCmd class to the HostConfig class.

Here is the updated way to do it:

ExposedPort tcp4444 = ExposedPort.tcp(4444);
Ports portBindings = new Ports();
portBindings.bind(tcp4444, Ports.Binding.bindPort(4444));

// create container from image
CreateContainerResponse container = dockerClient.createContainerCmd("selenium/hub:2.53.0")
            .withExposedPorts(tcp4444)
            .withHostConfig(newHostConfig()
                    .withPortBindings(portBindings))
            .withName("selenium-hub")
            .exec();

// start the container
dockerClient.startContainerCmd(container.getId()).exec();

As a side note, I had to go look at the unit tests in the docker-java repository in order to find how to do this. It seems that is the place to go to find working examples.

Noah Solomon
  • 1,251
  • 15
  • 23
3

You can use hostconfig like below to say bind port 1234 (equivalent to -p 1234:1234)

HostConfig hostConfig = HostConfig.newHostConfig().withPortBindings(PortBinding.parse("1234:1234"));
dockerClient.createContainerCmd(..).withHostConfig(hostConfig);
shashi ranjan
  • 380
  • 4
  • 8
2

docker-java has a nice wiki on https://github.com/docker-java/docker-java/wiki. Searching for "port" got me this:

Create new Docker container and start it with exposed ports

ExposedPort tcp22 = ExposedPort.tcp(22);
ExposedPort tcp23 = ExposedPort.tcp(23);

Ports portBindings = new Ports();
portBindings.bind(tcp22, Ports.Binding(11022));
portBindings.bind(tcp23, Ports.Binding(11023));

CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
   .withCmd("true")
   .withExposedPorts(tcp22, tcp23)
   .withPortBindings(portBindings)
   .exec();

I looked at some tests in docker-java, and it looks like you only did half the work for running a container, because you only created the container and didn't start it. Based on what I see in this test (https://github.com/docker-java/docker-java/blob/069987852c842e3bba85ed3325a8877c36f9e87f/src/test/java/com/github/dockerjava/core/command/ExecStartCmdImplTest.java#L69), your code should look something like this:

ExposedPort tcp4444 = ExposedPort.tcp(4444);

Ports portBindings = new Ports();
portBindings.bind(tcp4444, Ports.Binding(4444));

// Create the container (it will not be running)
CreateContainerResponse container = dockerClient.createContainerCmd("selenium/hub")
    .withName(name)
    .withExposedPorts(tcp4444)
    .withPortBindings(portBindings)
    .exec();

// Actually run the container
dockerClient.startContainerCmd(container).exec();

As far as I can tell, there's no reason to explicitly run it in detached mode because it will be started asynchronously by default.

Ede
  • 387
  • 2
  • 10
  • thank you already tried this.. I dont find this method : withDetach(true). that's why I am confused. now my code looks like this : `ExposedPort tcp4444 = ExposedPort.tcp(4444); Ports portBindings = new Ports(); portBindings.bind(tcp4444,Ports.Binding.bindPort(4444)); CreateContainerResponse response = dockerClient. createContainerCmd("selenium/hub") .withName(name) .withExposedPorts(tcp4444) .exec();` – Hendrione Mar 31 '17 at 08:18
  • 1
    It looks like this is out of date again. `withPortBindings` was removed from the create container command. Any chance you know the new syntax? – Noah Solomon Mar 13 '19 at 03:44
2

Found the solution... if someone find a better one please post in here. I already modify the code to be like this :

  ExposedPort tcp4444 = ExposedPort.tcp(4444);
   Ports portBindings = new Ports();
   portBindings.bind(tcp4444,Ports.Binding.bindPort(4444));

   CreateContainerResponse response = dockerClient.
           createContainerCmd("selenium/hub")
           .withName(name)
           .withImage("selenium/hub:"+version)
           .withExposedPorts(tcp4444)
           .withPortBindings(portBindings)
           .withAttachStderr(false)
           .withAttachStdin(false)
           .withAttachStdout(false)
           .exec();`
Hendrione
  • 225
  • 1
  • 5
  • 18
  • 2
    It looks like this is out of date again. `withPortBindings` was removed from the create container command. Any chance you know the new syntax? – Noah Solomon Mar 13 '19 at 03:44