Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
允许docker容器访问本地主机_Docker_Selenium_Selenium Webdriver_Codeception - Fatal编程技术网

允许docker容器访问本地主机

允许docker容器访问本地主机,docker,selenium,selenium-webdriver,codeception,Docker,Selenium,Selenium Webdriver,Codeception,我正在尝试运行selenium docker映像 docker run-d-p 4444:4444——shm尺寸2g硒/独立铬:4.0.0-beta-3-prerelease-20210329 从该图像:https://hub.docker.com/r/selenium/standalone-chrome 2021-03-30 10:33:15,835 INFO Included extra file "/etc/supervisor/conf.d/selenium.conf"

我正在尝试运行selenium docker映像

docker run-d-p 4444:4444——shm尺寸2g硒/独立铬:4.0.0-beta-3-prerelease-20210329

从该图像:
https://hub.docker.com/r/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
当我运行这个命令时,我可以看到Selenium在运行http://localhost:4444/

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

我使用Selenium触发Codeception测试,我在这里看到提到了Selenium()的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
2021-03-30 10:33:15835解析期间包含额外文件“/etc/supervisor/conf.d/selenium.conf”的信息
2021-03-30 10:33:15839信息主管从pid 9开始
2021-03-30 10:33:16848信息衍生:pid为11的“xvfb”
2021-03-30 10:33:16854信息衍生:“selenium standalone”和pid 12
10:33:17.160信息[GridLauncherV3.parse]-Selenium服务器版本:3.141.59,修订版:e82be7d358
2021-03-30 10:33:17161信息成功:xvfb进入运行状态,进程已保持超过0秒(startsecs)
2021-03-30 10:33:17162信息成功:selenium standalone进入运行状态,进程已停止运行超过0秒(startsecs)
10:33:17.261信息[GridLauncherV3.lambda$buildLaunchers$3]-在端口4444上启动独立的Selenium服务器
2021-03-30 10:33:17.318:INFO::main:Logging在435ms时初始化为org.seleniumhq.jetty9.util.log.StdErrLog
10:33:17.577信息[WebDriverServlet.]-初始化WebDriverServlet
10:33:17.680信息[SeleniumServer.boot]-Selenium服务器已启动并在端口4444上运行

使这两种方法中的任何一种工作的任何帮助。

如果使用
--net=host
标志运行容器,它将可以访问,就像容器中的服务在主机上运行一样。发件人:

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

在您的示例中,您应该能够在
localhost:4444
上访问selenium,而无需公开任何端口,并且selenium应该能够从容器访问您在localhost上的服务


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

使用以下命令:

docker run --net=host selenium/standalone-chrome
容器和主机共享相同的网络名称空间,这意味着无论应用程序(selenium)使用什么端口,主机系统上都使用相同的端口。因此,你也应该能够访问“localhost:4444”上的应用程序,这是你不想要的

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

  • 将selenium配置为在端口80上启动,我不建议这样做

  • 使用以下命令启动selenium:

    docker run --net=host selenium/standalone-chrome
    
    docker run-d-p 80:4444——shm尺寸2g硒/独立铬:4.0.0-beta-3-prerelease-20210329

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


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

    如果我不想将我的应用程序dockerize,该怎么办?你能给我一些建议吗?我只想在docker中运行Selenium,如果我运行
    docker run--net=host Selenium/standalone chrome
    我会有一些专用的URL来访问Selenium吗?
    localhost:4444
    应该可以。嘿!问题是,当我运行
    docker run--net=host Selenium/standalone chrome
    时,我想访问Selenium,但我的页面显示
    无法访问此站点。命令的日志看起来像
    11:29:14.463 INFO[SeleniumServer.boot]-Selenium服务器已启动并在端口4444上运行
    我不确定为什么在导航到