diff --git a/Dockerfile b/Dockerfile index e8774d5..fdc7da4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,46 +1,38 @@ # syntax=docker/dockerfile:1.4 +# https://github.com/LukeMathWalker/cargo-chef ARG APP_NAME=svt-agent ARG RUST_LOG=info ARG RUST_BACKTRACE=0 -########################## -## Build layer -########################## - -FROM rust:slim as builder - -## This is important, see https://github.com/rust-lang/docker-rust/issues/85 -#ENV RUSTFLAGS="-C target-feature=-crt-static" -# -## if needed, add additional dependencies here -#RUN apk add --no-cache build-base openssl-dev - -RUN apt update && apt install -y --no-install-recommends libopus-dev libssl-dev pkg-config - +FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef WORKDIR /app +FROM chef AS planner COPY Cargo.* . COPY src ./src +RUN cargo chef prepare --recipe-path recipe.json -RUN \ - --mount=type=cache,target=/usr/local/cargo/registry \ - --mount=type=cache,target=/app/target \ - cargo install --root /app --path . +FROM chef AS cacher +COPY --from=planner /app/recipe.json recipe.json +# Build dependencies - this is the caching Docker layer! +RUN --mount=type=cache,target=/usr/local/cargo/registry,id=svt-agent-registry \ + --mount=type=cache,target=/app/target,id=svt-agent-deps \ + cargo chef cook --release --recipe-path recipe.json && \ + ls -la ./target/release -# do a release build -#RUN cargo build --release -#RUN strip target/release/${APP_NAME} - -# RUN cargo install --root /app --target=x86_64-unknown-linux-musl --path . -# RUN cargo install --root /app --path . - -####################### -# Final image -####################### +FROM chef AS builder +COPY Cargo.* . +COPY src ./src +COPY --from=cacher $CARGO_HOME $CARGO_HOME +COPY --from=cacher /app/target target +RUN --mount=type=cache,target=/usr/local/cargo/registry,id=svt-agent-registry \ + cargo build --release -FROM debian:bullseye-slim as final -#FROM alpine:3.18 as final +# FROM scratch +# FROM gcr.io/distroless/cc +# FROM gcr.io/distroless/cc-debian1 +FROM debian:buster-slim AS final WORKDIR /app @@ -50,8 +42,7 @@ ENV RUST_LOG=${RUST_LOG} ARG RUST_BACKTRACE ENV RUST_BACKTRACE=${RUST_BACKTRACE} -COPY --from=builder /app/bin/${APP_NAME} / +COPY --from=builder /app/target/release/${APP_NAME} / COPY ./ansible/ ./ansible -#ENTRYPOINT ["/tini", "--"] ENTRYPOINT [ "/svt-agent" ] diff --git a/Dockerfile.chef b/Dockerfile.chef deleted file mode 100644 index 60b0d47..0000000 --- a/Dockerfile.chef +++ /dev/null @@ -1,47 +0,0 @@ -# Using the `rust-musl-builder` as base image, instead of -# the official Rust toolchain -# FROM clux/muslrust:stable AS chef -# USER root -# RUN cargo install cargo-chef -# WORKDIR /app - -FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef -WORKDIR app - -FROM chef AS planner -COPY . . -RUN cargo chef prepare --recipe-path recipe.json - -FROM chef AS builder -COPY --from=planner /app/recipe.json recipe.json -RUN cargo chef cook --release --recipe-path recipe.json -# RUN cargo chef cook --release --target x86_64-unknown-linux-musl --recipe-path recipe.json -COPY . . -# add logs folder here... (`RUN mkdir` doesnt work in the scratch image) -RUN mkdir -p logs -RUN cargo build --release -# RUN cargo build --release --target x86_64-unknown-linux-musl - -FROM chef AS sv_manager -ARG SV_MANAGER_TAG=agent -RUN curl -fsSL https://github.com/mfactory-lab/sv-manager/archive/refs/tags/${SV_MANAGER_TAG}.tar.gz -o archive.tar.gz \ - && tar -xvf archive.tar.gz --strip-components=1 \ - && rm archive.tar.gz - -# FROM gcr.io/distroless/cc -# FROM gcr.io/distroless/cc-debian1 -FROM debian:bullseye-slim -# FROM scratch - -WORKDIR app - -COPY --from=builder /app/target/release/svt-agent /usr/local/bin/svt-agent -# COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/svt-agent /usr/local/bin/ -COPY --from=builder /app/logs ./ -COPY --from=sv_manager /app ./ansible -COPY ./ansible/ ./ansible - -ENV RUST_LOG=info -ENV RUST_BACKTRACE=1 - -ENTRYPOINT ["/usr/local/bin/svt-agent"] diff --git a/old.Dockerfile b/old.Dockerfile new file mode 100644 index 0000000..42edb77 --- /dev/null +++ b/old.Dockerfile @@ -0,0 +1,77 @@ +# syntax=docker/dockerfile:1.4 + +ARG APP_NAME=svt-agent +ARG RUST_LOG=info +ARG RUST_BACKTRACE=0 + +########################## +## Build layer +########################## +# rust:1.69.0-slim-bullseye as of 2023/05/18 +FROM rust:1.69.0-slim-bullseye as builder + +ARG APP_NAME + +# Optional and will be used just if you have matrix build with different platforms. +ARG TARGETPLATFORM + +ENV CARGO_HOME=/usr/local/cargo +ENV SCCACHE_DIR=/usr/local/sccache +ENV SCCACHE_CACHE_SIZE="3G" + +#ENV CARGO_INCREMENTAL=0 + +WORKDIR /app + +RUN apt update && apt install -y libssl-dev ca-certificates pkg-config + +RUN --mount=type=cache,target=${CARGO_HOME}/registry,id=${TARGETPLATFORM} \ + cargo install cargo-strip sccache + +ENV CARGO_BUILD_RUSTC_WRAPPER=/usr/local/cargo/bin/sccache + +# RUN apt update && apt install -y --no-install-recommends libopus-dev libssl-dev pkg-config + +COPY Cargo.* . +COPY src ./src + +RUN --mount=type=cache,target=${CARGO_HOME}/registry,id=${TARGETPLATFORM} \ + --mount=type=cache,target=${SCCACHE_DIR},id=${TARGETPLATFORM} \ + --mount=type=cache,target=/app/target,id=${TARGETPLATFORM} \ + cargo --locked build --release + +RUN cargo strip && sccache --show-stats + +#RUN \ +# --mount=type=cache,target=/usr/local/cargo/registry \ +# --mount=type=cache,target=/app/target \ +# cargo install --root /app --path . + +# do a release build +#RUN cargo build --release +#RUN strip target/release/${APP_NAME} + +# RUN cargo install --root /app --target=x86_64-unknown-linux-musl --path . +# RUN cargo install --root /app --path . + +####################### +# Final image +####################### + +FROM debian:buster-slim AS final +#FROM debian:bullseye-slim as final +#FROM alpine:3.18 as final + +WORKDIR /app + +ARG APP_NAME +ARG RUST_LOG +ENV RUST_LOG=${RUST_LOG} +ARG RUST_BACKTRACE +ENV RUST_BACKTRACE=${RUST_BACKTRACE} + +COPY --from=builder /app/target/release/${APP_NAME} / +COPY ./ansible/ ./ansible + +#ENTRYPOINT ["/tini", "--"] +ENTRYPOINT [ "/svt-agent" ]