从docker容器内部连接到docker守护进程

编程入门 行业动态 更新时间:2024-10-28 21:19:34
本文介绍了从docker容器内部连接到docker守护进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试配置docker守护进程,以便我可以从启动的docker容器内部连接到它。.

Im trying configure the docker daemon so i can connect to it from inside the docker containers i start..

所以我更改了/etc/docker/daemon.json

So i changed /etc/docker/daemon.json to

{ "hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"] }

这样我就可以连接但是,当我重新启动docker时,我会得到

So that i connect to it through the docker bridge.. However when i restart docker i get

netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 3728/mysqld tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 24253/redis-server tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3756/nginx tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3634/sshd tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 3756/nginx tcp6 0 0 :::8010 :::* LISTEN 4230/apache2 tcp6 0 0 :::9200 :::* LISTEN 26824/java tcp6 0 0 :::9300 :::* LISTEN 26824/java tcp6 0 0 :::22 :::* LISTEN 3634/sshd tcp6 0 0 :::2375 :::* LISTEN 1955/dockerd

所以首先我虽然问题是它在ipv6而不是ipv4上监听。并根据 使docker使用IPv4进行端口绑定 它应该仍然可以正常工作,但不能正常工作。当我尝试

So first i though the issue was the fact that it was listening on ipv6 not ipv4. and according to Make docker use IPv4 for port binding It should all still work but it doesnt.. When i try

telnet 172.17.0.1(docker host) 2375

连接失败,而

telnet 172.17.0.1(docker host) 80

可以运行。如何连接到在主机上运行的Docker?我在Ubuntu 14.04.5 docker版本上运行的版本:17.06.2-ce

works. How can i connect to docker running on the host machine? Im running on Ubuntu 14.04.5 docker Version: 17.06.2-ce

推荐答案

您可以启动安装主机docker套接字的容器

You can start your containers mounting the host docker socket into your containers.

docker run -v /var/run/docker.sock:/var/run/docker.sock ...

通过此设置,容器内的Docker客户端将使用主机上的Docker守护进程。您的容器将能够使用在主机中运行的守护程序来构建,运行,推送等。请注意,通过这些设置,主机上所有事情都在发生,因此,如果启动新容器,它们将成为同级容器。

With this setup, Docker clients inside the containers will be using the Docker daemon from the host. Your containers will be able to build, run, push etc. using daemon running in host. Please note that with these setup everything is happening on the host, so if you start new containers they will be "sibling" containers.

编辑

如果使用桥接网络,则可以使用主机IP地址连接到主机上运行的任何服务。

If you are using the bridge network, you can connect to any service running on host machine using host IP address.

例如,我的mysqld在IP为10.0.0.1的主机上运行,​​并且可以从容器中运行

For example, I have mysqld running on my host with IP 10.0.0.1 and from a container I can do

mysql -u user -p -h 10.0.0.1

诀窍是从以下位置找出主机IP地址

The trick is to find out the host IP address from containers.

在Mac的Docker中(我正在运行版本17.07.0)就像连接到特殊主机 docker.for.mac.localhost一样简单。

In Docker for Mac (I am running version 17.07.0) is as simple as connecting to the special host "docker.for.mac.localhost"

另一种选择是向回送接口添加别名IP

Another option is to add an alias IP to your loopback interface

sudo ifconfig lo0 alias 192.168.1.1

然后在运行容器时为此别名IP添加主机

And then when running containers add a host for this alias IP

docker run --rm -ti --add-host host-machine:192.168.1.1 mysql:5.7 bash

通过此设置,在容器内,您应该能够做到

With this setup, inside container you should be able to do

mysql -u user -p -h host-machine

更多推荐

从docker容器内部连接到docker守护进程

本文发布于:2023-10-14 12:29:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1491061.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   容器   进程   docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!