Selenium · Browser automation foundations
How do you run Selenium in Docker and what are the pitfalls?
Short answer
Run official Selenium images as standalone or Grid components, publish the WebDriver port, increase shared memory, and pin image versions. Mount volumes for downloads and use VNC or logs for debugging.
The official Selenium images package a browser, a driver and the Grid components. A standalone image is enough for one browser; a hub-and-node compose setup provides scale and multiple browsers. Containerised browsers behave like real browsers but have different resource limits.
docker run --rm -d --name selenium -p 4444:4444 -p 7900:7900 --shm-size=2g selenium/standalone-chrome:128.0
docker run --rm -d --name selenium -p 4444:4444 -p 7900:7900 --shm-size=2g -v "$PWD/downloads:/home/seluser/Downloads" selenium/standalone-chrome:128.0- The default /dev/shm in Docker is small; Chrome can crash without a larger shm size
- Pin a specific image tag so browser and driver upgrades are deliberate
- Port 4444 exposes WebDriver; port 7900 exposes a VNC viewer for debugging
- Downloads land inside the container unless a volume is mounted
- Grid images separate the hub from browser nodes and need a shared network
- Container CPU and memory limits must match the number of browser slots
A browser inside a container is still subject to container isolation. File uploads from the host need a mounted path or a file detector, downloads need a volume or API verification, and a crashed container loses its session. Health checks and restart policies matter for long-running Grid deployments.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why does Chrome crash in Docker without a larger shm size?
- 2How do you retrieve a downloaded file from a container?
- 3What is the difference between standalone and Grid images?
- 4Why pin the image tag in CI?
Say it in the interview
The official images package the browser, driver and Grid. I use a standalone image for one browser or a hub-and-node setup for scale, always with an increased shm size because the Docker default is too small for Chrome. I pin the image tag, mount a download volume, and use VNC or logs when a test fails. Container CPU and memory must match the number of browser slots, or sessions will crash under load.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Selenium mock interview and get scored feedback on depth, clarity and confidence.