Dockerfile: add npm to an existing docker image

2020-06-16 02:13发布

Having a Jenkins Docker image, I would like to add the complete 'npm' environment to that image. So after building the Dockerfile I have an image with both Jenkins and the 'npm' environment.

The purpose is that a Jenkins job to run the shell command 'npm'. So 'npm' should be on the $PATH (in Ubuntu).

I have already a Dockerfile with a lot stuff in there like Jenkins and Maven.

A solution for node was described in this post. The important thing is, can I do something simular? Which folders should I copy to the Jenkins docker image?

FROM node as nodejs

FROM jenkins/jenkins
// All kinds of other stuff goes here
COPY --from=nodejs /usr/local/bin/node /usr/local/bin/node ???

Installing 'npm' automatically within a Jenkins Global tool is not my preferred solution.

2条回答
神经病院院长
2楼-- · 2020-06-16 02:54

Using multiple FROM directives is not a feature, it's a bug. It's proposed to remove it and you should avoid using it

at all costs!

If you need npm in your jenkins, just install it there. It's based on openjdk image anyway.

FROM jenkins/jenkins

RUN apt-get install -y curl \
  && curl -sL https://deb.nodesource.com/setup_9.x | bash - \
  && apt-get install -y nodejs \
  && curl -L https://www.npmjs.com/install.sh | sh
查看更多
Evening l夕情丶
3楼-- · 2020-06-16 03:02

I went into the same problem some time ago. In my case it turned out that it is better to use a throw-away containers with proper volumes instead of using a installed node/npm in the container with Jenkins.

Why? It turned out that many different version of npm have been used across the time. Setting up two versions of npm is possible of course (take a look at https://github.com/creationix/nvm) but the best solution I've chosen was that: https://getintodevops.com/blog/the-simple-way-to-run-docker-in-docker-for-ci.

However, please take notice that it was my case with my projects. I used docker inside the Jenkins container so that it was possible to use anything by using a job configuration (or later - Jenkinsfiles).

查看更多
登录 后发表回答