Skip to content

Commit

Permalink
dev: rework Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Aug 6, 2023
1 parent 4a8030e commit 9c881eb
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 79 deletions.
13 changes: 0 additions & 13 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,3 @@ jobs:
- id: coverage
name: Generate Coverage Report
run: cargo llvm-cov nextest --workspace --all-targets --all-features

integration:
name: Integrations
runs-on: ubuntu-latest

steps:
- id: checkout
name: Checkout Repository
uses: actions/checkout@v3

- id: test
name: Run Integration Tests
run: ./docker/bin/run-e2e-tests.sh
236 changes: 170 additions & 66 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,80 +1,184 @@
FROM clux/muslrust:stable AS chef
WORKDIR /app
RUN cargo install cargo-chef
# syntax=docker/dockerfile:latest

# Torrust Tracker Index

FROM rust:latest as chef
WORKDIR /tmp
RUN curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
RUN cargo binstall --no-confirm cargo-chef cargo-nextest

FROM rust:slim as tester
WORKDIR /tmp
# (fixme) https://github.com/cargo-bins/cargo-binstall/issues/1252
RUN apt-get update; apt-get install -y curl; apt-get autoclean
RUN curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
RUN cargo binstall --no-confirm cargo-nextest


# Chef Prepare (look at project and see wat we need)
FROM chef AS make_recipe
COPY . /app/src
WORKDIR /app/src
RUN cargo chef prepare --recipe-path /app/recipe.json

FROM chef AS planner
WORKDIR /app
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

## Cook (release)
FROM chef AS precook_release
WORKDIR /app/src
COPY --from=make_recipe /app/recipe.json /app/recipe.json
RUN cargo chef cook --tests --benches --examples --workspace --all-targets --all-features --recipe-path /app/recipe.json --release
RUN cargo nextest archive --tests --benches --examples --workspace --all-targets --all-features --archive-file /app/temp.tar.zst --release ; rm /app/temp.tar.zst

FROM chef as development
## Cook (debug)
FROM chef AS precook_debug
WORKDIR /app/src
COPY --from=make_recipe /app/recipe.json /app/recipe.json
RUN cargo chef cook --tests --benches --examples --workspace --all-targets --all-features --recipe-path /app/recipe.json
RUN cargo nextest archive --tests --benches --examples --workspace --all-targets --all-features --archive-file /app/temp.tar.zst ; rm /app/temp.tar.zst


## Build Archive (release)
FROM precook_release AS build_release
WORKDIR /app/src
COPY . /app/src
RUN cargo nextest archive --tests --benches --examples --workspace --all-targets --all-features --archive-file /app/torrust-tracker-release.tar.zst --release

## Build Archive (debug)
FROM precook_debug AS build_debug
WORKDIR /app/src
COPY . /app/src
RUN cargo nextest archive --tests --benches --examples --workspace --all-targets --all-features --archive-file /app/torrust-tracker-debug.tar.zst


# Extract and Test Release
FROM tester as test_release
WORKDIR /app
ARG UID=1000
ARG RUN_AS_USER=appuser
ARG TRACKER_UDP_PORT=6969
ARG TRACKER_HTTP_PORT=7070
ARG TRACKER_API_PORT=1212
# Add the app user for development
ENV USER=appuser
ENV UID=$UID
RUN adduser --uid "${UID}" "${USER}"
# Build dependencies
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --recipe-path recipe.json
# Build the application
COPY . .
RUN cargo build --bin torrust-tracker
USER $RUN_AS_USER:$RUN_AS_USER
EXPOSE $TRACKER_UDP_PORT/udp
EXPOSE $TRACKER_HTTP_PORT/tcp
EXPOSE $TRACKER_API_PORT/tcp
CMD ["cargo", "run"]


FROM chef AS builder
COPY . /app/src
COPY --from=build_release \
/app/torrust-tracker-release.tar.zst \
/app/torrust-tracker-release.tar.zst
RUN cargo nextest run --workspace-remap /app/src/ --extract-to /app/src/ --no-run --archive-file /app/torrust-tracker-release.tar.zst
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
RUN mkdir -p /app/bin/; cp --link /app/src/target/release/torrust-tracker /app/bin/torrust-tracker

# Extract and Test Debug
FROM tester as test_debug
WORKDIR /app
ARG UID=1000
# Add the app user for production
ENV USER=appuser
ENV UID=$UID
COPY . /app/src
COPY --from=build_debug \
/app/torrust-tracker-debug.tar.zst \
/app/torrust-tracker-debug.tar.zst
RUN mkdir -p /app/test
RUN cargo nextest run --workspace-remap /app/src/ --extract-to /app/src/ --no-run --archive-file /app/torrust-tracker-debug.tar.zst
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
RUN mkdir -p /app/bin/; cp --link /app/src/target/debug/torrust-tracker /app/bin/torrust-tracker


# Release Torrust-Tracker
FROM rust:slim as release
WORKDIR /app
COPY --from=test_release /app/bin /app/bin

ARG USER_ID=1000
ARG USER_NAME=appuser
ARG UDP_PORT=6969
ARG HTTP_PORT=7070
ARG API_PORT=1212

ENV USER_ID=${USER_ID}
ENV USER_NAME=${USER_NAME}
ENV UDP_PORT=${UDP_PORT}
ENV HTTP_PORT=${HTTP_PORT}
ENV API_PORT=${API_PORT}
ENV TZ=Etc/UTC

EXPOSE ${UDP_PORT}/udp
EXPOSE ${HTTP_PORT}/tcp
EXPOSE ${API_PORT}/tcp

RUN printf "\n in release mode \n \n run 'exec /app/bin/torrust-tracker' (release build) to start tracker \n \n" > /etc/motd
RUN echo '[ ! -z "$TERM" -a -r /etc/motd ] && cat /etc/motd' >> /etc/bash.bashrc

RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
"${USER}"
# Build dependencies
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --target x86_64-unknown-linux-musl --recipe-path recipe.json
# Build the application
COPY . .
RUN cargo build --release --target x86_64-unknown-linux-musl --bin torrust-tracker
# Strip the binary
# More info: https://github.com/LukeMathWalker/cargo-chef/issues/149
RUN strip /app/target/x86_64-unknown-linux-musl/release/torrust-tracker


FROM alpine:latest
--uid "${USER_ID}" \
"${USER_NAME}"

RUN chown -R "${USER_NAME}":"${USER_NAME}" /app
USER "${USER_NAME}":"${USER_NAME}"

RUN env


# Debug Torrust-Tracker
FROM rust:slim as debug
WORKDIR /app
COPY --from=build_debug /app/bin /app/bin

ARG USER_ID=1000
ARG USER_NAME=appuser
ARG UDP_PORT=6969
ARG HTTP_PORT=7070
ARG API_PORT=1212

ENV USER_ID=${USER_ID}
ENV USER_NAME=${USER_NAME}
ENV UDP_PORT=${UDP_PORT}
ENV HTTP_PORT=${HTTP_PORT}
ENV API_PORT=${API_PORT}
ENV TZ=Etc/UTC

EXPOSE ${UDP_PORT}/udp
EXPOSE ${HTTP_PORT}/tcp
EXPOSE ${API_PORT}/tcp

RUN printf "\n in debug mode \n \n run 'exec /app/bin/torrust-tracker' (debug build) to start tracker \n \n" > /etc/motd
RUN echo '[ ! -z "$TERM" -a -r /etc/motd ] && cat /etc/motd' >> /etc/bash.bashrc

RUN adduser --uid "${USER_ID}" "${USER_NAME}"
RUN chown -R "${USER_NAME}":"${USER_NAME}" /app
USER "${USER_NAME}":"${USER_NAME}"

RUN env


# Development
FROM build_debug as development
WORKDIR /app
ARG RUN_AS_USER=appuser
ARG TRACKER_UDP_PORT=6969
ARG TRACKER_HTTP_PORT=7070
ARG TRACKER_API_PORT=1212
RUN apk --no-cache add ca-certificates

ARG USER_ID=1000
ARG USER_NAME=appuser
ARG UDP_PORT=6969
ARG HTTP_PORT=7070
ARG API_PORT=1212

ENV USER_ID=${USER_ID}
ENV USER_NAME=${USER_NAME}
ENV UDP_PORT=${UDP_PORT}
ENV HTTP_PORT=${HTTP_PORT}
ENV API_PORT=${API_PORT}
ENV TZ=Etc/UTC
ENV RUN_AS_USER=$RUN_AS_USER
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
COPY --from=builder --chown=$RUN_AS_USER \
/app/target/x86_64-unknown-linux-musl/release/torrust-tracker \
/app/torrust-tracker
RUN chown -R $RUN_AS_USER:$RUN_AS_USER /app
USER $RUN_AS_USER:$RUN_AS_USER
EXPOSE $TRACKER_UDP_PORT/udp
EXPOSE $TRACKER_HTTP_PORT/tcp
EXPOSE $TRACKER_API_PORT/tcp
ENTRYPOINT ["/app/torrust-tracker"]

EXPOSE ${UDP_PORT}/udp
EXPOSE ${HTTP_PORT}/tcp
EXPOSE ${API_PORT}/tcp

RUN printf "\n in development environment \n \n run 'exec /app/src/target/debug/torrust-tracker' to start tracker \n \n" > /etc/motd
RUN echo '[ ! -z "$TERM" -a -r /etc/motd ] && cat /etc/motd' >> /etc/bash.bashrc

RUN adduser --uid "${USER_ID}" "${USER_NAME}"
RUN chown -R "${USER_NAME}":"${USER_NAME}" /app
USER "${USER_NAME}":"${USER_NAME}"

RUN env



# Run Release by Default
FROM release as default
WORKDIR /app
CMD /app/bin/torrust-tracker

0 comments on commit 9c881eb

Please sign in to comment.