From 90eedbf86e5907d4807df9e8950c4c9179c94a2c Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Tue, 17 Jan 2023 18:01:37 +0100 Subject: [PATCH] Improved Dockerfile --- Dockerfile | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index fcaa195..c4dd2f3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,61 @@ -FROM rustlang/rust:nightly-slim AS build +FROM rust:latest AS build +# Create user +ENV USER=automation +ENV UID=10001 +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "/nonexistent" \ + --shell "/sbin/nologin" \ + --no-create-home \ + --uid "${UID}" \ + "${USER}" + +# Create basic project structure RUN cargo new --bin /app RUN cargo new --lib /app/impl_cast RUN cargo new --lib /app/google-home +# Get the correct version of the compiler +RUN rustup default nightly + +# Copy the Cargo.toml files COPY impl_cast/Cargo.toml /app/impl_cast COPY google-home/Cargo.toml /app/google-home COPY Cargo.toml Cargo.lock /app/ -WORKDIR /app/ +# Download and build all the dependencies +WORKDIR /app RUN --mount=type=cache,target=/usr/local/cargo/registry cargo build --release +# Build impl_cast COPY impl_cast/src/ /app/impl_cast/src/ -COPY google-home/src/ /app/google-home/src/ -COPY src/ /app/src/ +RUN --mount=type=cache,target=/usr/local/cargo/registry set -e; touch /app/impl_cast/src/lib.rs; cargo build --release --package impl_cast +# Build google-home +COPY google-home/src/ /app/google-home/src/ +RUN --mount=type=cache,target=/usr/local/cargo/registry set -e; touch /app/google-home/src/lib.rs; cargo build --release --package google-home + +# Build automation +COPY src/ /app/src/ RUN --mount=type=cache,target=/usr/local/cargo/registry set -e; touch /app/src/main.rs /app/src/lib.rs /app/google-home/src/lib.rs /app/impl_cast/src/lib.rs; cargo build --release CMD ["/app/target/release/automation"] + + +# FINAL IMAGE +FROM gcr.io/distroless/cc + +COPY --from=build /etc/passwd /etc/passwd +COPY --from=build /etc/group /etc/group + +ENV AUTOMATION_CONFIG=/app/config.toml +COPY config/config.toml /app/config.toml + +WORKDIR /app +COPY --from=build /app/target/release/automation ./ + +USER automation:automation + +CMD ["/app/automation"]