在Docker中链接容器(RancherOS和命令行)

3
  • 我使用RancherOS运行docker容器
  • 我在GUI上创建了一个容器来运行我的数据库(镜像:mysql,名称:r-mysql-e4e8df05)。不同的容器都使用它。
  • 我可以在GUI上将其他容器链接到它 enter image description here
  • 这次我想要自动化创建和启动Jenkins上的一个容器,但链接不起作用

我的命令:

docker run -d --name=app-that-needs-mysql --link mysql:mysql myimages.mycompany.com/appthatneedsmysql

我遇到了一个错误:
Error response from daemon: Could not get container for mysql

I tried different things:
1)

--link r-mysql-e4e8df05:mysql

错误:

Cannot link to /r-mysql-e4e8df05, as it does not belong to the default network

2)
尝试使用--net选项
运行:docker network ls

NETWORK ID          NAME                DRIVER              SCOPE
c..........e        bridge              bridge              local
4..........c        host                host                local
c..........a        none                null                local
  • 使用--net none成功了,但实际上它并没有工作。应用程序无法连接到数据库。
  • 使用--net host会出现错误消息conflicting options: host type networking can't be used with links. This would result in undefined behavior
  • 使用--net bridge会出现错误消息:Cannot link to /r-mysql-e4e8df05, as it does not belong to the default network

我还在运行mysql的rancher GUI上进行了检查: enter image description here

它获得一个以10.X.X.X开头的容器IP

我也尝试了添加--net managed,但是出现错误:network managed not found

我相信我在docker链接过程中有些误解。请给我一些想法,如何使这些工作。
(以前当我创建相同的容器并链接GUI中的mysql时,它是有效的)


你能分享一下你用来运行MySQL的代码吗? - Rafaf Tahsin
你可以轻松地做到这一点,无需牧场主的帮助。 - Adiii
https://stackoverflow.com/questions/49376961/django-app-in-docker-container-unable-to-find-postgres/49377447#49377447 - Adiii
https://stackoverflow.com/questions/49335815/php-can-not-resolve-mysql-container-name-under-certain-circumstances/49342027#49342027 - Adiii
请检查我的这些答案,如果有帮助的话,请让我知道。 - Adiii
这些可能是不错的解决方案,如果我放弃一些需求,但下面的答案更适合我的需求。 - Tomi
2个回答

2

嘿 @Tomi,你可以在rancher中将mysql容器公开到任何端口。这样,你就不必链接容器,而是你的jenkins生成的容器连接到主机上公开端口的mysql。你也可以使用jenkins在rancher中启动容器,使用rancher cli。这样,你就不必将mysql显示在主机网络上... 有几种方法在rancher中实现。


嘿@aemneina,我回来处理这个问题了。通过暴露MySQL可以解决问题(但感觉这是一种硬编码的版本,我不太喜欢)。当你说使用Rancher CLI在Rancher中启动容器时,你是什么意思? - Tomi

1
乍一看,Rancher 使用了托管网络,但 docker network ls 命令并未显示。

问题复现

我使用虚拟的 Alpine 容器来复现此问题:
# create some network
docker network create your_invisible_network

# run a container belonging to this network
docker container run \
  --detach \
  --name r-mysql-e4e8df05 \
  --net your_invisible_network \
  alpine tail -f /dev/null

# trying to link this container
docker container run \
  --link r-mysql-e4e8df05:mysql \
  alpine ping mysql

我遇到了问题:docker: Error response from daemon: Cannot link to /r-mysql-e4e8df05, as it does not belong to the default network.

可能的解决方案

一个解决方法是创建一个user-defined bridge network,然后将你的mysql容器添加到其中:

# create a network
docker network create \
  --driver bridge \
  a_workaround_network

# connect the mysql to this network (and alias it)
docker network connect \
  --alias mysql \
  a_workaround_network r-mysql-e4e8df05

# try to ping it using its alias
docker container run \
  --net a_workaround_network \
  alpine \
  ping mysql

# yay!
PING mysql (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: seq=0 ttl=64 time=0.135 ms
64 bytes from 127.0.0.1: seq=1 ttl=64 time=0.084 ms

如您所见,在通过其DNS名称对mysql容器进行ping操作的输出中是可行的。

值得知道:

  • 使用用户创建的桥接网络时,DNS解析可以直接工作,无需显式地--link容器 :)
  • 容器可以属于多个网络,这就是为什么这样可以工作。 在本例中,mysql容器属于your_invisible_networka_workaround_network

希望这可以帮助您!


亲爱的@stepf,非常好的回答,我还有一个问题: Error response from daemon: container cannot be connected to multiple networks with one of the networks in private (none) mode 我必须将mysql保留在原始网络上,因为其他容器会在那里连接它。 - Tomi
1
@Tomi 这只是一个猜测,但在将 mysql 加入到 a_workaround_network 之前,您可以尝试执行 docker network disconnect none mysql - stepf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接