From a12dc717e88a3ff8c79df63a04c3a1979a21717a Mon Sep 17 00:00:00 2001 From: Vlad Proshchavaiev <32250097+F3Joule@users.noreply.github.com> Date: Wed, 25 Dec 2024 13:18:42 +0200 Subject: [PATCH] Refactor Dockerfile for builds Enhance init.sh script --- docker/Dockerfile | 50 +++++++++++++++++++++++++++-------------------- scripts/init.sh | 15 ++++++++++++++ 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 0bff55a8..0caba7d4 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,33 +1,41 @@ -FROM dappforce/cargo-chef:latest AS chef +FROM phusion/baseimage:jammy-1.0.1 AS builder + +ENV DEBIAN_FRONTEND=noninteractive + WORKDIR /subsocial -FROM chef AS planner -COPY . . -RUN cargo chef prepare --recipe-path recipe.json +COPY . /subsocial + +RUN apt-get update && \ + apt-get upgrade -y -o Dpkg::Options::="--force-confold" + +SHELL ["/bin/bash", "-o", "pipefail", "-c"] +RUN ./scripts/init.sh nosudo -FROM chef AS builder -COPY --from=planner /subsocial/recipe.json recipe.json +SHELL ["/bin/bash", "-o", "pipefail", "-c"] +RUN . "$HOME/.cargo/env" && cargo build --release -# Build dependencies - this is the caching Docker layer! -RUN cargo chef cook --release --recipe-path recipe.json +# ==== SECOND STAGE ==== -# Build application -COPY . . -RUN cargo build --release +FROM phusion/baseimage:jammy-1.0.1 + +RUN mv /usr/share/ca* /tmp && \ + rm -rf /usr/share/* && \ + mv /tmp/ca-certificates /usr/share/ && \ + useradd -m -u 1000 -U -s /bin/sh -d /subsocial subsocial -FROM debian:buster-slim COPY --from=builder /subsocial/target/release/subsocial-collator /usr/local/bin -RUN useradd -m -u 1000 -U -s /bin/sh -d /subsocial subsocial && \ - apt update && apt install curl -y && \ - mkdir -p /subsocial/.local/share && \ - mkdir /data && \ - chown -R subsocial:subsocial /data && \ - chown -R subsocial:subsocial /bin && \ - ln -s /data /subsocial/.local/share/subsocial-collator +# checks +RUN ldd /usr/local/bin/subsocial-collator && \ + /usr/local/bin/subsocial-collator --version + +# Shrinking +RUN rm -rf /usr/lib/python* && rm -rf /usr/share/man USER subsocial -EXPOSE 40333 8833 8844 +RUN mkdir -p /data + VOLUME ["/data"] -ENTRYPOINT ["/usr/local/bin/subsocial-collator"] +ENTRYPOINT ["/usr/local/bin/subsocial-collator"] \ No newline at end of file diff --git a/scripts/init.sh b/scripts/init.sh index 3d2d50e3..594469cf 100755 --- a/scripts/init.sh +++ b/scripts/init.sh @@ -4,6 +4,21 @@ set -e echo "*** Initializing WASM build environment" +if [ "$1" == "nosudo" ]; then + apt-get update && \ + apt-get install -y build-essential clang curl libssl-dev protobuf-compiler +else + sudo apt-get update && \ + sudo apt-get install -y build-essential clang curl libssl-dev protobuf-compiler +fi + +type rustup >/dev/null 2>&1 || { + echo >&2 "rustup is required, but it's not installed. Installing."; + curl https://sh.rustup.rs -sSf | sh -s -- -y && \ + . "$HOME/.cargo/env" && \ + rustup show; +} + CDIR=`dirname "$0"` export RUSTC_VERSION=`cat $CDIR/../RUSTC_VERSION`