CI Job with Node + Docker Compose - node.js

I have a GitHub Workflow which I also want to run on GitLab CI:
name: Node.js CI
on:
push:
branches:
- '*'
pull_request:
branches:
- master
jobs:
lint:
runs-on: ubuntu-18.04
name: Lint
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: '12'
- run: yarn
- run: yarn lint
build:
runs-on: ubuntu-18.04
name: Build + Test
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: '12'
- run: yarn
- run: yarn build
- run: yarn test
I tried to setup a new job using the Docker executor template:
docker-build:
# Use the official docker image.
image: docker:latest
stage: build
services:
- docker:dind
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
# Default branch leaves tag empty (= latest tag)
# All other branches are tagged with the escaped branch name (commit ref slug)
script:
- |
if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
tag=""
echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'"
else
tag=":$CI_COMMIT_REF_SLUG"
echo "Running on branch '$CI_COMMIT_BRANCH': tag = $tag"
fi
- docker build --pull -t "$CI_REGISTRY_IMAGE${tag}" .
- docker push "$CI_REGISTRY_IMAGE${tag}"
# Run this job in a branch where a Dockerfile exists
rules:
- if: $CI_COMMIT_BRANCH
exists:
- Dockerfile
I modified it to install node:12 and docker-compose but it doesn't work and it feels wrong doing this. Is there an easier/correct way of running Node.js, Docker and docker-compose in the GitLab CI job?
That's what I tried but as said, it fails:
docker-build:
# Use the official docker image.
image: docker:latest
stage: build
services:
- docker:dind
before_script:
- docker --version
- echo "shell" $0
- uname -a
- dnf -y install curl
- curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | sh
- export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
- nvm install 12
- nvm use 12
- curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- chmod +x /usr/local/bin/docker-compose
- docker-compose --version
# Default branch leaves tag empty (= latest tag)
# All other branches are tagged with the escaped branch name (commit ref slug)
script:
- echo "hello world"
Result:
Pulling docker image docker:latest ...
Using docker image sha256:08bdaf2f88f90320cd3e92a469969efb1f066c6d318631f94e7864828abd7c75 for docker:latest with digest docker#sha256:a61102937d2bda8319882998ef1ffa27387617f6eea6c298b18a05f7fba82c0d ...
Preparing environment 00:01
Running on runner-ed2dce3a-project-27995513-concurrent-0 via runner-ed2dce3a-srm-1625761937-d755bac1...
Getting source from Git repository 00:02
$ eval "$CI_PRE_CLONE_SCRIPT"
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/.../docker-compose/.git/
Created fresh repository.
Checking out 659d60d0 as test-gitlab-ci...
Skipping Git submodules setup
Executing "step_script" stage of the job script 00:00
Using docker image sha256:08bdaf2f88f90320cd3e92a469969efb1f066c6d318631f94e7864828abd7c75 for docker:latest with digest docker#sha256:a61102937d2bda8319882998ef1ffa27387617f6eea6c298b18a05f7fba82c0d ...
$ docker --version
Docker version 20.10.7, build f0df350
$ echo "shell" $0
shell /bin/sh
$ uname -a
Linux runner-ed2dce3a-project-27995513-concurrent-0 4.19.78-coreos #1 SMP Mon Oct 14 22:56:39 -00 2019 x86_64 Linux
$ dnf -y install curl
/bin/sh: eval: line 119: dnf: not found
Update: the answer by Aritra got me a bit further but it's still not working:
Using docker image sha256:08bdaf2f88f90320cd3e92a469969efb1f066c6d318631f94e7864828abd7c75 for docker:latest with digest docker#sha256:a61102937d2bda8319882998ef1ffa27387617f6eea6c298b18a05f7fba82c0d ...
$ docker --version
Docker version 20.10.7, build f0df350
$ echo "shell" $0
shell /bin/sh
$ uname -a
Linux runner-0277ea0f-project-27995513-concurrent-0 4.19.78-coreos #1 SMP Mon Oct 14 22:56:39 -00 2019 x86_64 Linux
$ apk add curl
fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/community/x86_64/APKINDEX.tar.gz
(1/4) Installing brotli-libs (1.0.9-r3)
(2/4) Installing nghttp2-libs (1.42.0-r1)
(3/4) Installing libcurl (7.77.0-r1)
(4/4) Installing curl (7.77.0-r1)
Executing busybox-1.32.1-r6.trigger
OK: 13 MiB in 24 packages
$ apk add -U curl bash ca-certificates openssl ncurses coreutils python2 make gcc g++ libgcc linux-headers grep util-linux binutils findutils
(1/53) Upgrading musl (1.2.2-r0 -> 1.2.2-r1)
(2/53) Installing readline (8.1.0-r0)
(3/53) Installing bash (5.1.0-r0)
Executing bash-5.1.0-r0.post-install
(4/53) Installing libgcc (10.2.1_pre1-r3)
(5/53) Installing libstdc++ (10.2.1_pre1-r3)
(6/53) Installing binutils (2.35.2-r1)
(7/53) Installing libacl (2.2.53-r0)
(8/53) Installing libattr (2.4.48-r0)
(9/53) Installing skalibs (2.10.0.0-r0)
(10/53) Installing s6-ipcserver (2.10.0.0-r0)
(11/53) Installing utmps (0.1.0.0-r0)
Executing utmps-0.1.0.0-r0.pre-install
(12/53) Installing coreutils (8.32-r2)
(13/53) Installing findutils (4.8.0-r0)
(14/53) Installing libgomp (10.2.1_pre1-r3)
(15/53) Installing libatomic (10.2.1_pre1-r3)
(16/53) Installing libgphobos (10.2.1_pre1-r3)
(17/53) Installing gmp (6.2.1-r0)
(18/53) Installing isl22 (0.22-r0)
(19/53) Installing mpfr4 (4.1.0-r0)
(20/53) Installing mpc1 (1.2.0-r0)
(21/53) Installing gcc (10.2.1_pre1-r3)
(22/53) Installing musl-dev (1.2.2-r1)
(23/53) Installing libc-dev (0.7.2-r3)
(24/53) Installing g++ (10.2.1_pre1-r3)
(25/53) Installing pcre (8.44-r0)
(26/53) Installing grep (3.6-r0)
(27/53) Installing linux-headers (5.7.8-r0)
(28/53) Installing make (4.3-r0)
(29/53) Installing ncurses (6.2_p20210109-r0)
(30/53) Installing openssl (1.1.1k-r0)
(31/53) Installing libbz2 (1.0.8-r1)
(32/53) Installing expat (2.2.10-r1)
(33/53) Installing libffi (3.3-r2)
(34/53) Installing gdbm (1.19-r0)
(35/53) Installing sqlite-libs (3.34.1-r0)
(36/53) Installing python2 (2.7.18-r1)
(37/53) Installing libblkid (2.36.1-r1)
(38/53) Installing blkid (2.36.1-r1)
(39/53) Installing libcap-ng (0.8.2-r0)
(40/53) Installing setpriv (2.36.1-r1)
(41/53) Installing libmount (2.36.1-r1)
(42/53) Installing libsmartcols (2.36.1-r1)
(43/53) Installing findmnt (2.36.1-r1)
(44/53) Installing mcookie (2.36.1-r1)
(45/53) Installing hexdump (2.36.1-r1)
(46/53) Installing lsblk (2.36.1-r1)
(47/53) Installing libuuid (2.36.1-r1)
(48/53) Installing libfdisk (2.36.1-r1)
(49/53) Installing sfdisk (2.36.1-r1)
(50/53) Installing cfdisk (2.36.1-r1)
(51/53) Installing partx (2.36.1-r1)
(52/53) Installing libeconf (0.3.8-r0)
(53/53) Installing util-linux (2.36.1-r1)
Executing busybox-1.32.1-r6.trigger
OK: 253 MiB in 76 packages
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 14926 100 14926 0 0 182k 0 --:--:-- --:--:-- --:--:-- 184k
=> Downloading nvm as script to '/root/.nvm'
=> Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
OR
=> Append the following lines to the correct file yourself:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
$ export NVM_DIR="$HOME/.nvm"
$ . "$NVM_DIR/nvm.sh"
$ nvm install 12 -s
Downloading and installing node v12.22.3...
Downloading https://nodejs.org/dist/v12.22.3/node-v12.22.3-linux-x64.tar.gz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.22.3 (npm v)
Creating default alias: default -> 12 (-> v12.22.3 *)
$ nvm use 12
Now using node v12.22.3 (npm v)
$ which node
/root/.nvm/versions/node/v12.22.3/bin/node
$ node --version
/bin/sh: eval: line 135: node: not found

dnf is fedora specific. It's RPM package manager.
if you see the Dockerfile of docker:latest you can see that it's using alpine.
FROM alpine:3.13
RUN apk add --no-cache \
ca-certificates \
# DOCKER_HOST=ssh://... -- https://github.com/docker/cli/pull/1014
openssh-client
# set up nsswitch.conf for Go's "netgo" implementation (which Docker explicitly uses)
# - https://github.com/docker/docker-ce/blob/v17.09.0-ce/components/engine/hack/make.sh#L149
# - https://github.com/golang/go/blob/go1.9.1/src/net/conf.go#L194-L275
# - docker run --rm debian:stretch grep '^hosts:' /etc/nsswitch.conf
RUN [ ! -e /etc/nsswitch.conf ] && echo 'hosts: files dns' > /etc/nsswitch.conf
ENV DOCKER_VERSION 20.10.7
So instead of dnf you would need to do:
apk add curl

Related

Alpine and docker:20.10.22 unable to install pip3

I am upgrading some images from to docker:19.03.5 to docker:20.10.22 but am hitting issues when my Push Dev job tries to install pip3 on the image being used by .gitlab-ci.yml.
Initially I was not explicitly installing pip3 as this worked fine, after an initial error saying pip3 was not installed I tried installing using apk as below.
before_script:
- apk add python3
- apk add python3-pip
- pip3 install awscli==1.18.8
- docker load --input data/image.tar
- $(aws ecr get-login --no-include-email --region us-east-1)
Push Dev:
stage: Push
script:
- docker tag proxy:latest $ECR_REPO:dev
- docker push $ECR_REPO:dev
rules:
- if: "$CI_COMMIT_BRANCH == 'main'"
However I still get this issue. Any idea what is wrong here?
$ apk add python3
fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/community/x86_64/APKINDEX.tar.gz
(1/11) Installing libbz2 (1.0.8-r4)
(2/11) Installing libexpat (2.5.0-r0)
(3/11) Installing libffi (3.4.4-r0)
(4/11) Installing gdbm (1.23-r0)
(5/11) Installing xz-libs (5.2.9-r0)
(6/11) Installing libgcc (12.2.1_git20220924-r4)
(7/11) Installing libstdc++ (12.2.1_git20220924-r4)
(8/11) Installing mpdecimal (2.5.1-r1)
(9/11) Installing readline (8.2.0-r0)
(10/11) Installing sqlite-libs (3.40.1-r0)
(11/11) Installing python3 (3.10.9-r1)
Executing busybox-1.35.0-r29.trigger
OK: 65 MiB in 34 packages
$ apk add python3-pip
ERROR: unable to select packages:
python3-pip (no such package):
required by: world[python3-pip]
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 1
As per the comment made by β.εηοιτ.βε, I changed the name of the pip3 package to py3-pip to be in line with the expected package name in alpine instead of python3-pip which is Debian.
before_script:
- apk add python3
- apk add py3-pip
- pip3 install awscli==1.18.8
- docker load --input data/image.tar
- $(aws ecr get-login --no-include-email --region us-east-1)
https://pkgs.alpinelinux.org/package/v3.17/community/armv7/py3-pip

alpine linux apk installation error in Dockerfile

I am trying to execute the following code in a Dockerfile:
FROM alpine:3.7
RUN apk update && apk add --no-cache busybox musl prometheus-node-exporter
RUN rc-update add prometheus-node-exporter default
RUN rc-service prometheus-node-exporter start
RUN echo " - job_name: node \
# If prometheus-node-exporter is installed, grab stats about the local \
# machine by default. \
static_configs: \
- targets: ['localhost:9100', 'alpine_distro:9100']" >> /etc/prometheus/prometheus.yml
but my build returns the following error:
ERROR:
unsatisfiable constraints:
prometheus-node-exporter (missing):
required by: world[prometheus-node-exporter]
The command '/bin/sh -c apk update && apk add --no-cache busybox musl prometheus-node-exporter' returned a non-zero code: 1
I am not used to Alpine package manager but my guess is that I might not be using a repo that contains prometheus-node-exporter?
Or am I missing something else?
If from some reason you're forced to use Alpine 3.7, you could install the prometheus-node-exporter package from the Alpine 3.13 community repo:
$ sudo docker run -it alpine:3.7
/ # apk add prometheus-node-exporter --repository=http://dl-cdn.alpinelinux.org/alpine/v3.13/community
fetch http://dl-cdn.alpinelinux.org/alpine/v3.13/community/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
(1/1) Installing prometheus-node-exporter (1.0.1-r0)
Executing prometheus-node-exporter-1.0.1-r0.pre-install
24% ###################################
Executing busybox-1.27.2-r11.trigger
OK: 20 MiB in 14 packages
/ #
/ #
/ # node_exporter --help
usage: node_exporter [<flags>]
...
It seems node_exporter only depends on musl, so it's likely to work well without special issues:
/ # ldd /usr/bin/node_exporter
/lib/ld-musl-x86_64.so.1 (0x7fea4c68a000)
libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7fea4c68a000)
However, if you have that option, it's recommended to upgrade your base Alpine version to 3.13, instead.

CircleCI forgetting node version on machine executor

I'm setting the node version to 10.15.1 with nvm and in the next run step it's back to 6.1.0. I've tried several variations including this one : https://www.cloudesire.com/how-to-upgrade-node-on-circleci-machine-executor/
Am I missing something obvious? I just need each run step to remember the node version I set in the first one so they will all use 10.15.1 in this case.
Here is the job in my workflow:
dev:
environment:
BASH_ENV: run/env/test/.env
machine:
image: circleci/classic:latest
steps:
- checkout
- run:
name: Install node#10
command: |
set +e
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
export NVM_DIR="/opt/circleci/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install 10
nvm alias default 10
rm -rf ~./node-gyp
node -v # prints 10.15.1 as expected
- run:
name: Install yarn and rsync
command: |
node -v # prints 6.1.0
export NVM_DIR="/opt/circleci/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
node -v # prints 6.1.0
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn rsync
- run:
name: Install node modules
command: |
node -v # prints 6.1.0
yarn install # this is what is failing because of the unexpected node version
- run:
name: Deploy to Dev Server
command: |
if [ "${CIRCLE_BRANCH}" == "master" ]; then rsync -arhvz --exclude .git/ -e "ssh -o StrictHostKeyChecking=no" --progress \
./ ubuntu#xxx.xxx.xxx.xxx:/var/www/xxx/xxx/; fi
if [ "${CIRCLE_BRANCH}" == "master" ]; then ssh -o StrictHostKeyChecking=no ubuntu#xxx.xxx.xxx.xxx 'cd /var/www/xxx/xxx && pm2 restart all --update-env'; fi
#FelicianoTech is incorrect, every single build step runs in a fresh environment and forgets your nvm settings. You have to set NVM_DIR and source the nvm setup script in every single build step. Annoying, I know.
Despite that FelicianoTech is correct, that in the machine executor you have nvm preinstalled, it does not remember the actual state in the next run command. In order to make nvm remember its state you have to store it in the $BASH_ENV. Searching hours long, I finally found an answer in the CircleCI Disussion Board:
machine:
image: circleci/classic:latest
steps:
- checkout
- run:
command: |
echo 'export NVM_DIR="/opt/circleci/.nvm"' >> $BASH_ENV
echo ' [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> $BASH_ENV
- run: nvm install 10
- run: node -v
- run: npm -v
- run: npm i -g npm#6.9.0
- run: npm -v
- run: nvm alias default 10
- run: nvm use 10
- run: node -v
- run: npm -v
Then, and only then, I had success as well that even the last run command - run: node -v still remembers the node version properly.
The output on CircleCI
(Credits to phil-lgr on the discussion board)
Solved by adding installed node to $PATH
Example:
- run:
name: 'Install Project Node'
command: |
set +x
source ~/.bashrc
nvm install 12
NODE_DIR=$(dirname $(which node))
echo "export PATH=$NODE_DIR:\$PATH" >> $BASH_ENV
You're doing way too much here. All you need to do is run nvm install v10. nvm is already installed in the machine executor.

Cannot get GitLab CI to build my Middleman/Gulp project

I am using Gitlab-CI to build my Middleman application which also uses some node stuff for the front end (Gulp).
Here is my .gitlab-ci.yml (mostly copied from here):
image: ruby:2.3
cache:
paths:
- vendor
- node_modules
before_script:
- apt-get update -yqqq
- apt-get install -y npm
- ln -s /usr/bin/nodejs /usr/bin/node
- npm install
- bundle install --path vendor
test:
script:
- bundle exec middleman build
except:
- master
pages:
script:
- bundle exec middleman build
artifacts:
paths:
- public
only:
- master
Everything goes alright apart from the vital problem that it seems to be using an old version of node when it's npm installing. I'm getting lots of this:
npm WARN engine gulp-babel#7.0.0: wanted: {"node":">=4"} (current: {"node":"0.10.29","npm":"1.4.21"})
Before finally failing on the "const path" SyntaxError.
I included a line to symlink the new nodejs with the old name (- ln -s /usr/bin/nodejs /usr/bin/node) but it seems to have no effect...?
Been banging my head for long enough, there's got to be someone out there who has made this work?
Debian Jessie ships with a fixed NodeJs major version, follow NodeSource instructions to install a specific version, this would fit in your gitlab-ci.yml like this (you probably need to install curl first since its not installed in the ruby:2.3 image):
before_script:
- apt-get update -q && apt-get -qqy install curl
- curl -sL https://deb.nodesource.com/setup_9.x | bash -
- apt-get update -q && apt-get -qqy install nodejs npm
- ln -s /usr/bin/nodejs /usr/bin/node
- npm install
- bundle install --path vendor

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

Resources