连接到Docker-container中的Selenium

时间:2018-05-10 07:58:50

标签: selenium docker networking

我正在用Selenium开发测试。目前我正在使用官方selenium/standalone-chrome:3.11.0图片。我在Docker-container中只运行Selenium。项目本身是在主机上编译的(测试连接到容器的公开端口):

$ docker run -p 4444:4444 selenium/standalone-chrome:3.11.0

$ curl -v localhost:4444
* Rebuilt URL to: localhost:4444/
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 4444 (#0)
> GET / HTTP/1.1
> Host: localhost:4444
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
...

但我想在Docker-container中完全编译和测试项目。所以我在selenium/standalone-chrome:3.11.0上创建了自己的图像。我的(简化)Dockerfile看起来像这样:

FROM selenium/standalone-chrome:3.11.0

RUN sudo apt-get --assume-yes --quiet update
RUN sudo apt-get --assume-yes --quiet install curl

CMD ["curl", "-v", "localhost:4444"]

从文件中可以看出,我正在尝试连接容器内的端口 4444 。当我运行图像时,例如:

docker build -t test . && docker run test

我明白了:

* Rebuilt URL to: localhost:4444/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 127.0.0.1...
* connect to 127.0.0.1 port 4444 failed: Connection refused
*   Trying ::1...
* Immediate connect fail for ::1: Cannot assign requested address
*   Trying ::1...
* Immediate connect fail for ::1: Cannot assign requested address
* Failed to connect to localhost port 4444: Connection refused
* Closing connection 0
curl: (7) Failed to connect to localhost port 4444: Connection refused

为什么我无法连接到从同一容器内部运行的Selenium?

0 个答案:

没有答案