运行 docker 在 jenkins 管道中构建的 dpkg 命令抛出 "cannot access archive: No such file or directory"

running dpkg command in docker build in jenkins pipeline throws "cannot access archive: No such file or directory"

我正在尝试在 Jenkins 管道中执行 Cypress E2E 测试。为此,我尝试构建一个 Docker 图像,然后在其中执行 shell 命令。这是我正在使用的 Docker 文件中的代码:

FROM jenkins/ssh-agent:latest
USER root
RUN apt-get update 
RUN apt install -y wget libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb fonts-liberation xdg-utils curl git
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
RUN mkdir -p /tmp && cd /tmp
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb

当我在本地 Jenkins 的管道中 运行 构建此 Docker 图像时成功构建,但是当我尝试 运行 在服务器上的 Jenkins 中构建它时,Chrome 成功下载并保存,但是 dpkg 命令 returns 出现以下错误: dpkg: error: cannot access archive 'google-chrome-stable_current_amd64.deb': No such file or directory 这是整个控制台日志:

Step 1/9 : FROM jenkins/ssh-agent:latest
 ---> 4f81b94cdf9e
Step 2/9 : USER root
 ---> Using cache
 ---> e59a8cc73033
Step 3/9 : RUN apt-get update
 ---> Using cache
 ---> feaf1b0f2695
Step 4/9 : RUN apt install -y wget libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb fonts-liberation xdg-utils curl git
 ---> Using cache
 ---> a9decc4a3bee
Step 5/9 : ENV LANG C.UTF-8
 ---> Using cache
 ---> ea6fe6ebf772
Step 6/9 : ENV LC_ALL C.UTF-8
 ---> Using cache
 ---> b1830b029dba
Step 7/9 : RUN cd /tmp
 ---> Using cache
 ---> 19798a9eaaa3
Step 8/9 : RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
 ---> Running in 3c8d6e7085d3
--2021-10-15 13:36:22--  https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
Resolving dl.google.com (dl.google.com)... 142.250.185.174, 2a00:1450:4001:810::200e
Connecting to dl.google.com (dl.google.com)|142.250.185.174|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 90229076 (86M) [application/x-debian-package]
Saving to: ‘google-chrome-stable_current_amd64.deb’

     0K .......... .......... .......... .......... ..........  0% 26.1M 3s
    50K .......... .......... .......... .......... ..........  0% 28.9M 3s
   100K .......... .......... .......... .......... ..........  0% 21.3M 3s
   150K .......... .......... .......... .......... ..........  0% 48.2M 3s
   200K .......... .......... .......... .......... ..........  0% 29.8M 3s
   250K .......... .......... .......... .......... ..........  0% 29.7M 3s
   300K .......... .......... .......... .......... ..........  0% 43.3M 3s
...
...
...
 87850K .......... .......... .......... .......... .......... 99%  241M 0s
 87900K .......... .......... .......... .......... .......... 99%  238M 0s
 87950K .......... .......... .......... .......... .......... 99%  181M 0s
 88000K .......... .......... .......... .......... .......... 99%  246M 0s
 88050K .......... .......... .......... .......... .......... 99%  244M 0s
 88100K .......... ....                                       100%  211M=0.6s

(147 MB/s) - ‘google-chrome-stable_current_amd64.deb’ saved [90229076/90229076]

Removing intermediate container 3c8d6e7085d3
 ---> 2c8e60b1b039
Step 9/9 : RUN dpkg -i google-chrome-stable_current_amd64.deb
 ---> Running in 60d2fbad1840
dpkg: error: cannot access archive 'google-chrome-stable_current_amd64.deb': No such file or directory
The command '/bin/sh -c dpkg -i google-chrome-stable_current_amd64.deb' returned a non-zero code: 2

如您所见,它甚至说 .deb 文件已保存,但是当我尝试 运行 时,现在可以使用 dpkg 访问它了。你能帮我解决这个问题吗?谢谢。

首先,Dockerfile不作为脚本执行。每个RUN都是单独执行的,就好像你打开了一个新的终端,所以

RUN cd /tmp

什么都不做,因为下一个RUN会在工作目录下执行,所以google chrome被下载到/home/jenkins.

你的问题是出于某种原因(.bashrc 脚本或其他原因)在启动 shell 之前清理了 jenkins 主目录。所以你下载 chrome,然后开始一个新的 shell,它删除了一个 .deb 然后你收到错误。

简单的修复将如下所示:

RUN mkdir -p /tmp && cd /tmp && \
    wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && \
    dpkg -i google-chrome-stable_current_amd64.deb && \
    rm google-chrome-stable_current_amd64.deb # remove .deb after installation

并实际将工作目录更改为 /tmp,只需执行

WORKDIR /tmp

如果目录不存在,将创建它(参见 Dockerfile reference)。