1+ FROM clux/muslrust:stable AS chef
2+ WORKDIR /app
3+ RUN cargo install cargo-chef
4+
5+
6+ FROM chef AS planner
7+ WORKDIR /app
8+ COPY . .
9+ RUN cargo chef prepare --recipe-path recipe.json
10+
11+
12+ FROM chef as development
13+ WORKDIR /app
14+ ARG UID=1000
15+ ARG RUN_AS_USER=appuser
16+ ARG TRACKER_UDP_PORT=6969
17+ ARG TRACKER_HTTP_PORT=7070
18+ ARG TRACKER_API_PORT=1212
19+ # Add the app user for development
20+ ENV USER=appuser
21+ ENV UID=$UID
22+ RUN adduser --uid "${UID}" "${USER}"
23+ # Build dependencies
24+ COPY --from=planner /app/recipe.json recipe.json
25+ RUN cargo chef cook --recipe-path recipe.json
26+ # Build the application
27+ COPY . .
28+ RUN cargo build --bin torrust-tracker
29+ USER $RUN_AS_USER:$RUN_AS_USER
30+ EXPOSE $TRACKER_UDP_PORT/udp
31+ EXPOSE $TRACKER_HTTP_PORT/tcp
32+ EXPOSE $TRACKER_API_PORT/tcp
33+ CMD ["cargo" , "run" ]
34+
35+
36+ FROM chef AS builder
37+ WORKDIR /app
38+ ARG UID=1000
39+ # Add the app user for production
40+ ENV USER=appuser
41+ ENV UID=$UID
42+ RUN adduser \
43+ --disabled-password \
44+ --gecos "" \
45+ --home "/nonexistent" \
46+ --shell "/sbin/nologin" \
47+ --no-create-home \
48+ --uid "${UID}" \
49+ "${USER}"
50+ # Build dependencies
51+ COPY --from=planner /app/recipe.json recipe.json
52+ RUN cargo chef cook --release --target x86_64-unknown-linux-musl --recipe-path recipe.json
53+ # Build the application
54+ COPY . .
55+ RUN cargo build --release --target x86_64-unknown-linux-musl --bin torrust-tracker
56+ # Strip the binary
57+ # More info: https://github.com/LukeMathWalker/cargo-chef/issues/149
58+ RUN strip /app/target/x86_64-unknown-linux-musl/release/torrust-tracker
59+
60+
61+ FROM alpine:latest
62+ WORKDIR /app
63+ ARG RUN_AS_USER=appuser
64+ ARG TRACKER_UDP_PORT=6969
65+ ARG TRACKER_HTTP_PORT=7070
66+ ARG TRACKER_API_PORT=1212
67+ RUN apk --no-cache add ca-certificates
68+ ENV TZ=Etc/UTC
69+ ENV RUN_AS_USER=$RUN_AS_USER
70+ COPY --from=builder /etc/passwd /etc/passwd
71+ COPY --from=builder /etc/group /etc/group
72+ COPY --from=builder --chown=$RUN_AS_USER \
73+ /app/target/x86_64-unknown-linux-musl/release/torrust-tracker \
74+ /app/torrust-tracker
75+ RUN chown -R $RUN_AS_USER:$RUN_AS_USER /app
76+ USER $RUN_AS_USER:$RUN_AS_USER
77+ EXPOSE $TRACKER_UDP_PORT/udp
78+ EXPOSE $TRACKER_HTTP_PORT/tcp
79+ EXPOSE $TRACKER_API_PORT/tcp
80+ ENTRYPOINT ["/app/torrust-tracker" ]
0 commit comments