Slight restructure, Containerfile now does npm build properly

This commit is contained in:
Imbus 2023-10-18 19:05:10 +02:00
parent 3bc6e6928e
commit e5dfc47ccf
8 changed files with 52 additions and 33 deletions

6
.containerignore Normal file
View file

@ -0,0 +1,6 @@
# Server files
/server/target
# Client files
/client/node_modules
/client/dist

42
Containerfile Normal file
View file

@ -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"]

View file

@ -1,6 +1,6 @@
# FrostByte forum # FrostByte forum
``` ```bash
# Local backend server (Actix-web with sqlx::sqlite) # Local backend server (Actix-web with sqlx::sqlite)
cd server cd server
cargo run cargo run

View file

@ -1,2 +0,0 @@
dist
node_modules

View file

@ -5,7 +5,7 @@ import { qrcode } from 'vite-plugin-qrcode'
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({
build: { build: {
outDir: '../server/public' // outDir: '../server/public' // Override default outDir('dist')
}, },
plugins: [react(), qrcode()], plugins: [react(), qrcode()],
server: { server: {

View file

@ -6,8 +6,8 @@ build-client:
cd client && npm run build cd client && npm run build
# npm run build places the build in the server/public directory # npm run build places the build in the server/public directory
build-container: build-client build-container:
cd server && podman build -t fb-server . podman build -t fb-server .
start-release: build-container start-release: build-container
podman container rm -f frostbyte podman container rm -f frostbyte

View file

@ -1 +0,0 @@
target

View file

@ -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"]