10

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Youssef El Rhailani
  • 199
  • 1
  • 4
  • 19
  • 1
    why a java application should start a container? This is not docker design as far as I know – Bestbug Jan 10 '16 at 14:46
  • "this solution doesn't work" is very subjective. Give more details, like the returned error, for example. – gustavohenke Jan 10 '16 at 14:48
  • [This](http://fr.slideshare.net/julienbarbier42/building-a-saas-using-docker) is the design I'm trying to make, see page N° 15 @Bestbug – Youssef El Rhailani Jan 10 '16 at 14:51
  • 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. @gustavohenke – Youssef El Rhailani Jan 10 '16 at 14:55
  • Is the first time you run docker on windows? Could be wrong gopath in this case – Bestbug Jan 10 '16 at 14:56
  • 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" – OneCricketeer Jan 10 '16 at 15:10
  • 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 ? @Bestbug – Youssef El Rhailani Jan 10 '16 at 19:41

3 Answers3

8

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

rakesh kotian
  • 118
  • 2
  • 8
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");
Thomasleveil
  • 95,867
  • 15
  • 119
  • 113
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/

Youssef El Rhailani
  • 199
  • 1
  • 4
  • 19