Unable to access runner machine from within a service container

Hello.

I’m working on a Ruby on Rails project where we use GitHub actions as a CI. While adding browser tests, I face issue accessing our app from within a service container.

Here is the part of workflow file that performs browser tests:

system:
  runs-on: ubuntu-latest
  services:
    firefox:
      image: selenium/standalone-firefox:4.0
      ports: ["4444:4444"]

  env:
    HUB_URL: "http://localhost:4444/wd/hub"
    APP_HOST: "http://localhost:4000"

  steps:
    - name: Checkout code
      uses: actions/checkout@v2.3.1

    - name: Install Ruby
      uses: ruby/setup-ruby@v1

    - name: Install gems
      run: "bundle install"

    - name: RUN TESTS
      run: "bundle exec rails spec:system"

Notice the APP_HOST variable. It’s used to point where the browser should connect to. My guess (I couldn’t find any information about this though) the hostname should be localhost.

These steps launch a container with Firefox, which should open our app accessible on the runner machine port 4000. But it didn’t happen: Firefox throws an error saying “http://localhost:4000 is not reachable”.

Could you please tell me are the runner machine ports blocked, or I should use some different hostname.

And here is the RSpec output:

Randomized with seed 14076

Landing page
  example at ./spec/system/landing_spec.rb:6 (FAILED - 1)

Failures:

  1) Landing page 

     Failure/Error: before { visit(root_path) }

     Selenium::WebDriver::Error::UnknownError:

       Reached error page: about:neterror?e=connectionFailure&u=http%3A//localhost%3A4000/&c=UTF-8&d=Firefox%20can%E2%80%99t%20establish%20a%20connection%20to%20the%20server%20at%20localhost%3A4000.

Hi @arg,

Glad to see you in Github Community Forum!

The services are running backend and your workflow steps are executed on hosted runner directly.

Your env is a job level setting, is it correct? If you’d like to access runner within service container, the env should have same level with services: image, ports. And you only opened 4444 port from on the host and service container, not port 4000.

Regarding access the host ip from docker container, please check the link here if it helps.

Typically the usage is access services from host, please check the sample code here for your reference.

Thanks.

1 Like

I managed to fix the issue by using a proper runner machine IP instead of localhost.

Here is the step I added into workflow:

- name: Set environment variables
  run: |
    echo "::set-env name=APP_IP::$(docker network inspect bridge --format='{{( index .IPAM.Config 0).Gateway}}')"

Then I can point browser to http://${{APP_IP}}:4000.

Thanks @weide-zhou for the hint.