构建基于Alpine的NodeJS镜像

Posted 何宇泽

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了构建基于Alpine的NodeJS镜像相关的知识,希望对你有一定的参考价值。

指定版本

查看Dockerfile

cat Dockerfile

FROM alpine:3.10.2
ENV NODE_VERSION 14.5.0
MAINTAINER heyuze

RUN apk add --no-cache curl gcc g++ python make linux-headers \\
    && curl -fsSLO --compressed "https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION.tar.xz" \\
    && tar -xf "node-v$NODE_VERSION.tar.xz" \\
    && cd "node-v$NODE_VERSION" \\
    && ./configure  \\
    && make -j$(getconf _NPROCESSORS_ONLN) \\
    && make install \\
    && cd .. \\
    && rm -Rf "node-v$NODE_VERSION" \\
    && rm "node-v$NODE_VERSION.tar.xz" 

CMD [ "node" ]

 

使用Dockerfile构建镜像

[root@heyuze ~]# docker build -t alpine-nodejs:v14.5.0 .

查看构建好的镜像

[root@heyuze ~]# docker images|grep alpine-nodejs
harbor.aimmcloud.cn/aimm/alpine-nodejs   v14.5.0             235175ed9068        32 seconds ago      339MB

查看node,npm,pm2版本

[root@heyuze ~]# docker run --rm alpine-nodejs:v14.5.0 node -v
v14.5.0
[root@heyuze ~]# docker run --rm alpine-nodejs:v14.5.0 npm -v
6.14.5
[root@heyuze ~]# docker run --rm alpine-nodejs:v14.5.0 pm2 -v

                        -------------

__/\\\\\\\\\\\\\\\\\\\\\\\\\\____/\\\\\\\\____________/\\\\\\\\____/\\\\\\\\\\\\\\\\\\_____
 _\\/\\\\\\/////////\\\\\\_\\/\\\\\\\\\\\\________/\\\\\\\\\\\\__/\\\\\\///////\\\\\\___
  _\\/\\\\\\_______\\/\\\\\\_\\/\\\\\\//\\\\\\____/\\\\\\//\\\\\\_\\///______\\//\\\\\\__
   _\\/\\\\\\\\\\\\\\\\\\\\\\\\\\/__\\/\\\\\\\\///\\\\\\/\\\\\\/_\\/\\\\\\___________/\\\\\\/___
    _\\/\\\\\\/////////____\\/\\\\\\__\\///\\\\\\/___\\/\\\\\\________/\\\\\\//_____
     _\\/\\\\\\_____________\\/\\\\\\____\\///_____\\/\\\\\\_____/\\\\\\//________
      _\\/\\\\\\_____________\\/\\\\\\_____________\\/\\\\\\___/\\\\\\/___________
       _\\/\\\\\\_____________\\/\\\\\\_____________\\/\\\\\\__/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\_
        _\\///______________\\///______________\\///__\\///////////////__


                          Runtime Edition

        PM2 is a Production Process Manager for Node.js applications
                     with a built-in Load Balancer.

                Start and Daemonize any application:
                $ pm2 start app.js

                Load Balance 4 instances of api.js:
                $ pm2 start api.js -i 4

                Monitor in production:
                $ pm2 monitor

                Make pm2 auto-boot at server restart:
                $ pm2 startup

                To go further checkout:
                http://pm2.io/


                        -------------

[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
4.5.6

给镜像打tag

[root@heyuze ~]# docker tag alpine-nodejs:v14.5.0 heyuze/nodejs:v14.5.0

将镜像push到仓库

[root@heyuze ~]# docker push heyuze/nodejs
The push refers to repository [docker.io/heyuze/nodejs]
f5e26fb32437: Pushed
03901b4a2ea8: Pushed
v14.5.0: digest: sha256:25022611bd19acc6cd7f4b8e357ecd55fc27f0bde494d41e272b53c4b3cb9eef size: 741

使用默认版本

查看Dockerfile

cat Dockerfile

FROM alpine:latest
MAINTAINER yuze
RUN apk add nodejs
RUN apk add npm
CMD [ "node" ]

使用Dockerfile构建镜像

root@heyuze:/data/docker# docker build -t alpine-nodejs .

查看构建好的镜像

root@heyuze:/data/docker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine-nodejs       latest              1857723a956b        2 minutes ago       62.9MB

 

 

以上是关于构建基于Alpine的NodeJS镜像的主要内容,如果未能解决你的问题,请参考以下文章

基于alpine镜像构建的步骤

基于alpine镜像构建的步骤

Docker -- 自定义镜像(Dockerfile)基于Ubuntu镜像构建一个新镜像基于java:8-alpine镜像将一个Java项目构建为镜像

alpine jdk 中文乱码

alpine jdk 中文乱码

构建属于自己的 jre Docker 镜像