Skip to content

Docker 相关

centos 安装

设置 yum 阿里云镜像

sh
sudo yum-config-manager \
    --add-repo \
    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

设置 docker 仓库镜像

json
{
  "registry-mirrors": ["https://hub.atomgit.com"]
}

docker build 后镜像太大有 1.1 个 G?

Docker 映像是为了创建一个运行环境,其中包含了应用程序运行所需的一切,包括但不限于 Node.js 运行时、npm 包管理和基础操作系统层。

Dockerfile 有挺多技巧:

  1. 使用 alpine 的镜像,而不是默认的 linux 镜像,可以极大减小镜像体积,比如 node:18-alpine3.14
  2. 使用多阶段构建,比如一个阶段来执行 build,一个阶段把文件复制过去,跑起服务来,最后只保留最后一个阶段的镜像。这样使镜像内只保留运行需要的文件以及 dependencies

部署一个小型 node.js 项目为例:

dockerfile
FROM alpine:3.18 AS base

RUN apk add --no-cache --update nodejs=18.20.1-r0 npm=9.6.6-r0

ENV APP_PATH=/usr/projects/nodejs-service \
  PORT=3000

WORKDIR ${APP_PATH}

FROM base AS install

COPY package.json .

RUN npm i --registry=https://registry.npmmirror.com/ --production

FROM base

COPY --from=install ${APP_PATH}/node_modules ./node_modules
COPY --from=install ${APP_PATH}/package.json .

# FROM install

COPY dist dist

CMD node dist/app.js --port=${PORT}

github 的 actions 构建镜像问题

github 的 actions 构建镜像问题

导出镜像上传至服务器或自定义仓库

Error response from daemon: No command specified

https://www.cnblogs.com/wish123/p/6573899.html

github 的 actions 构建镜像问题

先构建一个已经安装好依赖包的镜像,然后基于此镜像再次构建,相当于多阶段构建,把前两个阶段构建的镜像产物推送到镜像仓库,再以这个镜像为基础去构建后续部分。借助镜像仓库存储基础镜像从而达到缓存的效果

dockerfile
# 以这个镜像为基础去构建,这个镜像是已经装好项目依赖的镜像并推送到镜像仓库里,这里从镜像仓库拉下来
FROM project-base-image:latest

COPY . .

CMD yarn start
  1. 自托管 actions 运行机器
  2. docker 官方提供的 action 缓存方案

https://github.com/appleboy/scp-action/issues/160

Released under the MIT License.