Bob
Bob

Reputation: 8724

Allowing docker container to access localhost

I am trying to run selenium docker image

docker run -d -p 4444:4444 --shm-size 2g selenium/standalone-chrome:4.0.0-beta-3-prerelease-20210329

from this image: https://hub.docker.com/r/selenium/standalone-chrome

When I run this command, I can see Selenium on http://localhost:4444/

What I need is to allow selenium to access my localhost URL. I was looking through documentation but I was unable find anything that mentions it.

I am using Selenium to trigger Codeception tests and I saw here mentioning about docker container for Selenium (https://codeception.com/docs/modules/WebDriver#headless-selenium-in-docker), but that command actually starts Selenium, but I am not sure how to access it. This is the command and the log:

docker run --net=host selenium/standalone-chrome

2021-03-30 10:33:15,835 INFO Included extra file "/etc/supervisor/conf.d/selenium.conf" during parsing
2021-03-30 10:33:15,839 INFO supervisord started with pid 9
2021-03-30 10:33:16,848 INFO spawned: 'xvfb' with pid 11
2021-03-30 10:33:16,854 INFO spawned: 'selenium-standalone' with pid 12
10:33:17.160 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.59, revision: e82be7d358
2021-03-30 10:33:17,161 INFO success: xvfb entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
2021-03-30 10:33:17,162 INFO success: selenium-standalone entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
10:33:17.261 INFO [GridLauncherV3.lambda$buildLaunchers$3] - Launching a standalone Selenium Server on port 4444
2021-03-30 10:33:17.318:INFO::main: Logging initialized @435ms to org.seleniumhq.jetty9.util.log.StdErrLog
10:33:17.577 INFO [WebDriverServlet.<init>] - Initialising WebDriverServlet
10:33:17.680 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 4444

Any help to get either of these two ways working.

Upvotes: 3

Views: 7149

Answers (4)

Pradeep
Pradeep

Reputation: 63

docker run -d -p 4444:4444 selenium/standalone-chrome -p flag will allow access from localhost, tested on windows

Upvotes: 1

user3106759
user3106759

Reputation: 423

Hi try the options mentioned here, It's a mac specific issue.

https://robotninja.com/blog/introduction-using-selenium-docker-containers-end-end-testing/

Accessing Local Development Sites If you do plan on using the Docker containers to access local development sites, there are some additional considerations.

If you are using Windows or Linux, you can simply use the --net=host option when running the containers. For example:

$ docker run --net=host selenium/standalone-chrome

On macOS, however, it’s a little trickier due to the way the Docker app itself works (the above option just doesn’t work the same or as expected).

The easiest workarounds I’ve found are:

  1. Use your local host local network IP address for the URL i.e. http://192.168.1,168. This often isn’t ideal especially if you have a few sites.
  2. Use the special macOS-only DNS name available in Docker for macOS from 17.03 onward docker.for.mac.localhost, which will resolve to the internal IP address used by the host. Again, this likely won’t be ideal if you have a few sites or just don’t like the address.
  3. Use the --add-host option to add your local test site to the Docker hosts/containers e.g. --add-host store.localhost:192.168.1.191, which you would use when running the standalone or node containers like so: docker run -d --link selenium-hub:hub --add-host store.localhost:192.168.1.191 selenium/node-firefox:3.4.0.
  4. Configure and use a DNS server. You can use the --dns option to update the Docker containers to use a specific DNS server e.g. docker run -d --dns 54.252.183.4 --link selenium-hub:hub selenium/node-chrome:3.4.0.

Upvotes: 1

Chris Chandler
Chris Chandler

Reputation: 1

With following command:

docker run --net=host selenium/standalone-chrome

The container and host share the same network namespace, which means whatever port your app (selenium) uses, the same port is used on the host system. So again you should be able to access the app on on "localhost:4444", which you don't want. 

So to access selenium on the just "localhost", you have two options:

  1. Configure selenium to start on port 80, which I do not recommend. 

  2. Start the selenium using the following command:

    docker run -d -p 80:4444 --shm-size 2g selenium/standalone-chrome:4.0.0-beta-3-prerelease-20210329

Using which you are mapping port "80" of the host system to port "4444" of the container. 

You should now be able to access selenium on "http://localhost" but keep in mind that you would not be able to run anything else on port "80" of the host. 

Upvotes: 0

kolaente
kolaente

Reputation: 1302

If you run the container with the --net=host flag, it will be accessible as if the service inside of it was running on the host. From the docs:

If you use the host network mode for a container, that container’s network stack is not isolated from the Docker host (the container shares the host’s networking namespace), and the container does not get its own IP-address allocated.

In your example you should be able to access selenium on localhost:4444 without exposing any ports and selenium should be able to access your service on localhost from the container just fine.

We've found the easiest way to get selenium from docker running properly was to dockerize the application to test and put the selenium container in a network with the dockerized application. This also helps a lot when you want to test multiple instances on one host.

Upvotes: 2

Related Questions