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

爷,独闯天下 提交于 2021-01-28 18:00:35

问题


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?


回答1:


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
...



回答2:


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



来源:https://stackoverflow.com/questions/48152736/how-can-i-call-docker-daemon-of-the-host-machine-from-a-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!