Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile: add npm to an existing docker image

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.

like image 379
tm1701 Avatar asked Sep 19 '18 19:09

tm1701


People also ask

What is npm in Dockerfile?

npm is the command-line interface to the npm ecosystem. It is battle-tested, surprisingly flexible, and used by hundreds of thousands of JavaScript developers every day. Docker can be classified as a tool in the "Virtual Machine Platforms & Containers" category, while npm is grouped under "Front End Package Manager".

How do I create a docker image from an existing image?

You can create a new image by using docker command $docker build -f docker_filename . , It will first read the Dockerfile where the instructions are written and automatically build the image. The instruction in the Dockerfile contains the necessary commands to assemble an image.


2 Answers

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
like image 116
Alex Karshin Avatar answered Oct 28 '22 22:10

Alex Karshin


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).

like image 29
Marcin Avatar answered Oct 28 '22 22:10

Marcin