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.
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.
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.
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();`