[英]How do I expose a docker container port to another container
I have a docker container that relies on selenium as a linked service (also a docker container).我有一个依赖 selenium 作为链接服务的 docker 容器(也是一个 docker 容器)。 This docker container just uses selenium service as host to run nightwatchJS tests headlessly.
这个 docker 容器只是使用 selenium 服务作为主机来无头地运行 nightwatchJS 测试。 The selenium service starts up fine but when the nightwatch tests attempt to run within the docker container, i receive a refused connection error on the selenium service port (4444).
selenium 服务启动正常,但是当 nightwatch 测试尝试在 docker 容器内运行时,我在 selenium 服务端口 (4444) 上收到拒绝连接错误。 I can access the selenium host at this port using curl and can also run the nightwatch tests on it if i do not run the tests through docker.
我可以使用 curl 在这个端口访问 selenium 主机,如果我不通过 docker 运行测试,也可以在它上面运行 nightwatch 测试。 Here is my docker compose yaml file:
这是我的 docker compose yaml 文件:
services:
selenium:
image: selenium/standalone-chrome
ports:
- "4444:4444"
nightwatch:
build: .
volumes:
- .:/home/client_test
links:
- selenium
environment:
selenium: 4444
expose:
- "4444"
and my docker file:和我的 docker 文件:
#
#Nightwatch.js Dockerfile
#
FROM alpine:3.4
WORKDIR /home/client-tests
ADD . /home/client-tests/
ENV NODE_PATH=/home/client-app/node_modules
#RUN apk add 'firefox-esr<45.6.0-r0'
#install firefox
#RUN wget -c http://ftp.mozilla.org/pub/firefox/releases/45.8.0esr/firefox-45.8.0esr.linux-x86_64.sdk.tar.bz2 -P /usr/local
#RUN tar xvjf /usr/local/firefox-45.8.0esr.linux-x86_64.sdk.tar.bz2
#RUN ln -s /usr/local/firefox/firefox /usr/local/bin/
RUN apk --no-cache --update add bash \
# Install tini, a tiny but valid init for containers
tini \
# Install nodejs
nodejs-lts \
#Install java
openjdk8-jre-base \
#Install chrome
chromium \
chromium-chromedriver \
#Install firefox
#firefox \
unzip \
wget \
xvfb \
dbus \
tar \
# Install other dependencies
&& npm install -g nightwatch \
# -g request \
# chai \
# superagent \
# Clean up obsolete files:
&& rm -rf \
/tmp/* \
/root/.npm
#RUN apk update -q && apk add --no-cache xvfb firefox-esr dbus
#ADD https://gist.github.com/elbaschid/5a91271c07bb0de7bfa6/raw/d07b4eecf02fd3341ef72753e4c5875cf137bda8/xvfb-run.sh /usr/bin/xvfb-run
#RUN chmod u+x /usr/bin/xvfb-run
#
RUN npm install request
RUN npm install chai
RUN npm install superagent
EXPOSE 4444
#
##Fetch the chrome drive for linux
#RUN rm -rf /home/client-app/bin/chromedriver
#RUN rm -rf /home/client-app/bin/geckodriver
#
#RUN wget -c http://chromedriver.storage.googleapis.com/2.28/chromedriver_linux64.zip -P /tmp
#RUN unzip /tmp/chromedriver_linux64.zip -d /tmp
#RUN chmod +x /tmp/chromedriver
#RUN mv -f /tmp/chromedriver /home/client-app/bin/chromedriver
#
##get the gecko driver for linux
#RUN wget https://github.com/mozilla/geckodriver/releases/download/v0.16.1/geckodriver-v0.16.1-linux64.tar.gz -P /tmp
#RUN tar xvzf /tmp/geckodriver-v0.16.1-linux64.tar.gz -C /tmp
#RUN chmod 755 /tmp/geckodriver
#RUN mv -f /tmp/geckodriver /home/client-app/bin/geckodriver
#CMD xvfb-run -e java -Dwebdriver.chrome.driver=./bin/chromedriver -jar ./bin/selenium-server-standalone-3.4.0.jar &; nightwatch -e chrome -t tests/*'
CMD nightwatch -e chrome -t tests/loginTest.js
and this is my nightwatchJS json file that specifies the host (url is changed for privacy)这是我指定主机的 nightwatchJS json 文件(出于隐私原因更改了 url)
{
"src_folders" : ["./tests"],
"output_folder" : "./reports",
"custom_commands_path" : "./custom-commands",
"page_objects_path" : "",
"custom_assertions_path" : "",
"globals_path" : "./globals.js",
"live_output" : false,
"parallel_process_delay" : 10,
"disable_colors": false,
"test_workers" : true,
"selenium" : {
"start_process" : false,
"server_path" : "./bin/selenium-server-standalone-3.4.0.jar",
"log_path" : "",
"host" : "127.0.0.1",
"port" : 4444,
"cli_args" : {
"webdriver.gecko.driver" : "./bin/geckodriver",
"webdriver.chrome.driver" : "./bin/chromedriver",
"webdriver.ie.driver" : "./bin/IEDriverServer.exe"
}
},
"test_settings" : {
"default" : {
"launch_url" : "https://google.com",
"selenium_host" : "hub",
"selenium_port" : 4444,
"silent" : true,
"disable_colors": false,
"screenshots" : {
"enabled" : true,
"path" : "./screenshots"
},
"desiredCapabilities" : {
"browserName" : "firefox",
"javascriptEnabled" : true,
"acceptSslCerts" : true,
"acceptInsecureCerts" : true,
"applicationCacheEnabled" : true
}
},
"chrome" : {
"launch_url" : "https://google.com/",
"selenium_host" : "0.0.0.0",
"selenium_post" : 4444,
"disable_colors" : false,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities" : {
"browserName" : "chrome",
"javascriptEnabled" : true,
"acceptSslCerts" : true,
"acceptInsecureCerts" : true,
"chromeOptions": {
"args": [ "--no-sandbox"]
}
}
},
"IE" : {
"launch_url" : "https://google.com/",
"selenium_host" : "fakeURLFORSTACK.com",
"selenium_post" : 4444,
"disable_colors" : false,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities" : {
"browserName" : "internet explorer",
"javascriptEnabled" : true,
"acceptSslCerts" : true,
"acceptInsecureCerts" : true
}
},
"saucelabs" : {
"selenium_host" : "ondemand.saucelabs.com",
"selenium_port" : 80,
"username" : "${SAUCE_USERNAME}",
"access_key" : "${SAUCE_ACCESS_KEY}",
"use_ssl" : false,
"silent" : true,
"output" : true,
"screenshots" : {
"enabled" : false,
"on_failure" : true,
"path" : ""
},
"desiredCapabilities": {
"name" : "test-example",
"browserName": "firefox"
},
"globals" : {
"myGlobal" : "some_sauce_global"
},
"selenium" : {
"start_process" : false
}
},
"phantomjs" : {
"desiredCapabilities" : {
"browserName" : "phantomjs",
"javascriptEnabled" : true,
"acceptSslCerts" : true,
"acceptInsecureCerts" : true,
"phantomjs.binary.path" : "phantomjs"
}
},
"browserstack" : {
"selenium" : {
"start_process" : false
},
"selenium_host" : "hub.browserstack.com",
"selenium_port" : 80,
"silent" : true,
"desiredCapabilities": {
"name" : "test-example",
"browserName": "firefox",
"browserstack.user" : "...",
"browserstack.key" : "..."
}
},
"testingbot" : {
"selenium_host" : "hub.testingbot.com",
"selenium_port" : 80,
"apiKey" : "${TB_KEY}",
"apiSecret" : "${TB_SECRET}",
"silent" : true,
"output" : true,
"screenshots" : {
"enabled" : false,
"on_failure" : true,
"path" : ""
},
"desiredCapabilities": {
"name" : "test-example",
"browserName": "firefox"
},
"selenium" : {
"start_process" : false
}
}
}
}
How can i allow my nightwatch tests running in one docker container to utilize the selenium host running in another on port 4444?我怎样才能让我在一个 docker 容器中运行的 nightwatch 测试利用在另一个 4444 端口上运行的 selenium 主机?
Each container has its own networking.每个容器都有自己的网络。 Each one has
eth0
and lo
(localhost) interfaces, that live in the docker's network.每个都有
eth0
和lo
(localhost) 接口,它们位于 docker 的网络中。
So, your selenium is not in the localhost of the Nighwatch app.因此,您的 selenium 不在 Nightwatch 应用程序的本地主机中。 It is available as this:
它是这样可用的:
selenium:4444
That hostname is translated to the correct IP that correspond to the selenium
container.该主机名被转换为与
selenium
容器对应的正确 IP。 So adapt your scripts and config to that information.因此,请根据该信息调整您的脚本和配置。
You can find and inspect the docker network using this:您可以使用以下命令查找和检查 docker 网络:
docker network ls
docker network inspect <network-id>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.