Skip to content

Commit

Permalink
优化镜像打包层数 去掉默认的时区设置
Browse files Browse the repository at this point in the history
  • Loading branch information
Samiya committed Mar 8, 2024
1 parent e9beff2 commit 066beca
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
15 changes: 4 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ FROM python:3.10 AS builder
# 合并命令以减少层数,并安装时区数据,同时清理缓存和不再需要的文件
RUN apt-get update && \
apt-get install -y --no-install-recommends tzdata && \
ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo Asia/Shanghai > /etc/timezone && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

Expand All @@ -21,16 +19,15 @@ RUN pip install --no-cache-dir -r requirements.txt --target /root/dependencies
# 将代码复制到容器内
COPY . .

# 确保启动脚本具有执行权限
RUN chmod +x /root/VintageVigil/start.sh

# 阶段二:运行阶段
FROM python:3.10-slim AS runner

# 复制时区设置
COPY --from=builder /etc/localtime /etc/localtime
COPY --from=builder /etc/timezone /etc/timezone

# 安装运行时依赖,并清理APT缓存
RUN apt-get update && \
apt-get install -y --no-install-recommends jq curl && \
apt-get install -y --no-install-recommends jq curl tzdata && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

Expand All @@ -43,10 +40,6 @@ COPY --from=builder /root/dependencies /usr/local/lib/python3.10/site-packages
# 从构建阶段复制项目文件
COPY --from=builder /root/VintageVigil .

# 确保启动脚本具有执行权限
RUN chmod +x /root/VintageVigil/start.sh


# 设置容器启动时执行的命令(根据你的需要选择一个)
# 自动监控
# CMD ["bash", "./run_checker.sh"]
Expand Down
12 changes: 4 additions & 8 deletions Dockerfile-Alpine
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ WORKDIR /root/VintageVigil
# 安装时区数据和依赖,同时清理缓存
RUN apk update && \
apk add --no-cache tzdata build-base libffi-dev openssl-dev && \
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone && \
rm -rf /var/cache/apk/*

# 将依赖文件单独复制,以利用 Docker 缓存
Expand All @@ -21,26 +19,24 @@ RUN pip install --no-cache-dir -r requirements.txt --target /root/dependencies &
# 将代码复制到容器内
COPY . .

# 确保启动脚本具有执行权限
RUN chmod +x /root/VintageVigil/start.sh

# 阶段二:运行阶段
FROM python:3.10-alpine AS runner

# 复制时区设置和已安装的依赖
COPY --from=builder /etc/localtime /etc/localtime
COPY --from=builder /etc/timezone /etc/timezone
COPY --from=builder /root/dependencies /usr/local/lib/python3.10/site-packages

# 安装运行时依赖
RUN apk add --no-cache jq curl
RUN apk add --no-cache jq curl tzdata

# 设置工作目录
WORKDIR /root/VintageVigil

# 从构建阶段复制项目文件
COPY --from=builder /root/VintageVigil .

# 确保启动脚本具有执行权限
RUN chmod +x /root/VintageVigil/start.sh

# 设置容器启动时执行的命令
# CMD ["python", "./main.py"]
CMD ["sh", "-c", "./start.sh"]

0 comments on commit 066beca

Please sign in to comment.