自定义镜像

428 阅读2分钟

自定义镜像

1.自定义Dockerfile文件:
#Dockerfile

FROM harbor102.test.ximalaya.com/common/phusion-nile

# 安装node14.15.0版本
RUN wget -c https://nodejs.org/download/release/v14.15.0/node-v14.15.0-linux-x64.tar.gz \
    && tar -zxvf node-v14.15.0-linux-x64.tar.gz \
    && mv node-v14.15.0-linux-x64 nodejs \
    && ln -s /nodejs/bin/node /usr/local/bin/node \
    && ln -s /nodejs/bin/npm  /usr/local/bin/npm \
    && rm -rf node-v14.15.0-linux-x64.tar.gz

# 配置node环境变量
RUN echo 'export NODE_HOME=/nodejs/bin' >> /etc/profile \
    && echo 'export PATH=$NODE_HOME:$PATH' >> /etc/profile

RUN npm install nrm -g --registry=https://registry.npm.taobao.org \
    && NODE_HOME=/nodejs/bin PATH=$NODE_HOME:$PATH nrm add xnpm http://xnpm.ximalaya.com \
    && NODE_HOME=/nodejs/bin PATH=$NODE_HOME:$PATH nrm use taobao \
    && npm install yarn -g \
    && npm i -g npm \
    && npm install pm2@3.2.2 -g \
    && NODE_HOME=/nodejs/bin PATH=$NODE_HOME:$PATH nrm use xnpm

RUN mkdir /etc/service/node
COPY run /etc/service/node/
RUN  chmod +x /etc/service/node/run

# Disable apt-key warning
ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=true
# Specify executable path to be used in puppeteer.launch
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/google-chrome-stable
# Tell Puppeteer to skip installing Chrome. We'll be using the installed package.
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true

# Copy signiture key
COPY linux_signing_key.pub .

# installs, work.
RUN apt-key add linux_signing_key.pub \
  && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
  && apt-get update \
  && apt-get install -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf \
  --no-install-recommends \
  && rm -rf /var/lib/apt/lists/*
2.自定义run文件:
#run

#!/bin/sh
export HOME=/root
if [ ! -f "/etc/service/node/started" ];then
    touch /etc/service/node/started
    ## ecosystem.config.js use relative path, so cd /code
    cd /code
    /nodejs/bin/pm2-runtime --version
    exec 2>&1
    exec /nodejs/bin/pm2-runtime start /code/ecosystem.config.js > /root/.pm2/pm2.log 2>&1
else
    exec sv d node
fi
3.构建镜像

在Dockerfile和run文件的当前目录下

docker build ./ -t ubuntu-node14-express-puppeteer:v0.0.1

给新构建的镜像取名为 ubuntu-node14-express-puppeteer, 并设定版本为 v0.0.1

使用 docker images 查看是否构建成功

4.向Harbor仓库中推镜像

首先登录Harbor仓库

docker login https://harbor102.test.ximalaya.com

登录成功之后,我们需要将刚刚制作的tomcat推到Harbor仓库中 先打一个tag,然后再推上去,两条命令即可

docker tag ubuntu-node14-express-puppeteer:v0.0.1 harbor102.test.ximalaya.com/common/ubuntu-node14-express-puppeteer:v0.0.1

harbor102.test.ximalaya.com是Harbor仓库地址,common是Harbor仓库下的项目名称,ubuntu-node14-express-puppeteer:v0.0.1是推到项目上,该镜像的镜像名

docker push harbor102.test.ximalaya.com/common/ubuntu-node14-express-puppeteer:v0.0.1

然后在Harbor仓库中能够看到镜像

image-20210510193000515.png

5.在cmdb-docker中新建镜像

仓库地址:/common/ubuntu-node14-express-puppeteer:v0.0.1

dockerfile:

FROM {{.From}}
## copy code11
RUN mkdir -p /code
ADD {{.Code}} /code/

dockerignore:

.git
.idea
.vscode
6.拉取镜像

docker pull harbor102.test.ximalaya.com/common/ubuntu-node14-express-puppeteer:v0.0.1