Skip to content

Commit

Permalink
chore: improve dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
tiamo committed May 23, 2023
1 parent b5b9ffa commit e152d14
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 79 deletions.
55 changes: 23 additions & 32 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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" ]
47 changes: 0 additions & 47 deletions Dockerfile.chef

This file was deleted.

77 changes: 77 additions & 0 deletions old.Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]

0 comments on commit e152d14

Please sign in to comment.