gpt4 book ai didi

selenium - 在 docker 容器之间进行通信

转载 作者:行者123 更新时间:2023-12-02 18:15:37 25 4
gpt4 key购买 nike

我有一台运行两个 docker 容器的服务器。

一个 docker 容器是 Web 服务器,另一个是 selenium-chromedriver。

从带有 Web 服务器的容器中,我希望能够连接到 chrome 驱动程序。

Web 服务器是这样启动的:

docker run -i -p 80:80 -d '<name>:<version>' /sbin/my_init

selenium 驱动程序是这样启动的:
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:3.4.0

从主机上,我可以使用 curl 从 selenium 容器中获取信息:
curl http://localhost:4444/wd/hub/status
# => {"state":"success","sessionId":..........

但是,从 Web 服务器容器中,我只得到:
curl: (7) Failed to connect to localhost port 4444: Connection refused

我该怎么做才能让包含 Web 服务器的容器能够连接到另一个容器?

最佳答案

我认为您必须创建网络:docker.com work with networks help

基本容器网络示例:

首先,创建并运行两个容器,container1 和 container2:

$ docker run -itd --name=container1 busybox

18c062ef45ac0c026ee48a83afa39d25635ee5f02b58de4abc8f467bcaa28731

$ docker run -itd --name=container2 busybox

498eaaaf328e1018042c04b2de04036fc04719a6e39a097a4f4866043a2c2152

创建一个隔离的桥接网络进行测试。
$ docker network create -d bridge --subnet 172.25.0.0/16 isolated_nw

06a62f1c73c4e3107c0f555b7a5f163309827bfbbf999840166065a8f35455a8

将 container2 连接到网络,然后检查网络以验证连接:
$ docker network connect isolated_nw container2

$ docker network inspect isolated_nw

[
{
"Name": "isolated_nw",
"Id": "06a62f1c73c4e3107c0f555b7a5f163309827bfbbf999840166065a8f35455a8",
"Scope": "local",
"Driver": "bridge",
"IPAM": {
"Driver": "default",
"Config": [
{
"Subnet": "172.25.0.0/16",
"Gateway": "172.25.0.1/16"
}
]
},
"Containers": {
"90e1f3ec71caf82ae776a827e0712a68a110a3f175954e5bd4222fd142ac9428": {
"Name": "container2",
"EndpointID": "11cedac1810e864d6b1589d92da12af66203879ab89f4ccd8c8fdaa9b1c48b1d",
"MacAddress": "02:42:ac:19:00:02",
"IPv4Address": "172.25.0.2/16",
"IPv6Address": ""
}
},
"Options": {}
}
]

请注意,容器 2 会自动分配一个 IP 地址。因为您在创建网络时指定了 --subnet,所以 IP 地址是从该子网中选择的。

提醒一下,container1 只连接到默认的桥接网络。

关于selenium - 在 docker 容器之间进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45521591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com