【问题标题】:How do I expose a docker container port to another container如何将 docker 容器端口暴露给另一个容器
【发布时间】:2021-01-22 11:50:18
【问题描述】:

我有一个依赖 selenium 作为链接服务的 docker 容器(也是一个 docker 容器)。这个 docker 容器只是使用 selenium 服务作为主机来无头运行 nightwatchJS 测试。 selenium 服务启动正常,但是当 nightwatch 测试尝试在 docker 容器中运行时,我在 selenium 服务端口 (4444) 上收到拒绝连接错误。我可以使用 curl 在此端口访问 selenium 主机,如果我不通过 docker 运行测试,我还可以在其上运行 nightwatch 测试。这是我的 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"

还有我的 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

这是我的 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
      }
    }
  }
}

我怎样才能让我在一个 docker 容器中运行的守夜测试能够利用在另一个端口 4444 上运行的 selenium 主机?

【问题讨论】:

  • 不确定我是否正确。 Selenium 和 nightwatch 是两个不同的东西。为什么我们需要链接两者?

标签: node.js selenium docker docker-compose nightwatch.js


【解决方案1】:

每个容器都有自己的网络。每个都有eth0lo (localhost) 接口,它们位于docker 的网络中。

因此,您的 selenium 不在 Nightwatch 应用程序的本地主机中。它可以这样使用:

selenium:4444

该主机名被转换为与selenium 容器对应的正确 IP。因此,请根据该信息调整您的脚本和配置。

您可以使用以下方法查找和检查 docker 网络:

docker network ls
docker network inspect <network-id>

【讨论】:

  • 谢谢!只有一个问题,所以如果我将“selenium:4444”放入我的 nightwatch.json 文件中,那么它将转换为 selenium 容器的 IP?请看,chrome 的 nightwatch.json 文件配置,我直接将 ip 设置为 selenium 容器的 ip。