How to run Docker container from Java project?
Asked Answered
H

3

11

How to run a docker container from java code? I'm trying to make a SaaS using docker, once the user logs in, I should start a memcached container from java code, this solution doesn't work:

Process p = Runtime.getRuntime().exec("docker images");

Docker cmds run usually on git bash, not on cmd.
PS: I'm using docker on windows.

Honeycutt answered 10/1, 2016 at 14:35 Comment(7)
why a java application should start a container? This is not docker design as far as I knowDinin
"this solution doesn't work" is very subjective. Give more details, like the returned error, for example.Sokul
This is the design I'm trying to make, see page N° 15 @DininHoneycutt
No errors, when I display the result, the output value is empty, as I stated before, docker cmds run on git bash, and Runtime.getRuntime().exec() is for cmd. @SokulHoneycutt
Is the first time you run docker on windows? Could be wrong gopath in this caseDinin
You should be passing "docker images" as a command to the bash executable if you just said that runs in cmd. And if it does run cmd, then you should get an error saying "Docker isn't a command"Carisacarissa
No, my docker works fine on git bash, but it doesn't work on cmd, I'm trying to call git-bash from java, how to do it ? @DininHoneycutt
M
9

You can do it using https://github.com/docker-java/docker-java . It allows you to build a custom image and run it from java

Morette answered 28/11, 2019 at 21:17 Comment(0)
B
2

I assume you are using Docker Toolbox for Windows.

The docker command does not take a capital D. Maybe try with

Process p = Runtime.getRuntime().exec("docker images");

but as you are probably running this code on Windows, that might work anyway.

Another thing to consider is the value of the DOCKER_HOST environment variable which must be set accordingly to instruct the docker client how to communicate with the docker engine.

In your case the docker client is run on Windows while the docker engine runs inside a virtual machine living in VirtualBox.

The shell provided by Runtime.getRuntime().exec() won't have the DOCKER_HOST environment variable set.

Another way is to use the --host or -H docker client option to specify how to connect to your docker engine:

Process p = Runtime.getRuntime().exec("docker --host=tcp://<some IP>:2376 images");
Beverle answered 10/1, 2016 at 18:23 Comment(4)
Thank you for your help, the second part of the answer was helpful. =)Honeycutt
Instead of executing the docker commands, why not connect to the API directly? Several Java libraries exist that allow you to connect to the docker daemon through its API; docs.docker.com/engine/reference/api/…Fret
Yes --host @BeverleHoneycutt
@Fret Thank you so much for that option, I didn't know about it, it helps a lot really !Honeycutt
H
1

I'm sorry guys, when I open a new shell (client), I have to configure it in order to know how to connect to the docker daemon that is running in the virtualbox. I had to run cmds that set the shell environment, because the quickstart terminal does it automatically. So I had to run the following and then paste the output back into my cmd shell:

docker-machine env --shell cmd default

Now it works perfectly.

Update (thanks to @thaJeztah) : It's better to use Java libraries to connect directly to the docker daemon.
Link to API https://docs.docker.com/engine/reference/api/remote_api_client_libraries/

Honeycutt answered 10/1, 2016 at 23:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.