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

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…?

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

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!

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?

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

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

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

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.

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.

Note that this way, you’re talking to the host’s docker deamon. Some things like bind mounts won’t be working in the expected way - pathes refer to the host filesystem, not the calling container’s.

–mtx