Skip to content

Commit fc76313

Browse files
committed
dev: container work
overhaul dockerfile update container workflow
1 parent f399ae4 commit fc76313

File tree

5 files changed

+203
-105
lines changed

5 files changed

+203
-105
lines changed

.github/workflows/container.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Container (Docker)
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
env:
8+
CARGO_TERM_COLOR: always
9+
10+
jobs:
11+
test:
12+
name: Test
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- id: checkout
17+
name: Checkout Repository
18+
uses: actions/checkout@v3
19+
20+
- id: setup
21+
name: Setup Toolchain
22+
uses: docker/setup-buildx-action@v2
23+
24+
- id: build
25+
name: Build
26+
uses: docker/build-push-action@v4
27+
with:
28+
push: false
29+
load: true
30+
tags: torrust-tracker:local
31+
cache-from: type=gha
32+
cache-to: type=gha,mode=max
33+
34+
- id: inspect
35+
name: Inspect
36+
run: docker image inspect torrust-tracker:local
37+
38+
- id: compose
39+
name: Compose
40+
run: docker compose build

.github/workflows/test_docker.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

Dockerfile

Lines changed: 139 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,146 @@
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
3757
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
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
6272
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
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}
68138
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"]
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"]

cSpell.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
22
"words": [
3+
"adduser",
34
"alekitto",
45
"appuser",
56
"Arvid",
7+
"autoclean",
68
"AUTOINCREMENT",
79
"automock",
810
"Avicora",
@@ -12,6 +14,7 @@
1214
"bencoded",
1315
"beps",
1416
"binascii",
17+
"binstall",
1518
"Bitflu",
1619
"bools",
1720
"bufs",
@@ -26,11 +29,13 @@
2629
"codegen",
2730
"completei",
2831
"connectionless",
32+
"distroless",
2933
"dockerhub",
3034
"downloadedi",
3135
"dtolnay",
3236
"filesd",
3337
"Freebox",
38+
"gecos",
3439
"Grcov",
3540
"hasher",
3641
"hexlify",
@@ -49,6 +54,7 @@
4954
"leechers",
5055
"libsqlite",
5156
"libtorrent",
57+
"libz",
5258
"Lphant",
5359
"metainfo",
5460
"middlewares",
@@ -59,14 +65,18 @@
5965
"nanos",
6066
"nextest",
6167
"nocapture",
68+
"nologin",
69+
"nonroot",
6270
"Norberg",
6371
"numwant",
6472
"oneshot",
6573
"ostr",
6674
"Pando",
6775
"proot",
76+
"proto",
6877
"Quickstart",
6978
"Rasterbar",
79+
"realpath",
7080
"reannounce",
7181
"repr",
7282
"reqwest",
@@ -88,6 +98,7 @@
8898
"Swiftbit",
8999
"taiki",
90100
"thiserror",
101+
"tlsv",
91102
"Torrentstorm",
92103
"torrust",
93104
"torrustracker",
@@ -106,5 +117,8 @@
106117
"Xunlei",
107118
"xxxxxxxxxxxxxxxxxxxxd",
108119
"yyyyyyyyyyyyyyyyyyyyd"
120+
],
121+
"enableFiletypes": [
122+
"dockerfile"
109123
]
110124
}

compose.yaml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ name: torrust
22
services:
33

44
tracker:
5-
build:
6-
context: .
7-
target: development
8-
user: ${TORRUST_TRACKER_USER_UID:-1000}:${TORRUST_TRACKER_USER_UID:-1000}
5+
pull_policy: missing
6+
image: torrust-tracker:local
7+
# build:
8+
# context: .
9+
# tags:
10+
# - torrust-tracker:local
11+
12+
user: ${USER_UID:-1000}:${USER_UID:-1000}
913
tty: true
1014
networks:
1115
- server_side
@@ -14,8 +18,8 @@ services:
1418
- 7070:7070
1519
- 1212:1212
1620
volumes:
17-
- ./:/app
18-
- ~/.cargo:/home/appuser/.cargo
21+
- ./:/app/src
22+
- ./storage:/app/storage
1923
depends_on:
2024
- mysql
2125

0 commit comments

Comments
 (0)