目标:
- 镜像体积要小一些。但要满足应用需求,和少量调试需求
- 推送镜像到仓库的速度要快。npm install 利用docker layer
实现目标1:
- 选择镜像底
查看node docker hub文档,推荐slim,因此选了node:10-slim, 包含了bash 、curl - 启动应用报错,node-gyp依赖make、gcc、g++、python
- apt-get更新慢,改用清华源
最终dockerfile
FROM node:10-slim
MAINTAINER 252228148@qq.com
ENV TZ "Asia/Shanghai"
COPY ./sources.list /etc/apt/
# node-gyp require python
RUN set -ex \
&& apt-get update \
&& apt-get install -y make gcc g++ python \
&& rm -rf /var/lib/apt/lists/*
sourcelist
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.tuna.tsinghua.edu.cn/debian/ stretch main contrib non-free
# deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ stretch main contrib non-free
deb http://mirrors.tuna.tsinghua.edu.cn/debian/ stretch-updates main contrib non-free
# deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ stretch-updates main contrib non-free
deb http://mirrors.tuna.tsinghua.edu.cn/debian/ stretch-backports main contrib non-free
# deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ stretch-backports main contrib non-free
deb http://mirrors.tuna.tsinghua.edu.cn/debian-security stretch/updates main contrib non-free
# deb-src http://mirrors.tuna.tsinghua.edu.cn/debian-security stretch/updates main contrib non-free
实现目标2
增加.dockerignore,屏蔽掉用不到的目录和文件
node_modules/*
logs/*
.idea/*
.git/*
也参考了[lvwxx] 同学的文章 https://juejin.im/post/5cada976f265da035e210bf8
最终dockerfile
FROM docker.io/pianfeng240/node10 AS builder
WORKDIR /home/app
COPY package.json package-lock.json ./
COPY .npmrc /root
RUN npm install --production
FROM docker.io/pianfeng240/node10
WORKDIR /home/app
COPY --from=builder /home/app/node_modules ./node_modules
COPY . .
EXPOSE 80
CMD ["node", "bin/server"]
验证:
修改后推送镜像到腾讯云的速度大大提高了
网友评论