user2810472
user2810472

Reputation: 135

How can I call docker daemon of the host-machine from a container?

Here is exactly what I need. I already have a project which is starting up a particular set of docker images and it works completely fine.

But I want to create another image, which is particularly to build this project from the scratch having all the dependencies inside. So, the problem is, when building, to create docker images, we need to access the docker daemon running on the host machine from the building container.

Is there any way of doing this?

Upvotes: 3

Views: 3561

Answers (2)

OscarAkaElvis
OscarAkaElvis

Reputation: 5714

You can let the container access to the host's docker daemon through the docker socket and "tricking" it to have the docker executable inside the container without installing docker inside it. Just on this way (with an Ubuntu-Xenial container for the example):

docker run --name dockerInsideContainer -ti -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker ubuntu:xenial

Inside this, you can launch any docker command like for example docker images to check it's working.

If you see an error like this: docker: error while loading shared libraries: libltdl.so.7: cannot open shared object file: No such file or directory you should install inside the container a package called libltdl7. So for example you can create a Dockerfile for the container or installing it directly on run:

FROM ubuntu:xenial
apt update
apt install -y libltdl7

or

docker run --name dockerInsideContainer -ti -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker ubuntu:xenial bash -c "apt update && apt install libltdl7 && bash"

Hope it helps

Upvotes: 2

larsks
larsks

Reputation: 311526

If you need to access docker on the host from inside a container, you can simply expose the Docker socket inside the container using a host mount (-v /host/path:/container/path on the docker run command line).

For example, if I start a new fedora container exposing the docker socket on my host:

$ docker run -it -v /var/run/docker.sock:/var/run/docker.sock fedora bash

Then install docker inside the container:

[root@d28650013548 /]# yum -y install docker
...many lines elided...

I can now talk to docker on my host:

[root@d28650013548 /]# docker info
Containers: 6
 Running: 1
 Paused: 0
 Stopped: 5
Images: 530
Server Version: 17.05.0-ce
...

Upvotes: 9

Related Questions