docker下安装nodejs环境

# 安装

# centos7

centos7基础镜像;

# 源码安装

yum install -y gcc-c++ make
wget http://nodejs.org/dist/v10.24.0/node-v10.24.0.tar.gz
tar zxvf node-v10.24.0.tar.gz
mv node-v10.24.0 node
#或者git上拉去源码;
git clone -b v10.24.0 git@github.com:nodejs/node.git

#修改目录权限
chmod -R 755 node

#编译安装node
cd node
./configure
make
make install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

简化版本:

cd /usr/local/src
wget https://nodejs.org/dist/v10.15.3/node-v10.15.3-linux-x64.tar.xz
tar -jxf node-v10.15.3-linux-x64.tar.xz
mv node-v10.15.3-linux-x64 /usr/local/bin/node

vi /etc/profile
export NODE_HOME=/usr/local/bin/node
export NODE_PATH=/usr/local/bin/node/lib/node_modules
export PATH=$PATH:$NODE_HOME/bin:$NODE_PATH
source /etc/profile

node -v
npm -v
1
2
3
4
5
6
7
8
9
10
11
12
13

usr/local处理:【推荐】

cd /usr/local/
#tar -xvf node-v12.16.0-linux-x64.tar.xz
#mv /var/ftp/pub/node-v12.16.0-linux-64 . //后面的.表示移动到当前目录
#mv node-v10.16.0.0-linux-64/ nodejs
wget https://nodejs.org/dist/v10.15.3/node-v10.15.3-linux-x64.tar.xz
tar -jxf node-v10.15.3-linux-x64.tar.xz
mv node-v10.15.3-linux-x64 nodejs
mv nodejs /usr/local/nodejs

#方式一:环境变量方式
vi /ect/profile
export NODE_HOME=/usr/local/nodejs/bin
export NODE_PATH=/usr/local/nodejs/lib/node_modules
export PATH=$PATH:$NODE_HOME/bin:$NODE_PATH
source /etc/profile

#方式二:软链接方式(推荐)
#ln -s /usr/local/nodejs/bin/npm /usr/local/bin/
#ln -s /usr/local/nodejs/bin/node /usr/local/bin/
#ln -s /usr/local/nodejs/bin/npx /usr/local/bin/

ln -s /usr/local/nodejs/bin/npm /usr/local/bin/npm
ln -s /usr/local/nodejs/bin/node /usr/local/bin/node
ln -s /usr/local/nodejs/bin/npx /usr/local/bin/

#查看nodejs是否安装成功
node -v
npm -v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

最终使用

wget https://nodejs.org/dist/v10.24.0/node-v10.24.0-linux-x64.tar.gz
tar xvf node-v10.24.0-linux-x64.tar.gz
mv node-v10.24.0-linux-x64 nodejs
mv nodejs /usr/local/nodejs

#方式一:环境变量方式
vi /ect/profile
export PATH=$PATH:/usr/local/nodejs/bin
source /etc/profile
#方式二:软链接方式(推荐)
ln -s /usr/local/nodejs/bin/npm /usr/local/bin/
ln -s /usr/local/nodejs/bin/node /usr/local/bin/
ln -s /usr/local/nodejs/bin/npx /usr/local/bin/

#查看nodejs是否安装成功
node -v
npm -v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

安装bower/pm2

#全局安装 bower
npm install bower -g
#查看是否安装成功
bower -v
#输出bower的版本号即证明安装成功。

#【要点】如果提示command not found: bower,是因为当系统接受到 bower 命令后到 /usr/local/bin 目录中查找 bower 服务,没有找到,于是就报找不到命令。解决办法如下:

#在安装 bower 前,将 npm 的 config 中的 prefix 设置为 /usr/local/ 目录后再安装 bower,即将 bower 安装在 /usr/local/bin 中,安装后再启动时系统就可以找到 bower 了

npm config set prefix /usr/local
npm install -g bower pm2

#最后,验证是否安装成功
bower -v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# rpm安装【推荐】

FROM centos:centos7
LABEL author=samyzh version=0.1.0

RUN curl --silent --location https://rpm.nodesource.com/setup_12.x | bash - \
  && yum install -y nodejs
 
CMD [ "/bin/bash" ]
1
2
3
4
5
6
7

构建 build.sh

#!/bin/bash
PRO_NAME=centos-web
docker build -t $PRO_NAME .
1
2
3

运行 start.sh

docker run --privileged --name centos-web -itd centos-web /usr/sbin/init

docker exec -it centos-web /bin/bash
1
2
3

# nvm多版本

# 依赖centos环境
FROM centos:latest

#安装vim、git和nvm环境
RUN yum -y update install vim git \
	&& cd ~/ \
	&& git clone https://github.com/nvm-sh/nvm.git .nvm \
#	&& cd ~/.nvm && git checkout v0.35.3 \
	&& echo "source ~/.nvm/nvm.sh" >> ~/.bashrc && source ~/.bashrc
1
2
3
4
5
6
7
8
9

# 自带容器

docker pull node:latest
docker run -itd --name node-test node

docker exec -it node-test /bin/bash
node -v
1
2
3
4
5
# 进入容器
docker exec -it centos_git_nvm_node bash
# 查看可用node版本
nvm list-remote
# 安装node,以安装v14.1.0为例
nvm install v14.1.0
1
2
3
4
5
6

# ubuntu

# 源码安装

FROM ubuntu:16.04
apt-get update
apt-get install git wget python gcc make g++

wget http://nodejs.org/dist/v10.24.0/node-v10.24.0.tar.gz
tar zxvf node-v10.24.0.tar.gz
mv node-v10.24.0 node
#或者git上拉去源码;
git clone -b v10.24.0 git@github.com:nodejs/node.git

#修改目录权限
chmod -R 755 node

#编译安装node
cd node
./configure
make
make install

#查看node版本
node -v
npm -v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# rpm安装【推荐】

FROM ubuntu:16.04
MAINTAINER samy
LABEL "version"="0.0.1"
RUN apt-get update \
 && apt-get install curl
 && curl -sL https://deb.nodesource.com/setup_8.x | bash -
 && apt-get install -y nodejs
1
2
3
4
5
6
7

直接打包镜像;也可以在里面操作后,再体检成自己的镜像;

$ docker commit -a "author" -m "commit message" b0084b239645 xxx/node8.9:v1
sha256:bc03d86ef63bab18deafe643f99b2aa1da5697860e1432102dbbcbb281fdf335
1
2
  • -a: 作者信息
  • -m: 提交信息
  • b0084b239645: docker ps -a中查看的ID
  • xxx/node8.9:v1: 新的镜像名称

实践中:

FROM ubuntu:16.04
MAINTAINER samy
LABEL "version"="0.0.1"
ENV DEBIAN_FRONTEND noninteractive
#RUN apt-get update
# RUN apt-get install -y software-properties-common
# RUN add-apt-repository -y ppa:libreoffice/ppa
# COPY sources.list /etc/apt/
RUN apt-get update
RUN apt-get install -y sudo
RUN apt-get install -y language-pack-zh-hans
RUN apt-get install -y language-pack-en
RUN apt-get install -y curl
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs
RUN apt-get install -y build-essential
RUN apt-get install -y graphicsmagick
RUN apt-get install -y calibre
RUN apt-get install -y libreoffice 
RUN apt-get install -y python2.7 python2.7-dev make gcc
RUN apt-get install -y git
RUN apt-get install -y pdftk
RUN apt-get install -y poppler-utils
RUN apt-get install -y vim
RUN apt-get install -y unar zip
RUN mkdir -p /usr/src/app/uploads
RUN mkdir -p ~/.config/libreoffice/4/user/fonts
WORKDIR /usr/src/app
ENV LANG en_US.UTF-8
ENV NODE_PATH=/usr/local/lib/node_modules/:/usr/local/lib  NODE_ENV=docker
COPY dist /usr/src/app/
COPY docker-process.json /usr/src/app/
#RUN npm install -g cnpm --registry=https://registry.npm.taobao.org
#RUN cnpm install -g node-gyp
#RUN cnpm install -g pm2 
#RUN cnpm install
RUN npm install -g node-gyp
RUN npm install -g pm2 
RUN npm install
# ONBUILD COPY package.json /usr/src/app/
# ONBUILD RUN cnpm install
# ONBUILD COPY . /usr/src/app
CMD [ "pm2-docker", "docker-process.json" ]
EXPOSE 8080
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

# 设置个Docker容器

Dockerfile

###
# @Author: samy
# @email: yessz#foxmail.com
# @time: 2022-02-12 13:21:47
# @modAuthor: samy
# @modTime: 2022-02-28 13:28:20
# @desc: Dockerfile
# Copyright © 2015~2022 BDP FE
###
FROM centos:centos7
LABEL author=samyzh version=0.1.0

ENV WORKDIR=/srv/app
WORKDIR $WORKDIR

RUN curl -sL https://rpm.nodesource.com/setup_12.x | bash - \
  && yum install -y nodejs zip unzip git wget curl

RUN npm config set registry https://registry.npm.taobao.org/ \
  && npm install -g bower yarn \
  && yarn config set registry http://registry.npm.taobao.org/

CMD [ "/bin/bash" ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

start.sh

#docker stop centos-web && docker rm centos-web

PRO_NAME=centos-web
docker run -it --name $PRO_NAME $PRO_NAME /bin/bash
# docker run -itd --name centos-web centos-web /bin/bash
# docker exec -it centos-web bash
1
2
3
4
5
6
上次更新: 2022/04/15, 05:41:27
×