diff --git a/.containerignore b/.containerignore new file mode 100644 index 0000000..feca391 --- /dev/null +++ b/.containerignore @@ -0,0 +1,6 @@ +# Server files +/server/target + +# Client files +/client/node_modules +/client/dist diff --git a/Containerfile b/Containerfile new file mode 100644 index 0000000..257da87 --- /dev/null +++ b/Containerfile @@ -0,0 +1,42 @@ +# Client/frontend build in an isolated stage +# We use node:latest as the base image. +# Essentially we build the frontend SPA with vite and +# make it available in the public directory. +FROM docker.io/node:latest as client +WORKDIR /build +ADD ./client /build +RUN npm install +RUN npm run build + +# Builds the server in an isolated stage +# We use musl to get a truly static binary +# that runs cleanly without depending on glibc. +FROM docker.io/rust:latest as builder +WORKDIR /build +ADD ./server /build +RUN apt update +RUN apt install musl musl-dev musl-tools -y +RUN rustup target add x86_64-unknown-linux-musl +RUN cargo build --target x86_64-unknown-linux-musl --release + +# Final stage, copy the server binary and the frontend build +# This stage is the actual container, and is based on alpine +# a minimal linux distribution resulting in a small image. +FROM docker.io/alpine:latest as runner + +# Add a non-root user for running the server +RUN addgroup -S user && adduser -S user -G user + +WORKDIR /runner +# Copy the server binary and the public directory +COPY --from=builder /build/target/x86_64-unknown-linux-musl/release/server /runner/server +# Copy the frontend SPA build into public +COPY --from=client /build/dist /runner/public + +# Make sure the user can access the files +RUN chown -R user:user /runner +USER user + +# Run the server +EXPOSE 8080 +CMD ["./server"] diff --git a/README.md b/README.md index 35e0940..bd5a205 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # FrostByte forum -``` +```bash # Local backend server (Actix-web with sqlx::sqlite) cd server cargo run diff --git a/client/.containerignore b/client/.containerignore deleted file mode 100644 index de4d1f0..0000000 --- a/client/.containerignore +++ /dev/null @@ -1,2 +0,0 @@ -dist -node_modules diff --git a/client/vite.config.ts b/client/vite.config.ts index 8c01708..e2bcb3e 100644 --- a/client/vite.config.ts +++ b/client/vite.config.ts @@ -5,7 +5,7 @@ import { qrcode } from 'vite-plugin-qrcode' // https://vitejs.dev/config/ export default defineConfig({ build: { - outDir: '../server/public' + // outDir: '../server/public' // Override default outDir('dist') }, plugins: [react(), qrcode()], server: { diff --git a/justfile b/justfile index a16ef15..7b5af42 100644 --- a/justfile +++ b/justfile @@ -6,8 +6,8 @@ build-client: cd client && npm run build # npm run build places the build in the server/public directory -build-container: build-client - cd server && podman build -t fb-server . +build-container: + podman build -t fb-server . start-release: build-container podman container rm -f frostbyte diff --git a/server/.containerignore b/server/.containerignore deleted file mode 100644 index 1de5659..0000000 --- a/server/.containerignore +++ /dev/null @@ -1 +0,0 @@ -target \ No newline at end of file diff --git a/server/Containerfile b/server/Containerfile deleted file mode 100644 index fa91c2b..0000000 --- a/server/Containerfile +++ /dev/null @@ -1,26 +0,0 @@ -# The rust:latest-alpine uses musl libc -FROM docker.io/rust:latest as builder -WORKDIR /build -ADD . /build -RUN apt update -RUN apt install musl musl-dev musl-tools -y -RUN rustup target add x86_64-unknown-linux-musl -RUN cargo build --target x86_64-unknown-linux-musl --release - -FROM docker.io/alpine:latest as runner - -# Add a non-root user for running the server -RUN addgroup -S user && adduser -S user -G user - -# Copy the server binary and the public directory -WORKDIR /runner -COPY --from=builder /build/target/x86_64-unknown-linux-musl/release/server /runner/server -COPY --from=builder /build/public /runner/public - -# Make sure the user can access the files -RUN chown -R user:user /runner -USER user - -# Run the server -EXPOSE 8080 -CMD ["./server"]