installing nodejs in dockerfile issue - node.js

I am trying to install nodejs in dockerfile with pyenv but I keep getting this error when I run it through my gitlab runner. I am trying to install version 16.12.0. Is there a better solution to this issue?
Dockerfile
#install npm
ENV NODE_VERSION=16.12.0
RUN apt install -y curl
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
RUN node --version
RUN npm --version
output
Step 15/25 : ENV NODE_VERSION=16.12.0
---> Running in 7623dfe4669c
Removing intermediate container 7623dfe4669c
---> c1486340596a
Step 16/25 : RUN apt install -y curl
---> Running in a4661b68566b
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Reading package lists...
Building dependency tree...
Reading state information...
curl is already the newest version (7.68.0-1ubuntu2.7).
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
Removing intermediate container a4661b68566b
---> d727779ba39b
Step 17/25 : RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
---> Running in c3661e8eead3
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (60) SSL certificate problem: self signed certificate in certificate chain
More details here: https://curl.haxx.se/docs/sslcerts.html
curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
Removing intermediate container c3661e8eead3
---> beffd784c86b
Step 18/25 : ENV NVM_DIR=/root/.nvm
---> Running in 22b69a1563b2
Removing intermediate container 22b69a1563b2
---> 821b73dfd5fa
Step 19/25 : RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
---> Running in 54c168c88ec7
/bin/sh: 1: .: Can't open /root/.nvm/nvm.sh
The command '/bin/sh -c . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}' returned a non-zero code: 127
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 127

In general you should avoid using version managers like nvm in Docker. You shouldn't need more than one version of a language interpreter at a time, and the version managers often require shell dotfile setup that's complicated to configure in Docker.
You don't say why you need Node. The easiest thing to do is to use the Docker Hub node image
FROM node:lts
# and none of what you show in the question
If you're using this to build the front end of your otherwise Python application, you can use a multi-stage build for this
FROM node:lts AS frontend
WORKDIR /frontend
COPY frontend/package*.json ./
RUN npm ci
COPY frontend/ ./
RUN npm build
FROM python:3.10
...
COPY --from=frontend /frontend/dist ./static/
If you really need Node in your otherwise Ubuntu-based image, the next easiest thing to do is just install it. The default Ubuntu nodejs package should work fine for most practical uses.
FROM ubuntu:20.04
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get --no-install-recomends --assume-yes \
nodejs
If you really need it in a custom image, and it really needs to be a super specific version of Node, you should be able to just download Node and install it.
FROM ubuntu:20.04
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get --no-install-recomends --assume-yes \
curl \
xz-utils
ARG node_version=v16.14.2
RUN cd /opt \
&& curl -LO https://nodejs.org/dist/${node_version}/node-${node_version}-linux-x64.tar.xz \
&& tar xJf node-${node_version}-linux-x64.tar.xz \
&& rm node-${node_version}-linux-x64.tar.xz
ENV PATH=/opt/node-${node_version}-linux-x64/bin:${PATH}

Related

docker ERROR: Could not find a version that satisfies the requirement apturl==0.5.2

I am using windows 10 OS. I want to build an container based on linux so I can replicate code and dependencies developed from ubuntu. When I try to build it outputs Error message as above.
From my understanding docker for desktop runs linux OS kernel under-the-hood therefore allowing window users to run linux based containers, not sure why it is outputting this error.
My dockerfile looks like this:
FROM ubuntu:18.04
ENV PATH="/root/miniconda3/bin:${PATH}"
ARG PATH="/root/miniconda3/bin:${PATH}"
RUN apt update \
&& apt install -y htop python3-dev wget
RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh \
&& mkdir root/.conda \
&& sh Miniconda3-latest-Linux-x86_64.sh -b \
&& rm -f Miniconda3-latest-Linux-x86_64.sh
RUN conda create -y -n ml python=3.7
COPY . src/
RUN /bin/bash -c "cd src \
&& source activate ml \
&& pip install -r requirements.txt"
requirements.txt contains:
apturl==0.5.2
asn1crypto==0.24.0
bleach==2.1.2
Brlapi==0.6.6
certifi==2020.11.8
chardet==3.0.4
click==7.1.2
command-not-found==0.3
configparser==5.0.1
cryptography==2.1.4
cupshelpers==1.0
dataclasses==0.7
When I run docker build command it outputs:
1.649 ERROR: Could not find a version that satisfies the requirement apturl==0.5.2 1.649 ERROR: No matching distribution found for apturl==0.5.2 Deleting it and running it lead to another error. All error seem to be associated with ubuntu packages.
Am I not running a ubuntu container? why aren't I allowed to install ubuntu packages?
Thanks!
You try to install ubuntu packages with pip (which is for python packages")
try apt install -y apturl
If you want to install python packages write pip install package_name

Unable to run aliyun-cli in Docker:stable container after installing it. Errors as command not found

I am unsure if stack overflow or system fault is the right stack exchange site but I'm going with stack overflow cause the alicloud site posted to add a tag and ask a question here.
So. I'm currently building an image based on Docker:stable, that is an alpine distro, that will have aliyun-cli installed and available for use. However I am getting a weird error of Command Not Found when I'm running it. I have followed the guide here https://partners-intl.aliyun.com/help/doc-detail/139508.htm and moved the aliyun binary to /usr/sbin
Here is my Dockerfile for example
FROM docker:stable
RUN apk update && apk add curl
#Install python 3
RUN apk update && apk add python3 py3-pip
#Install AWS Cli
RUN pip3 install awscli --upgrade
# Install Aliyun CLI
RUN curl -L -o aliyun-cli.tgz https://aliyuncli.alicdn.com/aliyun-cli-linux-3.0.30-amd64.tgz
RUN tar -xzvf aliyun-cli.tgz
RUN mv aliyun /usr/bin
RUN chmod +x /usr/bin/aliyun
RUN rm aliyun-cli.tgz
However when i'm running aliyun (which can be auto-completed) I am getting this
/ # aliyun
sh: aliyun: not found
I've tried moving it to other bins. Cding into the folder and calling it explicitly but still always getting a command not found. Any suggestions would be welcome.
Did you check this Dockerfile?
Also why you need to install aws-cli in the same image and why you will need to maintain it for your self when AWS provide managed aws-cli image.
docker run --rm -it amazon/aws-cli --version
that's it for aws-cli image,but if you want in existing image then you can try
RUN pip install awscli --upgrade
DockerFile
FROM python:2-alpine3.8
LABEL com.frapsoft.maintainer="Maik Ellerbrock" \
com.frapsoft.version="0.1.0"
ARG SERVICE_USER
ENV SERVICE_USER ${SERVICE_USER:-aliyun}
RUN apk add --no-cache curl
RUN curl https://raw.githubusercontent.com/ellerbrock/docker-collection/master/dockerfiles/alpine-aliyuncli/requirements.txt > /tmp/requirements.txt
RUN \
adduser -s /sbin/nologin -u 1000 -H -D ${SERVICE_USER} && \
apk add --no-cache build-base && \
pip install aliyuncli && \
pip install --no-cache-dir -r /tmp/requirements.txt && \
apk del build-base && \
rm -rf /tmp/*
USER ${SERVICE_USER}
WORKDIR /usr/local/bin
ENTRYPOINT [ "aliyuncli" ]
CMD [ "--help" ]
build and run
docker build -t aliyuncli .
docker run -it --rm aliyuncli
output
docker run -it --rm abc aliyuncli
usage: aliyuncli <command> <operation> [options and parameters]
<aliyuncli> the valid command as follows:
batchcompute | bsn
bss | cms
crm | drds
ecs | ess
ft | ocs
oms | ossadmin
ram | rds
risk | slb
ubsms | yundun
After a lot of lookup I found a github issue in the official aliyun-cli that sort of describes that it is not compatible with alpine linux because of it's not muslc compatible.
Link here: https://github.com/aliyun/aliyun-cli/issues/54
Following the workarounds there I build a multi-stage docker file with the following that simply fixed my issue.
Dockerfile
#Build aliyun-cli binary ourselves because of issue
#in alpine https://github.com/aliyun/aliyun-cli/issues/54
FROM golang:1.13-alpine3.11 as cli_builder
RUN apk update && apk add curl git make
RUN mkdir /srv/aliyun
WORKDIR /srv/aliyun
RUN git clone https://github.com/aliyun/aliyun-cli.git
RUN git clone https://github.com/aliyun/aliyun-openapi-meta.git
ENV GOPROXY=https://goproxy.cn
WORKDIR aliyun-cli
RUN make deps; \
make testdeps; \
make build;
FROM docker:19
#Install python 3 & jq
RUN apk update && apk add python3 py3-pip python3-dev jq
#Install AWS Cli
RUN pip3 install awscli --upgrade
# Install Aliyun CLI from builder
COPY --from=cli_builder /srv/aliyun/aliyun-cli/out/aliyun /usr/bin
RUN aliyun configure set --profile default --mode EcsRamRole --ram-role-name build --region cn-shanghai

/bin/sh: npm: command not found

I am creating a docker image for stf.installing nodejs directly using apt-get install nodejs,had many issues.So i decided to go the nvm way.but after installation RUN npm install fails
I am building the docker image with su username docker build ..
make sure apt is up to date
RUN apt-get update --fix-missing
RUN apt-get install -y curl
RUN sudo apt-get install -y build-essential libssl-dev
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 6
# Install nvm with node and npm
RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash \
&& source $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
#WORKDIR /usr/app
# Install app dependencies
RUN npm install
and the output
=> Downloading nvm from git to '/usr/local/nvm'
=> Cloning into '/usr/local/nvm'...
* (HEAD detached at v0.30.1)
master
=> Appending source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm
Downloading https://nodejs.org/dist/v6.13.0/node-v6.13.0-linux-x64.tar.xz...
######################################################################## 100.0%
WARNING: checksums are currently disabled for node.js v4.0 and later
Now using node v6.13.0 (npm v3.10.10)
default -> 6 (-> v6.13.0)
Now using node v6.13.0 (npm v3.10.10)
Removing intermediate container eb9cb6c46f34
---> eeef6bf9f0f1
Step 38/52 : ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
---> Running in 185bef8e530c
Removing intermediate container 185bef8e530c
---> 0e5bf7b1cfd9
Step 39/52 : ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
---> Running in 00d58493e199
Removing intermediate container 00d58493e199
---> 81ed9823020b
Step 40/52 : RUN npm install
---> Running in 1c7577133e24
/bin/sh: npm: command not found
The command '/bin/sh -c npm install' returned a non-zero code: 127
Please Help.Thanks
Ran the container and checked NPM path
root#69e513b99e68:/home/mobile/MobileFarmDocker#
echo $PATH
/usr/local/nvm/versions/node/v6.13.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
root#69e513b99e68:/home/mobile/MobileFarmDocker# which npm
/usr/local/nvm/versions/node/v6.13.0/bin/npm
Your $NODE_VERSION variable has value 6 as defined in the Dockerfile (ENV NODE_VERSION 6) but it should be 6.13.0 so that the following line may work properly:
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
(mapping to /usr/local/nvm/versions/node/v6.13.0/bin)
Otherwise, you are actually generating the following (wrong) path:
/usr/local/nvm/versions/node/v6/bin

The npm command is not found in my NodeJS docker container

I created a Docker image:
$ docker build -t stephaneeybert/nodejs .
Sending build context to Docker daemon 2.56 kB
Step 1 : FROM debian
---> 1b088884749b
Step 2 : RUN apt-get clean && apt-get update
---> Using cache
---> b12133d6342f
Step 3 : RUN apt-get install -y curl
---> Using cache
---> 22dfb4882b12
Step 4 : RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
---> Using cache
---> 27f2fac45254
Step 5 : RUN . ~/.nvm/nvm.sh; nvm install stable
---> Using cache
---> 20d99d545755
Step 6 : RUN . ~/.nvm/nvm.sh; nvm use stable
---> Using cache
---> 9ec14efb2407
Step 7 : RUN . ~/.nvm/nvm.sh; npm install -g npm
---> Using cache
---> d264d38565f3
Step 8 : EXPOSE 9001
---> Using cache
---> 29e3589557e1
Step 9 : ENTRYPOINT /usr/bin/tail -f /dev/null
---> Using cache
---> 2ce499300fe1
Successfully built 2ce499300fe1
The image script is:
FROM debian
RUN apt-get clean && apt-get update
RUN apt-get install -y curl
# Installing nodesjs
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
RUN . ~/.nvm/nvm.sh; nvm install stable
RUN . ~/.nvm/nvm.sh; nvm use stable
RUN . ~/.nvm/nvm.sh; npm install -g npm
EXPOSE 9001
ENTRYPOINT ["/usr/bin/tail", "-f", "/dev/null"]
Then I run the container and open a bash shell:
$ docker run -d -p 127.0.0.1:9001:9001 --name nodejs stephaneeybert/nodejs
c6dddf0a5eb0f11c897f63910eb01f2868fe0f39a80e5e2a580ef3a82935b27b
[stephane#stephane-ThinkPad-X301 nodejs]
$ docker exec -it nodejs bash
root#c6dddf0a5eb0:/#
Once in there, I try to get the version:
root#c6dddf0a5eb0:/# npm -v
bash: npm: command not found
But npm is not found.
When typing the command nvm use stable in the interactive shell, it give the following error: N/A: version "N/A" is not yet installed.
I understand there is an alias against a non existant node version.
The nvm ls command shows:
root#60446f9286d0:/# nvm ls
N/A
node -> stable (-> N/A) (default)
iojs -> N/A (default)
The debugger has this to show:
root#60446f9286d0:/# nvm debug
nvm --version: v0.32.1
$SHELL: /bin/bash
$HOME: /root
$NVM_DIR: '$HOME/.nvm'
$PREFIX: ''
$NPM_CONFIG_PREFIX: ''
nvm current: none
which node:
which iojs:
which npm:
npm config get prefix: bash: npm: command not found
npm root -g: bash: npm: command not found
1- How come I need to source this script . ~/.nvm/nvm.sh; on each command?
2- Why is my Node package manager not found in the bash shell?
EDIT: I changed a bit the content of the Dockerfile file:
RUN curl -o-https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash \
&& . ~/.nvm/nvm.sh \
&& nvm install stable \
&& nvm alias default stable \
&& nvm use default
And building it now shows this:
Step 4 :RUN curl -o https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash && . ~/.nvm/nvm.sh && nvm install stable && nvm alias default stable && nvm use default
---> Running in 7d2c404135dd
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 10250 100 10250 0 0 18258 0 --:--:-- --:--:-- --:--:-- 18238
=> Downloading nvm as script to '/root/.nvm'
=> Appending source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="/root/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v7.2.0 (npm v3.10.9)
Creating default alias: default -> stable (-> v7.2.0 *)
default -> stable (-> v7.2.0 *)
Now using node v7.2.0 (npm v3.10.9)
---> ad960a4addbe
Removing intermediate container 7d2c404135dd
Step 5 : EXPOSE 9001
---> Running in df9284421302
---> 14d386f009fb
Removing intermediate container df9284421302
Step 6 : ENTRYPOINT /usr/bin/tail -f /dev/null
---> Running in fa2d71b6dfdf
---> d02c8e88eb7f
Removing intermediate container fa2d71b6dfdf
Successfully built d02c8e88eb7f
I can see it installed node v7.2.0 and is using it.
But when I log into the container with the command docker exec -it nodejs bash it does not see any node anywhere:
root#f8f2a32b462a:/# nvm --version
0.32.1
root#f8f2a32b462a:/# npm --version
bash: npm: command not found
root#f8f2a32b462a:/# echo $NVM_DIR
/root/.nvm
root#f8f2a32b462a:/# ls -l /root/.nvm
total 100
-rwxr-xr-x 1 root root 313 Nov 26 13:01 nvm-exec
-rw-r--r-- 1 root root 95660 Nov 26 13:01 nvm.sh
root#f8f2a32b462a:/# ls -l /root/.npm
ls: cannot access /root/.npm: No such file or directory
I changed the way I install Node and did it without the nvm tool:
RUN curl -sL https://deb.nodesource.com/setup_7.x | bash
RUN apt-get install -y nodejs
Now, when logging in the container, it can find the Node executable:
$ docker run -d -p 127.0.0.1:9001:9001 --name nodejs stephaneeybert/nodejs
f3a2f054934ef92a9b05486b6f6dbe53abd4390826c06d1b7a490d671d8e3422
[stephane#stephane-ThinkPad-X301 nodejs]
$ docker exec -it nodejs bash
root#f3a2f054934e:/# npm --version
3.10.9
Maybe, when I was using the nvm tool, I should have sourced the npm command in the client shell . ~/.nvm/nvm.sh npm --version.
For amazonlinux (Ubuntu Fedora) you can install it using yum like this inside the Dockerfile:
RUN curl -sL https://rpm.nodesource.com/setup_10.x | bash # for node version 10.x
RUN yum -y install nodejs
RUN node --version # optional to check that it worked
RUN npm --version # optional to check that it worked
Use docker build -t [name] [local_folder_like_dot]
Took me ages to google, don't know much about linux but it seems like every version is so different from the other. It's like google docker npm, use apt get nah doesn't work, figure out wth linux version amasonlinux is.. Ok called Fedora.. it has yum instead of apt.. ok .. you need to answer yes in the yum install, docker doesn't allow it just skips it then.. wth.. I can hackz0r it with | yes nah that just spams y forevah.. ok I canz killall nah not in Fedora.. okiz can yum installz without questionz askz??! -y
2h later and we have this :)

Docker nodejs not found

When i run docker build -t example . on the below im getting an error
FROM ruby:2.1
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 4.4.2
RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash \
&& source $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/v$NODE_VERSION/bin:$PATH
RUN node -v
I get the following error:
Step 9 : RUN node -v ---> Running in 6e3fac36d2fc /bin/sh: node:
command not found The command '/bin/sh -c node -v' returned a non-zero
code: 127
Can't understand why node is not found in the path. i tried executing the nvm.sh file as well but it didnt have an effect.
Node version manager is an excellent application for switching versions of Node.js on your development machine, but Docker begs a specific kind of image/container design that is meant to be both ephemeral and stripped down to the bare essentials in order to support the "best practice" of microservices. Docker is just a fancy way to run a process, not a full VM. That last sentence has helped me a lot in how to think about Docker. And so here, you can make things easier on yourself by creating different versions of your image, instead of making one container with many versions of Node.js inside of it. This way, you can reference the Node version you want to run inside of your docker run command instead of trying to feed in environment variables trying to get NVM to select the right version. For example:
docker build -t=jamescharlesworth-node:4.x-latest .
And of course your Dockerfile will have in it the install command in your RUN directive that you mention in the comments:
RUN curl -sL https://deb.nodesource.com/setup_4.x | bash -
RUN apt-get install -y nodejs

Resources