Stop Laravel Dusk Chrome Processes in Docker
In this article
To stop Laravel Dusk from leaving orphaned Chrome processes in a Docker environment, you must ensure that the Chrome driver is explicitly shut down and that the Docker container is configured to handle process signals correctly. When Dusk tests crash or are interrupted, the ChromeDriver and Chrome binary often remain active in the background, consuming system memory and eventually causing subsequent test runs to fail due to resource exhaustion or port conflicts.
What You'll Need
- A Laravel project with Dusk installed.
- A Dockerized environment (e.g., Laravel Sail or a custom Docker Compose setup).
- Root or sudo access to the Docker host to manage containers.
- Basic familiarity with the
docker execcommand.
Step-by-Step
-
Implement the tearDown method: Ensure every Dusk test class properly calls the parent tearDown method. In your test files, the
tearDownmethod is responsible for closing the browser session. If you override this method without callingparent::tearDown(), the driver remains open.protected function tearDown(): void { parent::tearDown(); } -
Configure the Docker Init Process: Docker containers often run the application as PID 1, which does not forward signals (like SIGTERM) to child processes. To fix this, add
init: trueto your service definition indocker-compose.yml. This uses a tiny init binary (tini) to reap zombie processes and forward signals to Chrome.services: laravel.test: build: context: ./ dockerfile: Dockerfile init: true -
Add a Cleanup Script to your CI/CD or Local Workflow: Since crashes can still happen, create a shell script to kill any lingering Chrome processes before starting a new test suite. Add this command to your test runner script:
docker exec your_container_name pkill -f chrome -
Optimize Chrome Options in DuskTestCase: Modify the
driver()`method in yourtests/DuskTestCase.phpto include flags that reduce the likelihood of process hangs. Specifically, use--disable-dev-shm-usageto prevent crashes in Docker containers where/dev/shmis too small.$options->addArguments([ '--disable-dev-shm-usage', '--no-sandbox', '--headless', '--disable-gpu', ]); -
Verify Process Termination: Run your tests, then execute
docker exec your_container_name ps aux | grep chrome. If the list is empty after the tests finish, the processes are being cleaned up correctly.
Common Mistakes
- Ignoring the /dev/shm issue: Many developers forget
--disable-dev-shm-usage. By default, Docker allocates 64MB to shared memory, which is often insufficient for Chrome, causing the browser to crash and leave a zombie process behind. - Manual Process Killing via Host: Attempting to kill Chrome processes from the host machine rather than inside the container. This often fails because the process IDs (PIDs) differ between the host and the container namespace.
- Overriding tearDown without parent call: This is the most common programmatic cause of orphaned drivers in Laravel Dusk.
Tips
- Use a dedicated Test Container: Instead of running Dusk in your main app container, use a separate Selenium or Chrome standalone container. This isolates the browser processes and allows you to restart the entire container without affecting your application state.
- Increase Shm-Size: If you prefer not to use
--disable-dev-shm-usage, you can increase the shared memory size indocker-compose.ymlusingshm_size: '2gb'. - Automate Cleanup: Integrate the
pkillcommand into a Makefile or a Composer script (e.g.,composer test-clean) to make it a one-command habit.
Troubleshooting
If Chrome processes still persist after implementing the above steps, check the following:
- Check for Zombie Processes: Run
ps auxinside the container. If you see processes marked as<defunct>, it means the parent process is not reaping the child. This confirms thatinit: trueis missing or not working. - Inspect Driver Logs: Check the ChromeDriver logs for "SessionNotCreatedException". This often happens when a previous process is still holding onto port 9515.
- Memory Constraints: If the container is hitting its RAM limit, the Linux OOM (Out Of Memory) killer might terminate the main process but leave the Chrome child processes in a hung state. Increase the Docker memory limit.
Frequently Asked Questions
Why does Chrome stay open even after the tests finish?
This usually happens because the ChromeDriver process does not receive a termination signal from the PHP process, or the PHP process crashed before it could send the shutdown command to the browser.
Does --headless mode prevent orphaned processes?
No. Headless mode only prevents the GUI from rendering; it does not change how the process is managed by the operating system or Docker.
Is it safe to use pkill -f chrome in a production environment?
No. You should only run cleanup scripts in your testing or development containers. Running this on a production server could kill legitimate browser-based processes.
Will adding init: true slow down my container startup?
No. The init process is extremely lightweight and has a negligible impact on startup time while providing significant stability for process management.
What is the difference between --no-sandbox and --disable-dev-shm-usage?
--no-sandbox disables the Chrome security sandbox, which is often required to run Chrome as root in Docker. --disable-dev-shm-usage forces Chrome to use /tmp instead of /dev/shm for shared memory, preventing crashes in memory-constrained containers.