Skip to content

Commit

Permalink
fix: dockerfile优化
Browse files Browse the repository at this point in the history
  • Loading branch information
linyuan0213 committed Jan 7, 2025
1 parent c636f75 commit f865e23
Showing 1 changed file with 44 additions and 22 deletions.
66 changes: 44 additions & 22 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
# 使用多阶段构建优化镜像大小
FROM python:3.11-alpine3.19 AS builder

# 减少 COPY 操作的次数
COPY ./package_list.txt /tmp/
COPY ./requirements.txt /tmp/
RUN apk add --no-cache --virtual .build-deps \
libffi-dev \
gcc \
musl-dev \
libxml2-dev \
libxslt-dev \
&& apk add --no-cache $(cat /tmp/package_list.txt) \
&& curl https://rclone.org/install.sh | bash \
&& if [ "$(uname -m)" = "x86_64" ]; then ARCH=amd64; elif [ "$(uname -m)" = "aarch64" ]; then ARCH=arm64; fi \
&& curl https://dl.min.io/client/mc/release/linux-${ARCH}/mc --create-dirs -o /usr/bin/mc \

# 安装依赖,安装 rclone 和 mc,清理无用文件
RUN apk add --no-cache $(cat /tmp/package_list.txt) \
&& curl -sSL https://rclone.org/install.sh | bash \
&& ARCH=$(case "$(uname -m)" in x86_64) echo "amd64";; aarch64) echo "arm64";; esac) \
&& curl -sSL https://dl.min.io/client/mc/release/linux-${ARCH}/mc -o /usr/bin/mc \
&& chmod +x /usr/bin/mc \
&& apk del --purge .build-deps \
&& rm -rf /tmp/* /root/.cache /var/cache/apk/*

# 添加 rootfs 文件
COPY --chmod=755 ./docker/rootfs /

# 最小化的运行时镜像
FROM scratch AS app
COPY --from=Builder / /

# 复制 builder 阶段的内容到运行时
COPY --from=builder / /

# 设置环境变量
ENV S6_SERVICES_GRACETIME=30000 \
S6_KILL_GRACETIME=60000 \
S6_CMD_WAIT_FOR_SERVICES_MAXTIME=0 \
Expand All @@ -35,23 +39,41 @@ ENV S6_SERVICES_GRACETIME=30000 \
UMASK=000 \
NT_PORT=3000 \
WORKDIR="/nas-tools"
RUN mkdir ${WORKDIR}

# 创建必要的目录
RUN mkdir -p ${WORKDIR} ${HOME}

# 复制应用代码到镜像
ADD ./ ${WORKDIR}/

WORKDIR ${WORKDIR}
RUN mkdir ${HOME} \
# 添加用户和用户组,并设置系统参数
RUN apk add --no-cache --virtual .build-deps \
libffi-dev \
gcc \
musl-dev \
libxml2-dev \
libxslt-dev \
&& addgroup -S nt -g 911 \
&& adduser -S nt -G nt -h ${HOME} -s /bin/bash -u 911 \
&& echo 'fs.inotify.max_user_watches=5242880' >> /etc/sysctl.conf \
&& echo 'fs.inotify.max_user_instances=5242880' >> /etc/sysctl.conf \
&& echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf \
&& echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf \
&& echo "nt ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers \
&& curl -LsSf https://astral.sh/uv/install.sh | sh \
&& source $HOME/.local/bin/env \
&& uv sync
&& curl -sSL https://astral.sh/uv/install.sh | sh \
&& . $HOME/.local/bin/env \
&& uv sync \
&& apk del --purge .build-deps \
&& rm -rf /tmp/* /root/.cache /var/cache/apk/*

# 健康检查
HEALTHCHECK --interval=30s --timeout=30s --retries=3 \
CMD wget -qO- http://localhost:${NT_PORT}/healthcheck || exit 1
EXPOSE 3000

# 暴露端口
EXPOSE ${NT_PORT}

# 挂载配置目录
VOLUME ["/config"]
ENTRYPOINT [ "/init" ]

# 启动入口
ENTRYPOINT ["/init"]

0 comments on commit f865e23

Please sign in to comment.