简体   繁体   中英

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

I am trying to execute Cypress E2E Tests in Jenkins pipeline. To do that I am trying to build a Docker Image and then execute shell commands in it. This is the code from Dockerfile that I am using:

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

This Docker Image is successfully built when I am running it in a pipeline in my local Jenkins, but when I try to run it in a Jenkins on a server, the Chrome gets successfully downloaded and saved, but the dpkg command returns the following error: dpkg: error: cannot access archive 'google-chrome-stable_current_amd64.deb': No such file or directory Here is the whole console log:

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

As you can see it even says that the .deb file gets saved but when I try to run it with dpkg is now accesible. Can you help me how this can be resolved? Thanks.

First of all, Dockerfile is not executed as a script. Each RUN is executed separately as if you open a new terminal, so

RUN cd /tmp

does nothing, because next RUN will be executed in working directory, so google chrome is downloaded to /home/jenkins .

And your problem is that for some reason (.bashrc script or something else) cleans jenkins home directory before starting shell. So you download chrome, then start a new shell, it deletes a .deb and you receive the error.

The simple fix will look like this:

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

And to actually change working directory to /tmp , just do

WORKDIR /tmp

If directory doesn't exist, it will be created (see Dockerfile reference ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM