如何从容器调用主机的docker守护进程? [英] How can I call docker daemon of the host-machine from a container?
问题描述
这正是我所需要的.我已经有一个项目正在启动一组特定的docker映像,并且可以正常运行.
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.
但是我想创建另一个图像,特别是要从头开始构建包含所有依赖项的该项目.因此,问题是,在构建时,要创建docker映像,我们需要从构建容器访问在主机上运行的docker守护进程.
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.
有什么办法吗?
推荐答案
如果您需要从容器内部访问主机上的docker,只需使用主机挂载(-在
. docker run
命令行上访问/host/path:/container/path
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).
例如,如果我启动一个新的 fedora
容器,将Docker套接字暴露在主机上:
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
然后在容器内安装 docker
:
[root@d28650013548 /]# yum -y install docker
...many lines elided...
我现在可以在主机上与docker对话了
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
...
这篇关于如何从容器调用主机的docker守护进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!