简体   繁体   中英

Add nodejs and npm to Jenkins docker image

I'm trying to do a quick experiment or two with Jenkins. For this I intended to use the jenkins image and add nodejs and npm to it. Here's what I'm trying to do:

FROM jenkins

USER root

RUN curl -sL https://deb.nodesource.com/setup_6.x | bash - \
    && apt-get update \
    && apt-get install -y \
        nodejs

RUN npm install -g npm

RUN npm --version

USER jenkins

The npm --version call will fail though, with this error:

Step 5/6 : RUN npm --version
 ---> Running in 48a250a4fdb8
module.js:471
    throw err;
    ^

Error: Cannot find module 'process-nextick-args'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/usr/lib/node_modules/npm/node_modules/readable-stream/lib/_stream_readable.js:26:23)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
The command '/bin/sh -c npm --version' returned a non-zero code: 1

What is the proper way (if any) to fix this?

Try the following, it uses docker multi-stage build to copy dependencies from one image to another. This will allow you to avoid all the special things that need to be done to install node in docker. There is an official docker image that is built by the docker team, so you use that as follows:

FROM node

FROM jenkins
USER root
COPY --from=0 /usr/local  /usr/local
RUN npm --version
USER jenkins

npm --version will output 5.3.0

Jenkins image by docker uses minimal installation for running Jenkins

FROM node:15.4 as nodejs
ENV JENKINS_HOME /var/jenkins_home

USER root
RUN rm -rf /var/lib/apt/lists/*
RUN rm -rf /etc/apt/sources.list.d/*
RUN apt-get update


#INSTALL NODE
COPY --from=nodejs /usr/local  /usr/local

RUN node -v
RUN npm -v 

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