允许docker容器访问本地主机

时间:2021-03-30 10:35:55

标签: docker selenium selenium-webdriver codeception

我正在尝试运行 selenium docker 映像

docker run -d -p 4444:4444 --shm-size 2g selenium/standalone-chrome:4.0.0-beta-3-prerelease-20210329

来自这张图片:https://hub.docker.com/r/selenium/standalone-chrome

当我运行这个命令时,我可以在 http://localhost:4444/ 上看到 Selenium

我需要的是允许 selenium 访问我的本地主机 URL。我正在浏览文档,但找不到任何提及它的内容。

我正在使用 Selenium 来触发 Codeception 测试,我看到这里提到了 Selenium 的 docker 容器 (https://codeception.com/docs/modules/WebDriver#headless-selenium-in-docker),但该命令实际上启动了 Selenium,但我不确定如何访问它。这是命令和日志:

docker run --net=host selenium/standalone-chrome

2021-03-30 10:33:15,835 INFO Included extra file "/etc/supervisor/conf.d/selenium.conf" during parsing
2021-03-30 10:33:15,839 INFO supervisord started with pid 9
2021-03-30 10:33:16,848 INFO spawned: 'xvfb' with pid 11
2021-03-30 10:33:16,854 INFO spawned: 'selenium-standalone' with pid 12
10:33:17.160 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.59, revision: e82be7d358
2021-03-30 10:33:17,161 INFO success: xvfb entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
2021-03-30 10:33:17,162 INFO success: selenium-standalone entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
10:33:17.261 INFO [GridLauncherV3.lambda$buildLaunchers$3] - Launching a standalone Selenium Server on port 4444
2021-03-30 10:33:17.318:INFO::main: Logging initialized @435ms to org.seleniumhq.jetty9.util.log.StdErrLog
10:33:17.577 INFO [WebDriverServlet.<init>] - Initialising WebDriverServlet
10:33:17.680 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 4444

任何有助于使这两种方式发挥作用的方法。

3 个答案:

答案 0 :(得分:1)

如果您使用 --net=host 标志运行容器,则可以访问它,就像它内部的服务在主机上运行一样。来自the docs

<块引用>

如果您对容器使用主机网络模式,则该容器的网络堆栈不会与 Docker 主机隔离(容器共享主机的网络命名空间),并且容器不会获得自己的 IP 地址分配。

在您的示例中,您应该能够在不暴露任何端口的情况下访问 localhost:4444 上的 selenium,并且 selenium 应该能够从容器访问您在 localhost 上的服务就好了。

我们发现从 docker 正常运行获取 selenium 的最简单方法是将应用程序 dockerize 以测试并将 selenium 容器与 dockerized 应用程序放在一个网络中。当您想在一台主机上测试多个实例时,这也很有帮助。

答案 1 :(得分:1)

您好,请尝试这里提到的选项,这是 mac 特定的问题。

https://robotninja.com/blog/introduction-using-selenium-docker-containers-end-end-testing/

<块引用>

访问本地开发站点 如果您计划使用 Docker 容器访问本地 开发网站,还有一些额外的注意事项。

如果您使用的是 Windows 或 Linux,您可以简单地使用 --net=host 运行容器时的选项。例如:

$ docker run --net=host selenium/standalone-chrome

然而,在 macOS 上,由于 Docker 的方式,它有点棘手 应用程序本身有效(上述选项与 预期)。

我发现的最简单的解决方法是:

  1. 使用您的本地主机本地网络 IP 地址作为 URL,即 http://192.168.1,168。这通常并不理想,特别是如果您有 几个网站。
  2. 使用 Docker 中提供的特殊 macOS 专用 DNS 名称 macOS 从 17.03 开始​​ docker.for.mac.localhost,它将解析 到主机使用的内部 IP 地址。同样,这可能不会 如果您有几个网站或只是不喜欢该地址,则这是理想的选择。
  3. 使用 --add-host 选项将您的本地测试站点添加到 Docker 主机/容器,例如--add-host store.localhost:192.168.1.191,其中 您将在运行独立或节点容器时使用,如下所示: docker run -d --link selenium-hub:hub --add-host store.localhost:192.168.1.191 selenium/node-firefox:3.4.0.
  4. 配置和使用 DNS 服务器。您可以使用 --dns 选项来更新 Docker 容器使用特定的 DNS 服务器,例如码头工人运行 -d --dns 54.252.183.4 --link selenium-hub:hub selenium/node-chrome:3.4.0.

答案 2 :(得分:0)

使用以下命令:

docker run --net=host selenium/standalone-chrome

容器和主机共享相同的网络命名空间,这意味着无论您的应用程序 (selenium) 使用什么端口,主机系统上都使用相同的端口。因此,您应该能够再次访问您不想要的“localhost:4444”上的应用程序。

因此要在“localhost”上访问 selenium,您有两个选择:

  1. 将 selenium 配置为在端口 80 上启动,我不推荐这样做。

  2. 使用以下命令启动 selenium:

    docker run -d -p 80:4444 --shm-size 2g selenium/standalone-chrome:4.0.0-beta-3-prerelease-20210329

使用哪个将主机系统的端口“80”映射到容器的端口“4444”。

您现在应该能够在“http://localhost”上访问 selenium,但请记住,您将无法在主机的“80”端口上运行任何其他内容。