Node.js 如何将docker容器端口公开给另一个容器
我有一个依赖selenium作为链接服务的docker容器(也是一个docker容器)。这个docker容器只是使用selenium服务作为主机来无头运行nightwatchJS测试。selenium服务启动正常,但当nightwatch测试尝试在docker容器中运行时,我在selenium服务端口(4444)上收到拒绝连接错误。我可以使用curl访问这个端口上的selenium主机,如果我没有通过docker运行测试,也可以在上面运行nightwatch测试。这是我的docker compose yaml文件:Node.js 如何将docker容器端口公开给另一个容器,node.js,selenium,docker,docker-compose,nightwatch.js,Node.js,Selenium,Docker,Docker Compose,Nightwatch.js,我有一个依赖selenium作为链接服务的docker容器(也是一个docker容器)。这个docker容器只是使用selenium服务作为主机来无头运行nightwatchJS测试。selenium服务启动正常,但当nightwatch测试尝试在docker容器中运行时,我在selenium服务端口(4444)上收到拒绝连接错误。我可以使用curl访问这个端口上的selenium主机,如果我没有通过docker运行测试,也可以在上面运行nightwatch测试。这是我的docker compo
services:
selenium:
image: selenium/standalone-chrome
ports:
- "4444:4444"
nightwatch:
build: .
volumes:
- .:/home/client_test
links:
- selenium
environment:
selenium: 4444
expose:
- "4444"
和我的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
如何允许在一个docker容器中运行的夜间监视测试利用在另一个端口4444上运行的selenium主机 每个容器都有自己的网络。每个都有
eth0
和lo
(本地主机)接口,它们位于docker的网络中
因此,您的selenium不在Nighwatch应用程序的本地主机中。可通过以下方式获得:
selenium:4444
该主机名被转换为对应于selenium
容器的正确IP。因此,根据这些信息调整脚本和配置
您可以使用以下方法查找和检查docker网络:
docker network ls
docker network inspect <network-id>
docker网络
docker网络检查
谢谢!只有一个问题,如果我将“selenium:4444”放入我的nightwatch.json文件中,那么它将转换为selenium容器的IP?请参阅,nightwatch.json文件config for chrome,其中我直接将ip设置为selenium容器的ip。不确定我是否正确。硒和夜表是两种不同的东西。为什么我们需要将两者联系起来?
docker network ls
docker network inspect <network-id>