3
  • 我运行 RancherOS 来运行 docker 容器
  • 我在 GUI 上创建了一个容器来运行我的数据库(图像:mysql,名称:r-mysql-e4e8df05)。不同的容器使用它。
  • 我可以在 GUI 上将其他容器链接到它 在此处输入图像描述
  • 这次我想在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

我尝试了不同的东西:
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: 在此处输入图像描述

它开始continer IP于:10.XXX

我也尝试过,add --net managed但错误:network managed not found

我相信我错过了在这个 docker 链接过程中理解的一些东西。请给我一些想法,我怎样才能使这些工作。
(以前当我创建相同的容器并链接到 GUI 中的 mysql 时它正在工作)

4

2 回答 2

2

嘿@Tomi,您可以从牧场主那里在您喜欢的任何端口上公开mysql容器。这样您就不必链接容器,然后您的詹金斯派生容器连接到主机上暴露端口上的容器。您还可以使用 jenkins 在 Rancher 中启动容器,使用 rancher cli。以这种方式,您不必在主机网络上显示 mysql ......用牧场主给猫剥皮的几种方法。

于 2018-04-14T04:49:28.190 回答
0

乍一看,Rancher 似乎使用托管网络,docker network ls但并未显示。

重现问题

我使用虚拟高山容器来重现这个:

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

可能的解决方案

一种解决方法是创建一个用户定义的桥接网络,然后简单地将您的 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 名称 ping mysql 容器。

很高兴知道:

  • 使用用户创建的桥接网络,DNS 解析开箱即用,无需显式--link容器 :)
  • 容器可以属于多个网络,这就是它起作用的原因。在这种情况下,mysql 容器同时属于your_invisible_networka_workaround_network

我希望这有帮助!

于 2018-03-20T11:13:39.620 回答