-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDockerfile
More file actions
72 lines (52 loc) · 1.75 KB
/
Copy pathDockerfile
File metadata and controls
72 lines (52 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 前端构建阶段
FROM node:24-alpine AS frontend-builder
RUN apk add git --no-cache
RUN npm install -g bun@latest
COPY ./.git /app/.git
WORKDIR /app/admin-panel
# 复制前端依赖文件
COPY ./admin-panel /app/admin-panel
RUN bun install --frozen-lockfile
RUN bun run build
# 多阶段构建 - 构建阶段
FROM rust:1.93-slim AS builder
# 安装 musl 工具链(项目使用 rustls,不需要 OpenSSL)
RUN apt-get update && apt-get install -y \
cmake \
musl-tools \
musl-dev \
&& rm -rf /var/lib/apt/lists/*
# 添加 musl 目标
RUN rustup target add x86_64-unknown-linux-musl
# 设置工作目录
WORKDIR /app
# 复制源代码
COPY Cargo.toml Cargo.lock ./
COPY benches ./benches
COPY migration ./migration
COPY src ./src
# 从前端构建阶段复制构建产物
COPY --from=frontend-builder /app/admin-panel/dist ./admin-panel/dist
# 编译选项
ENV RUSTFLAGS="-C link-arg=-s -C opt-level=z -C target-feature=+crt-static"
# 构建参数:可选 features(默认 cli)
ARG CARGO_FEATURES="cli"
# 静态链接编译 - 使用 musl 目标
RUN touch src/main.rs && \
cargo build --release --target x86_64-unknown-linux-musl --features "${CARGO_FEATURES}"
# 运行阶段 - 使用scratch
FROM scratch
LABEL maintainer="AptS:1547 <apts-1547@esaps.net>"
LABEL description="Shortlinker is a simple, fast, and secure URL shortener written in Rust."
LABEL version="0.5.0"
LABEL homepage="https://github.com/AptS-1547/shortlinker"
LABEL license="MIT"
# 从构建阶段复制二进制文件 (使用 musl 目标路径)
COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/shortlinker /shortlinker
VOLUME ["/data", "/socket"]
# 暴露端口
EXPOSE 8080
# 设置环境变量
ENV DOCKER_ENV=1
# 启动命令
ENTRYPOINT ["/shortlinker"]