feat: docker

This commit is contained in:
trev 2025-03-13 05:03:45 -04:00
parent da993d9ef5
commit 4e9a3de12a
2 changed files with 29 additions and 62 deletions

View File

@ -1,49 +1,28 @@
## BUF BUILD
FROM bufbuild/buf:1.50.0 AS buf
WORKDIR /buf
# Nix builder
FROM nixos/nix:latest AS builder
# Create client and server services
COPY buf.yaml buf.gen.yaml base.openapi.yaml ./
COPY proto ./proto
RUN buf generate
# Copy our source and setup our working dir.
COPY . /tmp/build
WORKDIR /tmp/build
# Build our Nix environment
RUN nix \
--extra-experimental-features "nix-command flakes" \
--option filter-syscalls false \
build
## CLIENT BUILD
FROM node:22-alpine AS client
WORKDIR /client
# Copy the Nix store closure into a directory. The Nix store closure is the
# entire set of Nix store values that we need for our build.
RUN mkdir /tmp/nix-store-closure
RUN cp -R $(nix-store -qR result/) /tmp/nix-store-closure
# Install client dependencies
COPY client/package.json client/package-lock.json ./
RUN npm ci
# Final image is based on scratch. We copy a bunch of Nix dependencies
# but they're fully self-contained so we don't need Nix anymore.
FROM scratch
# Get client source
COPY client .
WORKDIR /app
# Get buf service
COPY --from=buf /buf/client/src/lib/services ./src/lib/services
# Build client
RUN npm run build
## SERVER BUILD
FROM golang:1.23 AS server
WORKDIR /server
# Install server dependencies
COPY server/go.mod server/go.sum ./
RUN go mod download && go mod verify
# Get server source
COPY server .
# Get client build
COPY --from=client /client/build ./client
# Get buf service
COPY --from=buf /buf/server/internal/services ./internal/services
# Build server
RUN go build -v -o /server/main .
CMD ["/server/main"]
# Copy /nix/store
COPY --from=builder /tmp/nix-store-closure /nix/store
COPY --from=builder /tmp/build/result /app
CMD ["/app/bin/app"]

View File

@ -35,6 +35,11 @@
name = "client";
src = gitignore.lib.gitignoreSource ./client;
npmDepsHash = "sha256-hOmZZrCSuHyRQhG6M7Yu5uRLTdCYOL/giT4zUm9iTRE=";
nodejs = pkgs.nodejs_22;
installPhase = ''
cp -r build "$out"
chmod -R u+w "$out"
'';
};
in
@ -123,26 +128,9 @@
pname = "trevstack";
version = "1.0";
src = gitignore.lib.gitignoreSource ./server;
vendorHash = "";
vendorHash = "sha256-PE9ns1W+7/ZBBxb7+96aXqBTzpDo5tGcfnCXAV8vp8E=";
buildInputs = with pkgs; [
# Go backend
go
gotools
gopls
# Protobuf middleware
buf
protoc-gen-go
protoc-gen-connect-go
protoc-gen-es
protoc-gen-connect-openapi
# Svelte frontend
nodejs_22
];
configurePhase = ''
preBuild = ''
cp -r ${client} client
'';
};