Skip to content

Commit

Permalink
Refactor Dockerfile for builds and enhance init.sh script (#267)
Browse files Browse the repository at this point in the history
* Refactor Dockerfile for builds

Enhance init.sh script

* Fix Dockerfile
  • Loading branch information
F3Joule authored Dec 25, 2024
1 parent 98f0b0a commit c94621d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
49 changes: 28 additions & 21 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
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 && \
mkdir -p /data && chown -R subsocial:subsocial /data

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
VOLUME ["/data"]

ENTRYPOINT ["/usr/local/bin/subsocial-collator"]
ENTRYPOINT ["/usr/local/bin/subsocial-collator"]
15 changes: 15 additions & 0 deletions scripts/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down

0 comments on commit c94621d

Please sign in to comment.