0%

Install Headless Chrome with Docker

I recently used Headless Chrome together with Selenium to test a frontend project. The results were pretty good, and adding the tests after Jenkins deployment gave at least some confidence that the frontend still worked after each release.

This post records a Dockerfile for installing Headless Chrome on Ubuntu 20.04 so I can find it again later.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
FROM ubuntu:20.04

ARG APT_MIRROR_HOST=mirrors.tuna.tsinghua.edu.cn
ENV DEBIAN_FRONTEND noninteractive

RUN sed -i "s/archive.ubuntu.com/${APT_MIRROR_HOST}/g" /etc/apt/sources.list \
&& sed -i "s/security.ubuntu.com/${APT_MIRROR_HOST}/g" /etc/apt/sources.list

RUN apt update
RUN apt install -y wget fonts-wqy-microhei
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb;exit 0
RUN apt install -f -y
RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

Notes:

  • The APT mirror is switched to Tsinghua’s mirror to speed up dependency installation.
  • DEBIAN_FRONTEND=noninteractive is set because tzdata may otherwise prompt during installation and cause docker build to hang.
  • Because of that non-interactive setting, the final timezone configuration is handled manually.
  • fonts-wqy-microhei is installed so Chinese text can render correctly, especially when exporting PDFs.
  • Installing Chrome with dpkg -i is expected to fail at first because dependencies are missing, so the command ends with exit 0.
  • apt install -f -y is then used to repair dependencies and complete the installation.
如果我的文字帮到了您,那么可不可以请我喝罐可乐?