允许Docker容器访问localhost

3
我正在尝试运行 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 访问我的 localhost 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

希望能得到任何有关使这两种方式之一正常工作的帮助。


嘿@boban,看看我针对Mac的特定解决方案的答案。 - user3106759
4个回答

2
如果您使用--net=host标志运行容器,则可以访问该容器,就好像其中的服务正在主机上运行一样。来自文档

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

在您的示例中,您应该能够在不暴露任何端口的情况下访问localhost:4444上的selenium,并且selenium应该可以从容器中正常访问主机上的服务。
我们发现使docker化测试应用程序并将selenium容器放入与docker化应用程序相同的网络中是使selenium从docker中正确运行的最简单方法。这也在您想要在一个主机上测试多个实例时非常有帮助。

1
如果我不想将我的应用程序 docker 化,有什么提示可以给我吗?我只是想在 docker 中运行 Selenium。如果我运行 docker run --net=host selenium/standalone-chrome,那么我是否会有一些专用的 URL 来访问 Selenium? - Bob
@kolaente,它不行,我也有同样的问题。我也在MacOS上。这将在localhost上运行Selenium,但Chrome无法访问localhost网站。 docker run selenium/standalone-chrome docker run --net=host selenium/standalone-chrome - user3106759
@user3106759请查看下面的答案,以获取针对macOS的特定解决方案。 - kolaente

1

docker run -d -p 4444:4444 selenium/standalone-chrome -p 标志将允许从本地主机访问,在Windows上进行了测试


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 for macOS 17.03以后版本中提供的特殊的仅适用于macOS的DNS名称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服务器,例如docker run -d --dns 54.252.183.4 --link selenium-hub:hub selenium/node-chrome:3.4.0。

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”上运行其他任何内容。


嗨!问题是我想在运行docker run --net=host selenium/standalone-chrome时访问http://localhost:4444上的Selenium,但我的页面显示“This site can’t be reached”。命令日志看起来像这样:11:29:14.463 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 4444。我不确定为什么当我导航到http://localhost:4444时它不显示。 - Bob

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