如何在Docker容器中安装Google Chrome

46

我正在尝试在Docker容器中安装Chrome。我执行以下命令:

RUN apt-get install -y wget
RUN wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb  # problem here
RUN apt -f install -y
问题在于dpkg -i由于缺少依赖项而失败。原则上,这不是一个大问题,因为下一个命令应该可以解决这个问题,并且当从容器内部交互式运行时确实可以解决。但问题在于,在构建docker容器时,此错误会使构建过程停止。
dpkg: error processing package google-chrome-stable (--install):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 google-chrome-stable
root@78b45ab9aa33:/# 
exit

我该如何解决这个问题?有没有更简单的方法安装Chrome而不引起依赖问题?我找不到要添加的软件源,以便我可以运行常规的apt-get install google-chrome命令,这就是我想做的事情。在Google Linux存储库中,他们只提到“软件包会自动配置所需的存储库设置”。但这并不完全是我收到的信息...


1
这可能会有所帮助:https://medium.com/dot-debug/running-chrome-in-a-docker-container-a55e7f4da4a8 - Facty
没错!谢谢。 - Pythonist
1
很酷,能和我们分享你的Dockerfile吗?可能还有其他人在寻找答案 :) - Facty
5个回答

61
在@Facty的评论和进一步搜索后,我找到了两种解决方法来安装Google Chrome而不引发此错误。我将在下面发布它以供将来参考或遇到相同问题的人使用。
实际上有两种方法可以在Docker容器上安装Chrome:
如果您手动下载deb文件,您可以使用apt-get而不是dpkg进行安装。这将自动安装依赖项,而无需稍后调用apt -f install -y
RUN 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

另一种解决方案是添加软件源(安装 GPG 密钥)并直接从中安装,跳过手动下载的步骤。
RUN apt-get install -y wget
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

2
两者都卡在“选择地理区域:”处,要求输入数字。如何处理?有什么想法吗? - Yaser Sakkaf
5
请将 ENV DEBIAN_FRONTEND noninteractive 添加到 Dockerfile 中。 - slhck
对于第一种选择(安装deb文件),在安装之前您可能还需要执行'apt-get update'命令,至少我是这样做的。 - Marcus

11

以下是 Node 版本(基于 Debian 的)的 Dockerfile 示例:

FROM node:16.16.0 as base
# Chrome dependency Instalation
RUN apt-get update && apt-get install -y \
    fonts-liberation \
    libasound2 \
    libatk-bridge2.0-0 \
    libatk1.0-0 \
    libatspi2.0-0 \
    libcups2 \
    libdbus-1-3 \
    libdrm2 \
    libgbm1 \
    libgtk-3-0 \
#    libgtk-4-1 \
    libnspr4 \
    libnss3 \
    libwayland-client0 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxkbcommon0 \
    libxrandr2 \
    xdg-utils \
    libu2f-udev \
    libvulkan1
 # Chrome instalation 
RUN curl -LO  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
# Check chrome version
RUN echo "Chrome: " && google-chrome --version

在u20.04上非常顺利。谢谢。其他所有答案都失败了。 - AhmFM
我遇到了很多错误,显示“请求了一个不可能的情况”。 - fotoflo
@fotoflo 看起来你走了捷径,没有导入“# Chrome 依赖安装”,如果你这样做了,错误就会消失。 - Andrzej Krawczuk
1
即使添加了“# Chrome 依赖安装”,问题仍然存在。当我尝试使用 gcloud submit 命令构建镜像时,一切正常,但是使用 Docker 时,我遇到了与 @fotoflo 相同的问题。 - Idhem

3
如果你要在Python中使用它来运行Selenium,这是解决我的问题的方法。
RUN apt -f install -y
RUN apt-get install -y wget
RUN wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN apt-get install ./google-chrome-stable_current_amd64.deb -y

有时候,使用wget无法解决问题,因为它缺乏支持。所以您可以使用apt -f install -y
@Pythonist唯一的错误是命令的混乱。

1
请注意,某些基础镜像可能具有或不具有 wgetgnupg,因此完整的工作 Dockerfile 示例为:
FROM ubuntu:22.04

RUN apt-get update; apt-get clean

# Install wget.
RUN apt-get install -y wget

RUN apt-get install -y gnupg

# Set the Chrome repo.
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


1
嗨,谢谢。如果我想使用特定版本的Chrome(例如114),我该如何在上述代码片段中实现? - undefined

-4

apt-get 不起作用。

你应该使用 dpkg -i ./google-chrome.deb


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接