Install a Certificate for Headless Chrome in Selenium Docker
In this article
This guide walks you through installing a custom SSL/TLS certificate (for example, a corporate root CA or a self-signed certificate) into a Selenium Docker image so that headless Chrome trusts it. This is useful when your test environment uses internal HTTPS endpoints with certificates that aren't publicly trusted. You'll end up with a Docker image that has the certificate installed in the system trust store, and Chrome will load it automatically.
What You'll Need
- A Dockerfile (or a way to build a custom image based on
selenium/standalone-chromeorselenium/node-chrome) - The certificate file you want to install (usually a
.crtor.pemfile) - Basic familiarity with Docker commands and a running Docker environment
Step-by-Step
- Choose a base image. Use an official Selenium image like
selenium/standalone-chrome:latest(for a single container) orselenium/node-chrome:latest(if you use Selenium Grid). Check the image's documentation to confirm the base OS (Debian or Ubuntu) and the Chrome version. - Create a working directory. On your host machine, create a folder for your custom image, e.g.,
mkdir my-seleniumandcd my-selenium. - Copy your certificate file into this directory. Place your certificate (e.g.,
mycert.crt) in the same folder as your Dockerfile. - Create a Dockerfile. In that folder, create a file named
Dockerfile(no extension). Start with the base image, then copy the certificate into the container. Example:FROM selenium/standalone-chrome:latestCOPY mycert.crt /usr/local/share/ca-certificates/mycert.crt - Update the certificate store. Add a command to update the system CA certificates. For Debian/Ubuntu-based images, run:
RUN update-ca-certificates
This will read all certificates in/usr/local/share/ca-certificatesand add them to the system trust store. - Build the image. In your terminal, run
docker build -t my-selenium-chrome .(the dot means use the current directory). This will create a new image with your certificate installed. - Run your tests with the new image. Use the custom image in your Selenium tests. For example, run a container with
docker run -d -p 4444:4444 my-selenium-chromeand point your Selenium WebDriver tohttp://localhost:4444/wd/hub. - Verify the certificate is trusted. Optionally, inside the container, run a command like
curl https://your-internal-siteto see if the certificate is accepted without errors. If you see a “certificate verify failed” error, the certificate wasn't installed correctly.
Common Mistakes
- Using the wrong base image. Some Selenium images are based on Alpine Linux, which uses a different certificate store. If you use an Alpine-based image, the
update-ca-certificatescommand may not work as expected. Stick to the standard Debian/Ubuntu images. - Putting the certificate in the wrong directory. The directory
/usr/local/share/ca-certificatesis for Debian/Ubuntu. If you place it elsewhere, the update command won't pick it up. - Forgetting to run
update-ca-certificates. Copying the file alone does nothing; you must run the update command to make the system trust it. - Using a certificate with an unsupported format. The certificate must be in PEM format (base64 encoded). If it's in DER format, convert it first with
openssl x509 -in cert.der -out cert.pem -inform DER. - Not rebuilding the image after changes. If you modify the Dockerfile or certificate, you must rebuild the image. Running the old image will still have the old certificate.
Tips
- Use a specific tag. Instead of
latest, pin a specific version likeselenium/standalone-chrome:4.20.0-20240505to avoid unexpected changes in the base image. - Test locally first. Before integrating into CI/CD, test the image locally by running a simple Selenium script that hits an internal HTTPS URL.
- Consider using environment variables. If you need to install different certificates for different environments, you can pass the certificate path as a build argument or mount it at runtime (though mounting at runtime is more complex).
- Keep your certificate private. If your certificate is sensitive, don't commit it to a public repository. Use Docker secrets or build args to inject it during the build.
- Check Chrome's own certificate store. Chrome on Linux uses the system trust store, so installing the certificate system-wide is sufficient. You don't need to configure Chrome separately.
Troubleshooting
- “update-ca-certificates: command not found” – This means your base image isn't Debian/Ubuntu. Switch to a standard Selenium image (e.g.,
selenium/standalone-chrome). If you must use Alpine, you'll need to useapk add ca-certificatesand then copy the certificate to/usr/local/share/ca-certificatesand runupdate-ca-certificates(Alpine might require a different path). - Certificate still not trusted in Chrome. Verify the certificate is actually in the container's trust store by running
docker run my-selenium-chrome openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt your-site.crt(replace with your certificate). If it fails, the certificate may be invalid or the chain is incomplete. - Chrome still shows a certificate error in Selenium logs. Sometimes Chrome has its own internal certificate cache. Restart the container or use a fresh container to ensure the trust store is loaded.
- Certificate is for an IP address, not a domain. Chrome may reject certificates that don't match the hostname. Ensure your certificate includes the correct Subject Alternative Name (SAN) for the IP or domain you're testing.
- Build fails due to network issues. If
update-ca-certificatestries to download something and fails, check your network connection and proxy settings. You can add--network=hostto the build command if needed.
Frequently Asked Questions
Do I need to install the certificate in the Selenium grid hub as well?
No, only the nodes that run Chrome need the certificate. The hub just routes requests; it doesn't execute the browser.
Can I use a .pfx or .p12 file?
No, those are PKCS#12 formats. You need to convert them to PEM format first. Use openssl pkcs12 -in yourfile.pfx -out yourfile.pem -nodes and then extract the certificate part.
Will this work with ChromeDriver or only with Selenium?
This works for any Chrome instance running in the container, regardless of how it's launched. ChromeDriver will use the system trust store.
What if I need to install multiple certificates?
Copy all of them into /usr/local/share/ca-certificates/ and run update-ca-certificates once. It will process all files in that directory.
Can I install the certificate at runtime instead of build time?
Yes, but it's more complex. You'd need to mount the certificate into the container and run the update command in your test setup, but this requires root access and may not work in all environments. Build time is simpler and more reliable.
Does this affect the performance of my tests?
No, installing a certificate has negligible impact on performance. It just adds a file to the trust store.