From 5cbc83043975c882ecf2be16b221edb970eb3474 Mon Sep 17 00:00:00 2001 From: Ethan Girouard Date: Sat, 30 Dec 2023 15:19:02 -0500 Subject: [PATCH] Add Dockerfile --- .dockerignore | 9 +++++++ Dockerfile | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0c102f2 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,9 @@ +# Ignore everything +* + +# Except: +!/assets +!/src +!/style +!/Cargo.lock +!/Cargo.toml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..17a8624 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,70 @@ +FROM rust:slim AS builder + +WORKDIR /app + +# Select the nightly toolchain and add needed targets +RUN rustup toolchain install nightly +RUN rustup default nightly +RUN rustup target add wasm32-unknown-unknown +RUN rustup target add x86_64-unknown-linux-musl + +# Install a few dependencies +RUN set -eux; \ + apt-get update; \ + apt-get install -y --no-install-recommends \ + libssl-dev \ + pkg-config \ + npm \ + musl-tools; \ + rm -rf /var/lib/apt/lists/* + +RUN cargo install cargo-leptos +RUN npm install tailwindcss -g + +# Copy project dependency manifests +COPY Cargo.toml Cargo.lock /app/ + +# Add the target to Cargo.toml so we can statically link: +RUN echo 'bin-target-triple = "x86_64-unknown-linux-musl"' >> Cargo.toml + +# Create dummy files to force cargo to build the dependencies +RUN mkdir /app/src && mkdir /app/style && mkdir /app/assets && \ + echo "fn main() {}" > /app/src/main.rs && \ + touch /app/src/lib.rs && \ + touch /app/style/main.scss + +# Prebuild dependencies +RUN cargo-leptos build --release --precompress + +RUN rm -rf /app/src /app/style /app/assets +COPY style /app/style + +# Minify CSS +RUN npx tailwindcss -i /app/style/main.scss -o /app/style/main.scss --minify + +COPY assets /app/assets +COPY src /app/src + +# Touch files to force rebuild +RUN touch /app/src/main.rs && touch /app/src/lib.rs + +# Actually build the binary +RUN cargo-leptos build --release --precompress + +# Build the final image +FROM scratch + +LABEL license="MIT" +LABEL description="LibreTunes, an open-source browser audio player and \ +library manager built for collaborative listening." + +# Copy the binary and the compressed assets to the "site root" +COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/libretunes /libretunes +COPY --from=builder /app/target/site /target/site + +# Configure Leptos settings +ENV LEPTOS_SITE_ADDR=0.0.0.0:3000 +ENV LEPTOS_SITE_ROOT=/site +EXPOSE 3000 + +ENTRYPOINT [ "/libretunes" ]