Selenium is a popular tool for browser automation. Java is a preferred language for many Selenium users. Docker makes it easy to isolate and deploy services. Combine all three, and you get a powerful testing setup. In this article, you’ll learn how to run a Java Selenium server using Docker.
Why Use Docker with Selenium?
Selenium tests often depend on browsers and drivers. Installing them on every machine takes time and effort. Docker simplifies this. It creates a container with everything set up. You can share this container across machines. That ensures consistency.
Also, Docker removes compatibility issues. Your tests will run the same way on every system. Therefore, you spend less time debugging.
Now, let’s look at how to run a Selenium server using Docker.
Step 1: Install Docker
Before anything else, install Docker on your system. Download and install Docker Desktop.

After installation, open a terminal. Run this command:
docker --version
You should see the Docker version. If so, Docker works correctly.
Step 2: Pull Selenium Docker Images
Selenium provides official Docker images. You can use them directly. To pull a standalone Chrome image, run:
docker pull selenium/standalone-chrome
Alternatively, for Firefox, use:
docker pull selenium/standalone-firefox
These images contain everything you need. They include the browser, driver, and Selenium server.
Step 3: Run the Selenium Server Container
Now it’s time to run the container. Use the following command:
docker run -d -p 4444:4444 --name selenium-server selenium/standalone-chrome
Here’s what each part means:
-d: Run in detached mode-p 4444:4444: Map port 4444 from container to host--name selenium-server: Name the containerselenium/standalone-chrome: The image to use
The server is now running. To confirm, open your browser and go to:
http://localhost:4444
You should see the Selenium Grid UI.
Step 4: Write Java Code for Selenium
Now you need Java code to connect to the server. Let’s write a simple test.
First, create a Maven project. In the pom.xml, add these dependencies:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.19.1</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>6.1.0</version>
</dependency>
</dependencies>Next, write your test class:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
public class SeleniumDockerTest {
public static void main(String[] args) throws Exception {
WebDriverManager.chromedriver().setup(); // Setup ChromeDriver automatically
URL url = new URL("http://localhost:4444/wd/hub");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("chrome");
WebDriver driver = new RemoteWebDriver(url, capabilities);
driver.get("https://example.com");
System.out.println("Page title: " + driver.getTitle());
driver.quit();
}
}This code connects to the Dockerized Selenium server. Then, it opens a webpage and prints the title.
Step 5: Run the Test
To run the test, build your Maven project. Then run the SeleniumDockerTest class.
You’ll see the browser inside the Docker container opening the site. The test prints the page title. Finally, the browser closes.
Step 6: Use Docker Compose (Optional)
For a more complex setup, consider using Docker Compose. This is particularly helpful when you have multiple services.
Create a file named docker-compose.yml:
services:
selenium:
image: selenium/standalone-chrome
ports:
- "4444:4444"Run it using:
docker-compose up -d
Now your Selenium server runs using Compose. You can scale, manage, or shut down services more easily.
Tips for Using Java Selenium with Docker
Here are some tips to improve your experience:
- Use Waits: Docker containers might take time to start. Use
WebDriverWaitto handle delays. - Check Logs: Run
docker logs selenium-serverto view logs. - Use Volumes: For saving reports, map volumes from the host to the container.
- Limit Resources: Set memory or CPU limits in Docker to avoid overuse.
Common Problems and Solutions
Problem 1: Connection refused on port 4444
Solution: Make sure the Docker container is running. Use docker ps to confirm.
Problem 2: Browser not opening
Solution: Ensure the browser is available in the image. Stick with official Selenium images.
Problem 3: The Test is too slow
Solution: Disable animations and images during test runs. Also, use headless mode if needed.
Modify capabilities like this:
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--disable-gpu");
WebDriver driver = new RemoteWebDriver(url, options);Benefits of Using Docker for Selenium
Let’s summarize the key benefits:
- Consistency: Same environment across machines
- Speed: Quick setup and teardown
- Scalability: Run tests in parallel across containers
- Isolation: Separate browser sessions from your host OS
These benefits make Docker a wise choice for Selenium projects.
When Not to Use Docker
Although Docker is powerful, it’s not always ideal. If you need to test browser features tied to hardware, Docker may not be the best option. Additionally, GUI testing in Docker can be challenging unless you utilize a VNC viewer.
In such cases, run tests on a real machine.
Finally
Running a Java Selenium server on Docker is simple. It’s efficient, portable, and scalable. With just a few commands, you get a working Selenium environment. That saves time and reduces errors.
If you’re working in a team, Docker ensures everyone runs the same tests in the same way. Add this setup to your CI/CD pipeline for even greater benefits.
So, give it a try. Use Docker with Java Selenium and streamline your testing process today.
This article was originally published on Medium.



