本文内容:将node.js 利用构建成dokcer镜像, 并上传到阿里云
创立 Node.js 利用
1.首先,创立一个 package.json 文件,形容你应用程序以及须要的依赖:
{ "name": "pack_docker", "version": "1.0.0", "description": "Node.js on Docker", "author": "weiweiyi", "main": "pack.js", "dependencies": { "dingtalk-robot-sender": "^1.2.0", "compressing": "^1.7.0", "node-ssh": "13.0.1" }}
2.运行 npm install
这会主动生成一个 package-lock.json 文件,将其拷贝进入接下来的 Docker 镜像中。
3.将你须要运行或者寄存的node.js文件放在本目录下
创立 Dockerfile 文件
创立一个空文件,命名为 Dockerfile:
touch Dockerfile
编写该文件
# 指定node 版本FROM node:14.16.0# 工作目录WORKDIR /root/# 复制文件COPY package*.json ./# 装置依赖RUN npm install# 拷贝应用程序COPY pack.js .# 也能够抉择,间接运行该js文件# CMD [ "node", "pack.js" ]
创立.dockerignore文件 和 .gitignore文件
防止 node_modules 被拷贝到docker image中。
/node_modules
创立docker image
创立命令:
docker build . -t <your username>/pack-docker
查看创立状况docker images
将本目录下文件推送到github仓库
推送镜像到阿里云
参考老师的文章: https://segmentfault.com/a/11...
首先登录阿里云的容器镜像服务,创立仓库
代码源抉择方才上传的github仓库
增加构建规定
点击立刻构建,期待构建胜利
至此,咱们就能够通过该镜像仓库的公网地址应用该镜像
应用
例如在.gitlab-ci.yml中应用
angular-pack: tags: - docker stage: pack-web # 应用镜像 image: registry.cn-hangzhou.aliyuncs.com/weiweiyi/pack-docker:1.0 before_script: - cd web variables: HOST: "xxxx" DINGTOKEN: "xxx" # ssh连贯用户名明码 PASSWORD: "xxx" USERNAME: "xxx" # http端口 PORT: "5080" # ssh端口 SSHPORT: "5022" script: - env - npm install -dd - npm run build - export - node /root/pack.js rules: - if: $CI_COMMIT_TAG != null