Installing Google Chrome in Docker Containers
Problem Statement
When installing Google Chrome in Docker containers using direct .deb
package downloads, many developers encounter dependency issues that halt the build process. The common approach of:
RUN wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb
RUN apt -f install -y
Fails during Docker builds because dpkg -i
exits with a non-zero status code when encountering missing dependencies, causing the Docker build to stop immediately, even though apt -f install
would normally resolve these issues in an interactive session.
Recommended Solutions
Method 1: Install .deb Package Using apt-get
The simplest solution is to use apt-get install
instead of dpkg -i
to handle the downloaded package, as it automatically resolves dependencies:
RUN apt-get update && apt-get install -y wget
RUN wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN apt-get install -y ./google-chrome-stable_current_amd64.deb
Method 2: Add Google Chrome Repository
For a more maintainable approach, add Google's official repository and install from there:
# Install required tools first
RUN apt-get update && apt-get install -y wget gnupg
# Add Google Chrome repository
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
# Install Chrome
RUN apt-get update && apt-get -y install google-chrome-stable
Method 3: Chrome for Testing (Recommended for Automation)
For testing frameworks like Selenium or Puppeteer, use Google's Chrome for Testing:
# Fetch and install latest stable Chrome version
RUN CHROME_VERSION=$(curl -sSL https://googlechromelabs.github.io/chrome-for-testing/ | awk -F 'Version:' '/Stable/ {print $2}' | awk '{print $1}' | sed 's/<code>//g; s/<\/code>//g') \
&& CHROME_URL="https://storage.googleapis.com/chrome-for-testing-public/${CHROME_VERSION}/linux64/chrome-linux64.zip" \
&& echo "Fetching Chrome version: ${CHROME_VERSION}" \
&& curl -sSL ${CHROME_URL} -o /tmp/chrome-linux64.zip \
&& mkdir -p /opt/google/chrome \
&& unzip -q /tmp/chrome-linux64.zip -d /opt/google/chrome \
&& rm /tmp/chrome-linux64.zip
Complete Dockerfile Examples
Basic Ubuntu Installation
FROM ubuntu:22.04
# Install Chrome with repository approach
RUN apt-get update && apt-get install -y wget gnupg
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
RUN apt-get update && apt-get -y install google-chrome-stable
# Verify installation
RUN echo "Chrome: " && google-chrome --version
Node.js with Chrome for Selenium
FROM node:18
# Install Chrome dependencies
RUN apt-get update && apt-get install -y \
wget \
libgtk-3-dev \
libnotify-dev \
libgconf-2-4 \
libnss3 \
libxss1 \
libasound2 \
libxtst6 \
fonts-liberation
# Install Chrome
RUN wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN apt-get install -y ./google-chrome-stable_current_amd64.deb
RUN rm google-chrome-stable_current_amd64.deb
# Verify installation
RUN echo "Chrome version: " && google-chrome --version
Chrome Sandbox Considerations
For security reasons, Chrome typically runs with the --no-sandbox
flag in Docker containers. For better security, create a dedicated user:
# Add user to avoid --no-sandbox flag
RUN groupadd -r chromeuser && useradd -r -g chromeuser -G audio,video chromeuser \
&& mkdir -p /home/chromeuser/Downloads \
&& chown -R chromeuser:chromeuser /home/chromeuser
USER chromeuser
Common Issues and Troubleshooting
Missing Dependencies
If you encounter missing dependency errors, ensure all required libraries are installed:
# Common Chrome dependencies
RUN apt-get update && apt-get install -y \
libnss3 \
libxss1 \
libasound2 \
libxtst6 \
libgtk-3-0 \
libgbm1 \
libxshmfence1 \
ca-certificates \
fonts-liberation \
xdg-utils
GPG Key Issues
If repository installation fails due to GPG key issues, use this alternative approach:
# Alternative GPG key installation
RUN mkdir -p /etc/apt/keyrings
RUN wget -q -O /etc/apt/keyrings/google-chrome.gpg https://dl.google.com/linux/linux_signing_key.pub
RUN echo "deb [signed-by=/etc/apt/keyrings/google-chrome.gpg] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list
Best Practices
Always combine apt-get commands to reduce layer size:
dockerfileRUN apt-get update && apt-get install -y \ wget \ gnupg \ && rm -rf /var/lib/apt/lists/*
Clean up downloaded packages to minimize image size:
dockerfileRUN wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \ && apt-get install -y ./google-chrome-stable_current_amd64.deb \ && rm google-chrome-stable_current_amd64.deb
Use specific versions for production stability when using the testing version approach.
Version Verification
Always verify Chrome installation with:
RUN echo "Chrome version: " && google-chrome --version
By following these methods, you can reliably install Google Chrome in Docker containers without encountering dependency issues during the build process.