1- FROM clux/muslrust:stable AS chef
2- WORKDIR /app
3- RUN cargo install cargo-chef
1+ # syntax=docker/dockerfile:latest
42
3+ # Torrust Tracker Index
54
6- FROM chef AS planner
7- WORKDIR /app
8- COPY . .
9- RUN cargo chef prepare --recipe-path recipe.json
5+ # # Builder Image
6+ FROM rust:latest as chef
7+ WORKDIR /tmp
8+ RUN curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
9+ RUN cargo binstall --no-confirm cargo-chef cargo-nextest
1010
11+ # # Tester Image
12+ FROM rust:slim as tester
13+ WORKDIR /tmp
14+ # ## (fixme) https://github.com/cargo-bins/cargo-binstall/issues/1252
15+ RUN apt-get update; apt-get install -y curl; apt-get autoclean
16+ RUN curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
17+ RUN cargo binstall --no-confirm cargo-nextest
1118
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
19+
20+ # # Chef Prepare (look at project and see wat we need)
21+ FROM chef AS recipe
22+ COPY . /app/src
23+ WORKDIR /app/src
24+ RUN cargo chef prepare --recipe-path /app/recipe.json
25+
26+
27+ # # Cook (release)
28+ FROM chef AS dependencies
29+ WORKDIR /app/src
30+ COPY --from=recipe /app/recipe.json /app/recipe.json
31+ RUN cargo chef cook --tests --benches --examples --workspace --all-targets --all-features --recipe-path /app/recipe.json --release
32+ RUN cargo nextest archive --tests --benches --examples --workspace --all-targets --all-features --archive-file /app/temp.tar.zst --release ; rm /app/temp.tar.zst
33+
34+ # # Cook (debug)
35+ FROM chef AS dependencies_debug
36+ WORKDIR /app/src
37+ COPY --from=recipe /app/recipe.json /app/recipe.json
38+ RUN cargo chef cook --tests --benches --examples --workspace --all-targets --all-features --recipe-path /app/recipe.json
39+ RUN cargo nextest archive --tests --benches --examples --workspace --all-targets --all-features --archive-file /app/temp.tar.zst ; rm /app/temp.tar.zst
40+
41+
42+ # # Build Archive (release)
43+ FROM dependencies AS build
44+ WORKDIR /app/src
45+ COPY . /app/src
46+ RUN cargo nextest archive --tests --benches --examples --workspace --all-targets --all-features --archive-file /app/torrust-tracker.tar.zst --release
47+
48+ # # Build Archive (debug)
49+ FROM dependencies_debug AS build_debug
50+ WORKDIR /app/src
51+ COPY . /app/src
52+ RUN cargo nextest archive --tests --benches --examples --workspace --all-targets --all-features --archive-file /app/torrust-tracker-debug.tar.zst
53+
54+
55+ # Extract and Test (release)
56+ FROM tester as test
3757WORKDIR /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
58+ COPY . /app/src
59+ COPY --from=build \
60+ /app/torrust-tracker.tar.zst \
61+ /app/torrust-tracker.tar.zst
62+ RUN cargo nextest run --workspace-remap /app/src/ --extract-to /app/src/ --no-run --archive-file /app/torrust-tracker.tar.zst
63+ RUN cargo nextest run --workspace-remap /app/src/ --cargo-metadata /app/src/target/nextest/cargo-metadata.json --binaries-metadata /app/src/target/nextest/binaries-metadata.json
64+ RUN mkdir -p /app/bin/; cp --link /app/src/target/release/torrust-tracker /app/bin/torrust-tracker
65+ RUN chmod -R u=rwx,go=rx,a+X /app/bin
66+ RUN chown -R root:root /app/bin
67+ RUN mkdir /app/lib/; cp $(realpath $(ldd /app/bin/torrust-tracker | grep "libz\. so\. 1" | awk '{print $3}' )) /app/lib/libz.so.1
68+
69+
70+ # Extract and Test (debug)
71+ FROM tester as test_debug
6272WORKDIR /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
73+ COPY . /app/src
74+ COPY --from=build_debug \
75+ /app/torrust-tracker-debug.tar.zst \
76+ /app/torrust-tracker-debug.tar.zst
77+ RUN mkdir -p /app/test
78+ RUN cargo nextest run --workspace-remap /app/src/ --extract-to /app/src/ --no-run --archive-file /app/torrust-tracker-debug.tar.zst
79+ RUN cargo nextest run --workspace-remap /app/src/ --cargo-metadata /app/src/target/nextest/cargo-metadata.json --binaries-metadata /app/src/target/nextest/binaries-metadata.json
80+ RUN mkdir -p /app/bin/; cp --link /app/src/target/debug/torrust-tracker /app/bin/torrust-tracker
81+ RUN chmod -R u=rwx,go=rx,a+X /app/bin
82+ RUN chown -R root:root /app/bin
83+ RUN mkdir /app/lib/; cp -p $(realpath $(ldd /app/bin/torrust-tracker | grep "libz\. so\. 1" | awk '{print $3}' )) /app/lib/libz.so.1
84+
85+
86+ # # Torrust-Tracker (release)
87+ FROM gcr.io/distroless/cc:nonroot as tracker
88+ COPY --from=gcr.io/distroless/cc:debug /busybox/wget /usr/bin/wget
89+ COPY --from=test /app/bin /usr/bin
90+ COPY --from=test /app/lib /usr/lib
91+
92+ # # Torrust-Tracker (debug)
93+ FROM gcr.io/distroless/cc:debug as tracker_debug
94+
95+ RUN ["/busybox/cp" , "-sp" , "/busybox/sh" , "/bin/sh" ]
96+ ENV ENV=/etc/profile
97+
98+ ARG USER_ID=1000
99+ ARG USER_NAME=appuser
100+ ARG UDP_PORT=6969
101+ ARG HTTP_PORT=7070
102+ ARG API_PORT=1212
103+
104+ ENV USER_ID=${USER_ID}
105+ ENV USER_NAME=${USER_NAME}
106+ ENV UDP_PORT=${UDP_PORT}
107+ ENV HTTP_PORT=${HTTP_PORT}
108+ ENV API_PORT=${API_PORT}
109+ ENV TZ=Etc/UTC
110+
111+ EXPOSE ${UDP_PORT}/udp
112+ EXPOSE ${HTTP_PORT}/tcp
113+ EXPOSE ${API_PORT}/tcp
114+
115+ COPY --from=test_debug /app/bin /usr/bin
116+ COPY --from=test_debug /app/lib /usr/lib
117+ RUN chmod -R u=rwx,go=rx,a+X /usr/bin
118+ RUN chown -R root:root /usr/bin
119+
120+ RUN printf "\n in debug mode \n \n run 'exec /app/bin/torrust-tracker' (debug build) to start tracker \n \n " > /etc/motd
121+ RUN echo '[ ! -z "$TERM" -a -r /etc/motd ] && cat /etc/motd' >> /etc/profile.d/motd.sh
122+
123+ RUN adduser --disabled-password --uid "${USER_ID}" "${USER_NAME}"
124+ USER "${USER_NAME}" :"${USER_NAME}"
125+
126+ RUN env
127+
128+
129+ # # Run Release by Default
130+ FROM tracker as default
131+ ARG UDP_PORT=6969
132+ ARG HTTP_PORT=7070
133+ ARG API_PORT=1212
134+
135+ ENV UDP_PORT=${UDP_PORT}
136+ ENV HTTP_PORT=${HTTP_PORT}
137+ ENV API_PORT=${API_PORT}
68138ENV 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" ]
139+
140+ EXPOSE ${UDP_PORT}/udp
141+ EXPOSE ${HTTP_PORT}/tcp
142+ EXPOSE ${API_PORT}/tcp
143+
144+ # HEALTHCHECK ["/usr/bin/wget", "--no-verbose", "--tries=1", "--spider", "localhost:${API_PORT}/version"]
145+
146+ CMD ["/usr/bin/torrust-tracker" ]
0 commit comments