允许 docker 容器访问本地主机

Allowing docker container to access localhost

我正在尝试 运行 selenium docker image

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 (https://codeception.com/docs/modules/WebDriver#headless-selenium-in-docker) 的 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] - 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

任何有助于使这两种方式中的任何一种工作的帮助。

如果您 运行 带有 --net=host 标志的容器,它将可以访问,就好像其中的服务在主机上 运行ning 一样。来自 the docs:

If you use the host network mode for a container, that container’s network stack is not isolated from the Docker host (the container shares the host’s networking namespace), and the container does not get its own IP-address allocated.

在您的示例中,您应该能够在 localhost:4444 上访问 selenium 而无需暴露任何端口,并且 selenium 应该能够很好地从容器访问本地主机上的服务。

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

使用以下命令:

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

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

因此,要在“本地主机”上访问 selenium,您有两个选择:

  1. 配置selenium在80端口启动,我不推荐。

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

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

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

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

您好,请尝试此处提到的选项,这是一个 mac 特定问题。

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

Accessing Local Development Sites If you do plan on using the Docker containers to access local development sites, there are some additional considerations.

If you are using Windows or Linux, you can simply use the --net=host option when running the containers. For example:

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

On macOS, however, it’s a little trickier due to the way the Docker app itself works (the above option just doesn’t work the same or as expected).

The easiest workarounds I’ve found are:

  1. Use your local host local network IP address for the URL i.e. http://192.168.1,168. This often isn’t ideal especially if you have a few sites.
  2. Use the special macOS-only DNS name available in Docker for macOS from 17.03 onward docker.for.mac.localhost, which will resolve to the internal IP address used by the host. Again, this likely won’t be ideal if you have a few sites or just don’t like the address.
  3. Use the --add-host option to add your local test site to the Docker hosts/containers e.g. --add-host store.localhost:192.168.1.191, which you would use when running the standalone or node containers like so: docker run -d --link selenium-hub:hub --add-host store.localhost:192.168.1.191 selenium/node-firefox:3.4.0.
  4. Configure and use a DNS server. You can use the --dns option to update the Docker containers to use a specific DNS server e.g. docker run -d --dns 54.252.183.4 --link selenium-hub:hub selenium/node-chrome:3.4.0.