'Docker access container from java application in different container

I created a Dockerfile and i want to conntect my crawler application with my selenium server. I have connected the services via link, but its not possible to reach the selenium server from my java application. What wrong?

My Docker Compose:

version: '3'
services:
  selenium:
    image: selenium/standalone-chrome:latest
    hostname: selenium
    ports:
      - "5901:5900"
      - "4000:4444"
    privileged: true
    shm_size: 2g

  scraper:
    container_name: scraper
    image: 'openjdk:11'
    volumes:
      - './:/app'
    working_dir: /app
    command: >-
      bash -c "cd /app && java -jar crawler.jar http://selenium:4000/wd/hub"
    stdin_open: true
    tty: true
    links:
      - "selenium:selenium"
    depends_on:
      - selenium

The output of selenium while starting

selenium_1    | 08:40:10.458 INFO [GridModel.setAvailability] - Switching Node 3e8df1bd-2417-4c4a-b793-376f45763366 (uri: http://172.18.0.4:4444) from DOWN to UP
selenium_1    | 08:40:10.458 INFO [LocalDistributor.add] - Added node 3e8df1bd-2417-4c4a-b793-376f45763366 at http://172.18.0.4:4444. Health check every 120s
selenium_1    | 08:40:10.529 INFO [Standalone.execute] - Started Selenium Standalone 4.1.4 (revision 535d840ee2): http://172.18.0.4:4444

But while running crawler I get this error:

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
scraper       | Build info: version: '4.1.4', revision: '535d840ee2'
scraper       | System info: host: '62e8dac61015', ip: '172.18.0.6', os.name: 'Linux', os.arch: 'amd64', os.version: '5.10.60.1-microsoft-standard-WSL2', java.version: '11.0.15'
scraper       | Driver info: org.openqa.selenium.remote.RemoteWebDriver
scraper       | Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--blink-settings=imagesEnab...], extensions: []}}], desiredCapabilities=Capabilities {browserName: chrome, goog:chromeOptions: {args: [--blink-settings=imagesEnab...], extensions: []}}}]     
scraper       | Capabilities {}
scraper       |         at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:585)
scraper       |         at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:248)
scraper       |         at org.openqa.selenium.remote.RemoteWebDriver.<init>...
scraper       | Caused by: java.io.UncheckedIOException: java.net.ConnectException: Connection refused: selenium/172.18.0.4:4000


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source