Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save bgizdov/6fdf747f22eb57d3a624ffebbc3b5b56 to your computer and use it in GitHub Desktop.

Select an option

Save bgizdov/6fdf747f22eb57d3a624ffebbc3b5b56 to your computer and use it in GitHub Desktop.
docker container with specific versions of node, alpine and npm
Here is my solution which is using unofficial node builds from https://nodejs.org/en/download
1. Create Dockerfile, with desired alpine, node and npm versions
In the example:
- alpine v3.17.2
- nodejs v18.16.0
- npm v9.6.6
```
FROM alpine:3.17.2
ENV NODE_PACKAGE_URL https://unofficial-builds.nodejs.org/download/release/v18.16.0/node-v18.16.0-linux-x64-musl.tar.gz
RUN apk add libstdc++
WORKDIR /opt
RUN wget $NODE_PACKAGE_URL
RUN mkdir -p /opt/nodejs
RUN tar -zxvf *.tar.gz --directory /opt/nodejs --strip-components=1
RUN rm *.tar.gz
RUN ln -s /opt/nodejs/bin/node /usr/local/bin/node
RUN ln -s /opt/nodejs/bin/npm /usr/local/bin/npm
# npm version comming with node is 9.5.1
# To install specific npm version, run the following command, or remove it to use the default npm version:
RUN npm install -g [email protected]
```
2. Build a docker image
```
docker build -t node-18.16.0-npm-9.6.6-alpine-3.17.2 .
[+] Building 24.4s (15/15) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 753B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/alpine:3.17.2 0.7s
=> [ 1/11] FROM docker.io/library/alpine:3.17.2@sha256:ff6bdca1701f3a8a67e328815ff2346b0e4067d32ec36b7992c1fdc001dc8517 0.0s
=> CACHED [ 2/11] RUN apk add libstdc++ 0.0s
=> CACHED [ 3/11] WORKDIR /opt 0.0s
=> CACHED [ 4/11] RUN wget https://unofficial-builds.nodejs.org/download/release/v18.16.0/node-v18.16.0-linux-x64-musl.tar.gz 0.0s
=> CACHED [ 5/11] RUN mkdir -p /opt/nodejs 0.0s
=> [ 6/11] RUN tar -zxvf *.tar.gz --directory /opt/nodejs --strip-components=1 4.2s
=> [ 7/11] RUN rm *.tar.gz 0.3s
=> [ 8/11] RUN ls -l /opt/nodejs 0.6s
=> [ 9/11] RUN ln -s /opt/nodejs/bin/node /usr/local/bin/node 0.2s
=> [10/11] RUN ln -s /opt/nodejs/bin/npm /usr/local/bin/npm 0.2s
=> [11/11] RUN npm install -g [email protected] 17.1s
=> exporting to image 1.0s
=> => exporting layers 0.9s
=> => writing image sha256:e133d992089c2efd071b423fa093f81bf4a732ac06c1ec7dd6cb09cec894a9b3 0.0s
=> => naming to docker.io/library/node-18.16.0-npm-9.6.6-alpine-3.17.2
```
3. Run the container and check the installed versions
```
docker run -it node-18.16.0-npm-9.6.6-alpine-3.17.2 node --version
v18.16.0
```
```
docker run -it node-18.16.0-npm-9.6.6-alpine-3.17.2 npm --version
9.6.6
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment