Docker Community Forums

Share and learn in the Docker community.

Access to docker command from host inside a container with java program


(Zartbigdata) #1

Hi everyone,

I’d like to access to docker command from host (linux VM) inside a container (ubuntu 16.04) which embeds a java program. In this last, it triggers a process which calls ‘docker…’ to start another container. The outcome is actually:
java.io.IOException: Cannot run program “docker” (in directory “.”): error=2, No such file or directory
because it cannot find docker.

I tried with --add-host=… but nothing change.

If anyone has an idea…?


(Think) #2

Three possible solutions:

  1. There is Docker in Docker. But this shouldn’t be used for production or even CI. https://github.com/jpetazzo/dind
  2. You could install docker client inside your container, but use the docker socket from outside. This requires that your container-inside docker client and your outside docker server are version comptabile.
  3. Map the docker socket and the docker binary into the container. This requires that your host and your container OS are compatible

This would map the docker socket within a ubuntu container.
docker run -t -i -v /var/run/docker.sock:/run/docker.sock ubuntu


(Zartbigdata) #3

Hi,

Thanks for your reply.

I investigated your tips. I am on the both last one actually. I have host and containers under ubuntu 16.04. I installed the client. I typed docker version and got client and server information. I typed also service docker status and I got docker is not running. It’s a little bit disturbing because socket makes the link between host and container. I rebuilt my container with java app with fresh install of docker. I ran this container with new command line you gave me. And that’s it. It works like a charm!


(Zartbigdata) #4

Well, now I have got another issue because I call others containers from java process call: the input device is not a TTY. Maybe it needs to run with some options. Any idea?


(Think) #5

did you by accident forgot to add the ‘-t’ for the ‘docker run’? cannot explain it otherwise


(Zartbigdata) #6

I put it. so, it is something else, then.


(Zartbigdata) #7

Well, I found it. In my java program, I needed to declare stdin, stdout and stderr and I worked.


(Zartbigdata) #8

Indeed, if I put -t in commandline to trigger docker, I got: the input device is not a TTY
If I avoid -t, everything is well.


(Rochakbhalla) #9

Hey Hi,

I am trying to execute command “Docker version” using Java from the Eclipse running on my Mac.
But I get error :
java.io.IOException: Cannot run program “docker”: error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at dockertest.LaunchDocker.executeCommand(LaunchDocker.java:34)
at dockertest.LaunchDocker.main(LaunchDocker.java:13)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.(UNIXProcess.java:247)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
… 2 more

However when I run same command in terminal i get proper output.
Please help me what I am doing wrong here.