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 ba8ad9c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 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
16 changes: 6 additions & 10 deletions Dockerfile-Alpine
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# 阶段一:构建阶段
FROM python:3.10-alpine AS builder
FROM python:3.13.0a4-alpine3.18 AS builder

# 设置工作目录
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
FROM python:3.13.0a4-alpine3.18 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"]
1 change: 1 addition & 0 deletions docker-swarm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
# - CHECK_INTERVAL=60
# 是否使用内置的Github Token来进行请求,否则每分钟只能请求60次/单个IP(默认是)
# - USE_TOKEN=true
- TZ=Asia/Shanghai
deploy:
mode: replicated
replicas: 1
Expand Down

0 comments on commit ba8ad9c

Please sign in to comment.