scalaseleniumdocker-composeselenium-docker

Selenium on Docker: Testing a Docker Image within the same `docker-compose` file


Scenario:

I started from this SeleniumHQ/docker-selenium

Docker Compose:

version: "3.1"
services:    

  my-app:
    image: pame/play-binding-form-server
    networks:
      - mynet
    ...
    ports:
      - "9000:9000"

  chrome-webdriver:
    image: selenium/standalone-chrome:3.141.59-europium
    networks:
      - mynet
    volumes:
      - /dev/shm:/dev/shm
    ports:
      - "4444:4444"
    depends_on:
      - my-app

networks:
  mynet:

docker ps:

CONTAINER ID        IMAGE                                          COMMAND                  CREATED             STATUS              PORTS                              NAMES
53ac65c2babd        selenium/standalone-chrome:3.141.59-europium   "/opt/bin/entry_poin…"   13 minutes ago      Up 13 minutes       0.0.0.0:4444->4444/tcp             e2e_chrome-webdriver_1
fc0ca2af3148        pame/play-binding-form-server                  "/pme123/conf/docker…"   17 minutes ago      Up 17 minutes       0.0.0.0:9000->9000/tcp, 9443/tcp   pme123-forms
d

My test looks like:

  import org.openqa.selenium.remote.DesiredCapabilities
  import org.openqa.selenium.remote.RemoteWebDriver

  val driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),
    DesiredCapabilities.chrome())
  driver.get("http://localhost:9000/")

This returns a HTML Page:

This site can’t be reached

I tried different versions, with no success.

When running the webdriver locally everything works.

Is this scenario not possible or do I miss something?


Solution

  • That's a very classic question.

    Since my-app and chrome-webdriver are on two separate containers, chrome-webdrive can't reach my-app on its localhost(127.0.0.1).

    Keep in mind that one of docker's functions is to isolate running environments with the concept of container. That is, localhost on your host machine is not the same as the one of a container, and localhost on container A is not the same as the one on container B, they all have their own localhost.

    To connect two containers within the same network, just use their container name or service name as the host name.

    In your case driver.get("http://my-app:9000/").