5

I can create a docker container with PHP and composer with this:

FROM php:fpm
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

Is there any equivalent way to archive the same for node.js and npm? The only solutions I figured so far are:

If I use the last method, the container gets bloated in size as I copy everything over (around 908 MB). So I wonder if there is a smarter way. What folders/files would I have to copy over to just implement node and npm functionality?

Richard Wilson
  • 297
  • 4
  • 17
Wombat98
  • 51
  • 1
  • Node and npm are executables, you install them in the OS you're running inside docker like any other application that you need installed and ready in your container, you don't copy them over or compile them from source...? E.g. https://askubuntu.com/a/1113339 – Mike 'Pomax' Kamermans Jun 22 '21 at 21:55
  • 1
    if you want to avoid the bloat, install it and then copy it from the first stage to a 2nd stage. That way it's built for your OS platform inside the docker container but you don't pick up all the cruft. – Joe Jun 22 '21 at 22:50

2 Answers2

0

I used this:

COPY --from=node:18.16.0-slim /usr/local/bin /usr/local/bin

It increased the size of my Docker image by about 240MB, AFAICT.

veuncent
  • 1,599
  • 1
  • 20
  • 17
0

For node and npm to work you have to copy two directories in your Dockerfile:

# Get NodeJS
COPY --from=node:20-slim /usr/local/bin /usr/local/bin
# Get npm
COPY --from=node:20-slim /usr/local/lib/node_modules /usr/local/lib/node_modules
andreasm
  • 36
  • 3