From c93b02233bff8adeed77ffe32f2a5215006ac108 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Sun, 12 Sep 2021 15:03:49 +0300 Subject: [PATCH] decrease docker image size --- Dockerfile | 58 +++++++++++++++++++++++++++++++++++++++---------- docker/start.sh | 2 +- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index 03da983b..44fafe58 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,57 @@ -FROM rustlang/rust:nightly-bullseye - -WORKDIR /app - -COPY ./. . +#################################################################################################### +## Builder +#################################################################################################### +FROM rustlang/rust:nightly-bullseye AS builder +RUN apt update && apt install -y libssl-dev pkg-config libz-dev libcurl4 postgresql +RUN update-ca-certificates + +# Create appuser +ENV USER=bot +ENV UID=10001 + +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "/nonexistent" \ + --shell "/sbin/nologin" \ + --no-create-home \ + --uid "${UID}" \ + "${USER}" + +WORKDIR /bot + +COPY ./ . RUN cargo install diesel_cli --no-default-features --features postgres RUN cargo build --release -RUN cp ./target/release/cleaner ./cleaner +#################################################################################################### +## Final image +#################################################################################################### +FROM debian:bullseye-slim + +RUN apt update && apt install -y postgresql + +# Import from builder. +COPY --from=builder /etc/passwd /etc/passwd +COPY --from=builder /etc/group /etc/group + +WORKDIR /bot + +# Copy our build +COPY --from=builder /bot/target/release/el_monitorro ./ +COPY --from=builder /bot/target/release/deliver ./ +COPY --from=builder /bot/target/release/sync ./ +COPY --from=builder /bot/target/release/cleaner ./ -RUN cp ./target/release/sync ./sync +COPY --from=builder /bot/docker/start.sh ./ -RUN cp ./target/release/el_monitorro ./el_monitorro +COPY --from=builder /usr/local/cargo/bin/diesel ./ +COPY --from=builder /bot/migrations/ ./migrations/ -RUN cp ./target/release/deliver ./deliver -RUN rm -rf ./target +# Use an unprivileged user. +USER bot:bot -CMD ["bash", "/app/docker/start.sh"] +CMD ["bash", "/bot/start.sh"] diff --git a/docker/start.sh b/docker/start.sh index 482d63b4..d87fdf05 100644 --- a/docker/start.sh +++ b/docker/start.sh @@ -6,7 +6,7 @@ then echo "Proceeding without setting the database" else echo "Setting the database" - diesel database setup + ./diesel database setup fi