Skip to content

Commit 46e1a37

Browse files
committed
feat: docker support
1 parent f8700aa commit 46e1a37

File tree

14 files changed

+581
-0
lines changed

14 files changed

+581
-0
lines changed

.dockerignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.git
2+
.git-blame-ignore
3+
.github
4+
.gitignore
5+
.vscode
6+
bin/
7+
config.toml
8+
config.toml.local
9+
cSpell.json
10+
data.db
11+
docker/
12+
NOTICE
13+
README.md
14+
rustfmt.toml
15+
storage/
16+
target/

.env.local

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TORRUST_TRACKER_USER_UID=1000
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: Publish docker image
2+
3+
on:
4+
push:
5+
branches:
6+
- 'develop'
7+
# todo: only during development of issue 11
8+
- 'docker'
9+
- 'docker-reorganized-pr'
10+
tags:
11+
- "v*"
12+
13+
env:
14+
# Azure file share volume mount requires the Linux container run as root
15+
# https://learn.microsoft.com/en-us/azure/container-instances/container-instances-volume-azure-files#limitations
16+
TORRUST_TRACKER_RUN_AS_USER: root
17+
18+
jobs:
19+
test:
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v2
23+
- uses: actions-rs/toolchain@v1
24+
with:
25+
profile: minimal
26+
toolchain: stable
27+
components: llvm-tools-preview
28+
- uses: Swatinem/rust-cache@v1
29+
- name: Run Tests
30+
run: cargo test
31+
32+
dockerhub:
33+
needs: test
34+
runs-on: ubuntu-latest
35+
environment: dockerhub-torrust
36+
steps:
37+
- name: Checkout
38+
uses: actions/checkout@v3
39+
40+
- name: Docker meta
41+
id: meta
42+
uses: docker/metadata-action@v4
43+
with:
44+
images: |
45+
# For example: torrust/tracker
46+
"${{ secrets.DOCKER_HUB_USERNAME }}/${{secrets.DOCKER_HUB_REPOSITORY_NAME }}"
47+
tags: |
48+
type=ref,event=branch
49+
type=ref,event=pr
50+
type=semver,pattern={{version}}
51+
type=semver,pattern={{major}}.{{minor}}
52+
53+
- name: Login to Docker Hub
54+
uses: docker/login-action@v2
55+
with:
56+
username: ${{ secrets.DOCKER_HUB_USERNAME }}
57+
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
58+
59+
- name: Set up Docker Buildx
60+
uses: docker/setup-buildx-action@v2
61+
62+
- name: Build and push
63+
uses: docker/build-push-action@v3
64+
with:
65+
context: .
66+
file: ./Dockerfile
67+
build-args: |
68+
RUN_AS_USER=${{ env.TORRUST_TRACKER_RUN_AS_USER }}
69+
push: ${{ github.event_name != 'pull_request' }}
70+
tags: ${{ steps.meta.outputs.tags }}
71+
labels: ${{ steps.meta.outputs.labels }}
72+
cache-from: type=gha
73+
cache-to: type=gha,mode=max

.github/workflows/test_docker.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Test docker build
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
13+
- name: Set up Docker Buildx
14+
uses: docker/setup-buildx-action@v2
15+
16+
- name: Build docker image
17+
uses: docker/build-push-action@v3
18+
with:
19+
context: .
20+
file: ./Dockerfile
21+
push: false
22+
cache-from: type=gha
23+
cache-to: type=gha,mode=max
24+
25+
- name: Build docker-compose images
26+
run: docker compose build

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.env
12
/target
23
**/*.rs.bk
34
/database.json.bz2

Dockerfile

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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"]

bin/install.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# Generate the default settings file if it does not exist
4+
if ! [ -f "./config.toml" ]; then
5+
cp ./config.toml.local ./config.toml
6+
fi
7+
8+
# Generate the sqlite database if it does not exist
9+
if ! [ -f "./storage/database/data.db" ]; then
10+
# todo: it should get the path from config.toml and only do it when we use sqlite
11+
touch ./storage/database/data.db
12+
echo ";" | sqlite3 ./storage/database/data.db
13+
fi

cSpell.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,38 @@
99
"Bitflu",
1010
"bools",
1111
"bufs",
12+
"Buildx",
1213
"byteorder",
1314
"canonicalize",
1415
"canonicalized",
1516
"chrono",
1617
"clippy",
1718
"completei",
19+
"dockerhub",
1820
"downloadedi",
1921
"filesd",
2022
"Freebox",
2123
"hasher",
2224
"hexlify",
25+
"hlocalhost",
2326
"Hydranode",
2427
"incompletei",
28+
"infoschema",
2529
"intervali",
2630
"leecher",
2731
"leechers",
2832
"libtorrent",
2933
"Lphant",
3034
"mockall",
35+
"myacicontext",
3136
"nanos",
3237
"nextest",
3338
"nocapture",
3439
"oneshot",
3540
"ostr",
3641
"Pando",
42+
"proot",
43+
"Quickstart",
3744
"Rasterbar",
3845
"repr",
3946
"reqwest",
@@ -50,9 +57,11 @@
5057
"thiserror",
5158
"Torrentstorm",
5259
"torrust",
60+
"torrustracker",
5361
"typenum",
5462
"Unamed",
5563
"untuple",
64+
"uroot",
5665
"Vagaa",
5766
"Xtorrent",
5867
"Xunlei"

compose.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: torrust
2+
services:
3+
4+
tracker:
5+
build:
6+
context: .
7+
target: development
8+
user: ${TORRUST_TRACKER_USER_UID:-1000}:${TORRUST_TRACKER_USER_UID:-1000}
9+
tty: true
10+
networks:
11+
- server_side
12+
ports:
13+
- 6969:6969/udp
14+
- 7070:7070
15+
- 1212:1212
16+
volumes:
17+
- ./:/app
18+
- ~/.cargo:/home/appuser/.cargo
19+
depends_on:
20+
- mysql
21+
22+
mysql:
23+
image: mysql:8.0
24+
command: '--default-authentication-plugin=mysql_native_password'
25+
restart: always
26+
healthcheck:
27+
test: ['CMD-SHELL', 'mysqladmin ping -h 127.0.0.1 --password="$$(cat /run/secrets/db-password)" --silent']
28+
interval: 3s
29+
retries: 5
30+
start_period: 30s
31+
environment:
32+
- MYSQL_ROOT_HOST=%
33+
- MYSQL_ROOT_PASSWORD=root_secret_password
34+
- MYSQL_DATABASE=torrust_tracker
35+
- MYSQL_USER=db_user
36+
- MYSQL_PASSWORD=db_user_secret_password
37+
networks:
38+
- server_side
39+
ports:
40+
- 3306:3306
41+
volumes:
42+
- mysql_data:/var/lib/mysql
43+
44+
networks:
45+
server_side: {}
46+
47+
volumes:
48+
mysql_data: {}

config.toml.local

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
log_level = "info"
2+
mode = "public"
3+
db_driver = "Sqlite3"
4+
db_path = "./storage/database/data.db"
5+
announce_interval = 120
6+
min_announce_interval = 120
7+
max_peer_timeout = 900
8+
on_reverse_proxy = false
9+
external_ip = "0.0.0.0"
10+
tracker_usage_statistics = true
11+
persistent_torrent_completed_stat = false
12+
inactive_peer_cleanup_interval = 600
13+
remove_peerless_torrents = true
14+
15+
[[udp_trackers]]
16+
enabled = false
17+
bind_address = "0.0.0.0:6969"
18+
19+
[[http_trackers]]
20+
enabled = false
21+
bind_address = "0.0.0.0:7070"
22+
ssl_enabled = false
23+
ssl_cert_path = ""
24+
ssl_key_path = ""
25+
26+
[http_api]
27+
enabled = true
28+
bind_address = "127.0.0.1:1212"
29+
ssl_enabled = false
30+
ssl_cert_path = ""
31+
ssl_key_path = ""
32+
33+
[http_api.access_tokens]
34+
admin = "MyAccessToken"

0 commit comments

Comments
 (0)