Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

geertjanknapen1's avatar

Laravel Dusk and Docker

So I've been racking my brain getting Laravel Dusk to work with docker. There's hardly anything online and what is online does not seem to work at all. I added Selenium as a service to my docker compose and changed relevant code. I would ask on SO but I fear I will get downvoted into oblivion, so I hope the fine people of Laracasts can guide me in the right direction.

I'm mainly hoping someone who is way more proficient in Docker can tell me if I got the docker part right.

the selenium service in docker-compose.yml looks as follows:

selenium:
  image: 'selenium/standalone-chrome'
  container_name: ${APP_NAME}_selenium
  volumes:
    - '/dev/shm:/dev/shm'
  ports:
    - "4444:4444"
    - "7900:7900"
  networks:
    mynetwork:

Containers build just fine and the website works.

After I installed Dusk using composer and the whole shebang, I have the following in the driver() method of my DuskTestCase.php, which according to my understanding now points to the correct url. (i.e. the Selenium service in my Docker Compose file)

protected function driver(): RemoteWebDriver
{
    $options = (new ChromeOptions)->addArguments([
        '--disable-gpu',
        '--no-sandbox',
        '--ignore-ssl-errors',
        '--whitelisted-ips=""'
    ]);

    return RemoteWebDriver::create(
        "http://my-app-name_selenium:4444/wd/hub”,
        DesiredCapabilities::chrome()->setCapability(
            ChromeOptions::CAPABILITY, $options
        )
    );
}

But tests never run properly. They always result in the following error:

Facebook\WebDriver\Exception\UnknownErrorException: unkown error: net::ERR_CONNECTION_REFUSED (Session info: chrome=127.0.6533.119)

Now I have no clue why that happens, some guidance would be appreciated.

0 likes
3 replies
CodingArchaeo's avatar

The internal networking of Docker (Compose) projects is confusing to many, so I would hope your question would be received kindly on any forum.

I would suggest looking at two things. First, make sure that both containers (selenium and the one your are running DuskTestCase.php from) are on the same network (mynetwork).

Second, instead of using the container name, use the service name. The my-app-name_selenium domain you use in your code looks like the container name that you would see from the host (your computer), for example when running docker ps. But that full name is probably not known from within the docker project. Instead it (the DNS within Docker) is only aware of the service names (i.e. the keys used in the docker-compose.yml file as children of services: ). In your case that would be selenium.
(Perhaps you can even leave out http://)

geertjanknapen1's avatar

@CodingArchaeo Thanks for the pointers, they are in the same network.

And, regarding the naming. my-app-name_selenium is present as the container_name for the Selenium service, and running docker inspect my-app-name_selenium also confirms the following aliases, so I don't think that is an issue:

"Aliases": [
    "my-app-name_selenium",
    "selenium",
    "8030c209e86b
],

But unfortunately, this keeps throwing the same error (also without the http:// part).

kovacs's avatar

Here's a working example of a dockerized dusk+selenium setup. Migrate the laravel database before creating tests.

Please or to participate in this conversation.