Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
How To Create And Start Docker Container with specific port, detached mode using docker-java

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.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

about 4 years ago · Santiago Trujillo Report

0

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.

about 4 years ago · Santiago Trujillo Report

0

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();`
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!