flaz14
flaz14

Reputation: 856

Connect to Selenium within Docker-container

I'm developing tests with Selenium. Currently I'm using official selenium/standalone-chrome:3.11.0 image. I'm running only Selenium inside Docker-container. The project itself is compiled on the host machine (tests connect to the container's exposed port):

$ docker run -p 4444:4444 selenium/standalone-chrome:3.11.0

$ curl -v localhost:4444
* Rebuilt URL to: localhost:4444/
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 4444 (#0)
> GET / HTTP/1.1
> Host: localhost:4444
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
...

But I would like to compile and test the project entirely inside Docker-container. So I created my own image upon selenium/standalone-chrome:3.11.0. My (simplified) Dockerfile looks like this:

FROM selenium/standalone-chrome:3.11.0

RUN sudo apt-get --assume-yes --quiet update
RUN sudo apt-get --assume-yes --quiet install curl

CMD ["curl", "-v", "localhost:4444"]

As can be seen from the file, I'm trying to connect to port 4444 within container. When I run the image, e.g.:

docker build -t test . && docker run test

I get:

* Rebuilt URL to: localhost:4444/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 127.0.0.1...
* connect to 127.0.0.1 port 4444 failed: Connection refused
*   Trying ::1...
* Immediate connect fail for ::1: Cannot assign requested address
*   Trying ::1...
* Immediate connect fail for ::1: Cannot assign requested address
* Failed to connect to localhost port 4444: Connection refused
* Closing connection 0
curl: (7) Failed to connect to localhost port 4444: Connection refused

Why I'm not able to connect to Selenium which is ran inside container from the same container?

Upvotes: 0

Views: 1712

Answers (1)

flaz14
flaz14

Reputation: 856

I've found the solution at last (sorry for my stupidity).

Building an image upon selenium/standalone-chrome:3.11.0 is not sufficient. You need to start Selenium explicitly.

The Dockerfile:

FROM selenium/standalone-chrome:3.11.0

WORKDIR /app

COPY . /app

RUN sudo apt-get --assume-yes --quiet update
RUN sudo apt-get --assume-yes --quiet install curl

CMD ["./acceptance.sh"] 

The acceptance.sh wrapper script:

#!/bin/bash

set -x
set -e

/opt/bin/entry_point.sh &
# It will be better to look into log and wait for 
# record 'Selenium Server is up and running on port 4444'.
# But in this script simplified approach is used, just for
# the sake of brevity.
sleep 30 
curl -v localhost:4444

The result:

...
+ set -e
+ sleep 30
+ /opt/bin/entry_point.sh
07:51:35.092 INFO [GridLauncherV3.launch] - Selenium build info: version: '3.11.0', revision: 'e59cfb3'
07:51:35.095 INFO [GridLauncherV3$1.launch] - Launching a standalone Selenium Server on port 4444
2018-05-15 07:51:35.661:INFO::main: Logging initialized @2436ms to org.seleniumhq.jetty9.util.log.StdErrLog
07:51:36.448 INFO [SeleniumServer.boot] - Welcome to Selenium for Workgroups....
07:51:36.450 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 4444
^[[23;5~^[[23;5~+ curl -v localhost:4444
* Rebuilt URL to: localhost:4444/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 4444 (#0)
> GET / HTTP/1.1
> Host: localhost:4444
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
...

Cheers!

Upvotes: 1

Related Questions