Wednesday 7 June 2017

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). This docker container just uses selenium service as host to run nightwatchJS tests headlessly. 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). 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. Here is my docker compose yaml file:

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:

#
#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)

{
  "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?



via TDmoneybanks

No comments:

Post a Comment