build: Split dockerfile into more layers for better caching

This commit is contained in:
Cucumberrbob 2025-03-12 11:58:01 +00:00
parent aa6e6c7df5
commit 9ac87727c7
No known key found for this signature in database
GPG key ID: 2B935C47401C3614
5 changed files with 51 additions and 39 deletions

View file

@ -38,3 +38,4 @@ server/RdtClient.Web/wwwroot/
build/ build/
publish/data publish/data
.github/

View file

@ -1,42 +1,61 @@
# Stage 1 - Build the frontend # Stage 1 - Install Frontend Dependencies
FROM node:18-alpine3.18 AS node-build-env FROM node:18-alpine3.18 AS node-deps
ARG TARGETPLATFORM ARG TARGETPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64} ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64}
ARG BUILDPLATFORM ARG BUILDPLATFORM
ENV BUILDPLATFORM=${BUILDPLATFORM:-linux/amd64} ENV BUILDPLATFORM=${BUILDPLATFORM:-linux/amd64}
RUN mkdir /appclient RUN mkdir -p /appclient/client
WORKDIR /appclient WORKDIR /appclient/client
RUN apk add --no-cache git python3 py3-pip make g++ COPY client/package.json client/package-lock.json ./
COPY client ./client
COPY root ./root
RUN \ RUN \
cd client && \ echo "**** Installing Frontend Dependencies ****" && \
echo "**** Building Code ****" && \ npm ci
npm ci && \
npx ng build --output-path=out
RUN ls -FCla /appclient/root # Stage 2 - Build Frontend
FROM node-deps AS node-build-env
# Stage 2 - Build the backend WORKDIR /appclient/client
FROM mcr.microsoft.com/dotnet/sdk:9.0-bookworm-slim-amd64 AS dotnet-build-env
COPY client .
RUN \
echo "**** Building Frontend ****" && \
npm run build -- --output-path=out
# Stage 3 - Install Backend Dependencies
FROM mcr.microsoft.com/dotnet/sdk:9.0-alpine AS dotnet-deps
ARG TARGETPLATFORM ARG TARGETPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64} ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64}
ARG BUILDPLATFORM ARG BUILDPLATFORM
ENV BUILDPLATFORM=${BUILDPLATFORM:-linux/amd64} ENV BUILDPLATFORM=${BUILDPLATFORM:-linux/amd64}
RUN mkdir /appserver RUN mkdir -p /appserver/server
WORKDIR /appserver WORKDIR /appserver/server
COPY server/RdtClient.sln ./
COPY server/RdtClient.Data/RdtClient.Data.csproj RdtClient.Data/RdtClient.Data.csproj
COPY server/RdtClient.Service/RdtClient.Service.csproj RdtClient.Service/RdtClient.Service.csproj
COPY server/RdtClient.Web/RdtClient.Web.csproj RdtClient.Web/RdtClient.Web.csproj
COPY server ./server
RUN \ RUN \
echo "**** Building Source Code for $TARGETPLATFORM on $BUILDPLATFORM ****" && \ echo "**** Installing Backend Dependencies for $TARGETPLATFORM on $BUILDPLATFORM ****" && \
cd server && \ dotnet restore --no-cache RdtClient.Web/RdtClient.Web.csproj
dotnet restore --no-cache RdtClient.sln && dotnet publish --no-restore -c Release -o out ;
# Stage 3 - Build runtime image # Stage 4 - Build the backend
FROM dotnet-deps AS dotnet-build-env
WORKDIR /appserver/server
COPY server .
RUN \
echo "**** Building Backend Code for $TARGETPLATFORM on $BUILDPLATFORM ****" && \
dotnet publish RdtClient.Web/RdtClient.Web.csproj --no-restore -c Release -o out ;
# Stage 5 - Install dotnet runtime
FROM mcr.microsoft.com/dotnet/aspnet:9.0-alpine AS dotnet-runtime
# Stage 6 - Build runtime image
FROM ghcr.io/linuxserver/baseimage-alpine:3.20 FROM ghcr.io/linuxserver/baseimage-alpine:3.20
ARG TARGETPLATFORM ARG TARGETPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64} ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64}
@ -59,21 +78,10 @@ RUN \
echo "**** Updating package information ****" && \ echo "**** Updating package information ****" && \
apk update && \ apk update && \
echo "**** Install pre-reqs ****" && \ echo "**** Install pre-reqs ****" && \
apk add bash icu-libs krb5-libs libgcc libintl libssl3 libstdc++ zlib && \ apk add bash icu-libs krb5-libs libgcc libintl libssl3 libstdc++ zlib
echo "**** Installing dotnet ****" && \
mkdir -p /usr/share/dotnet
RUN \ COPY --from=dotnet-runtime /usr/share/dotnet /usr/share/dotnet
if [ "$TARGETPLATFORM" = "linux/arm/v7" ] ; then \ ENV PATH="$PATH:/usr/share/dotnet"
wget https://download.visualstudio.microsoft.com/download/pr/59a041e1-921e-405e-8092-95333f80f9ca/63e83e3feb70e05ca05ed5db3c579be2/aspnetcore-runtime-9.0.0-linux-musl-arm.tar.gz && \
tar zxf aspnetcore-runtime-9.0.0-linux-musl-arm.tar.gz -C /usr/share/dotnet ; \
elif [ "$TARGETPLATFORM" = "linux/arm64" ] ; then \
wget https://download.visualstudio.microsoft.com/download/pr/e137f557-83cb-4f55-b1c8-e5f59ccd3cba/b8ba6f2ab96d0961757b71b00c201f31/aspnetcore-runtime-9.0.0-linux-musl-arm64.tar.gz && \
tar zxf aspnetcore-runtime-9.0.0-linux-musl-arm64.tar.gz -C /usr/share/dotnet ; \
else \
wget https://download.visualstudio.microsoft.com/download/pr/86d7a513-fe71-4f37-b9ec-fdcf5566cce8/e72574fc82d7496c73a61f411d967d8e/aspnetcore-runtime-9.0.0-linux-musl-x64.tar.gz && \
tar zxf aspnetcore-runtime-9.0.0-linux-musl-x64.tar.gz -C /usr/share/dotnet ; \
fi
RUN \ RUN \
echo "**** Setting permissions ****" && \ echo "**** Setting permissions ****" && \
@ -83,13 +91,11 @@ RUN \
/var/cache/apk/* \ /var/cache/apk/* \
/var/tmp/* || true /var/tmp/* || true
ENV PATH "$PATH:/usr/share/dotnet"
# Copy files for app # Copy files for app
WORKDIR /app WORKDIR /app
COPY --from=dotnet-build-env /appserver/server/out . COPY --from=dotnet-build-env /appserver/server/out .
COPY --from=node-build-env /appclient/client/out/browser ./wwwroot COPY --from=node-build-env /appclient/client/out/browser ./wwwroot
COPY --from=node-build-env /appclient/root/ / COPY ./root/ /
# ports and volumes # ports and volumes
EXPOSE 6500 EXPOSE 6500

2
client/.dockerignore Normal file
View file

@ -0,0 +1,2 @@
node_modules
.angular

2
server/.dockerignore Normal file
View file

@ -0,0 +1,2 @@
**/bin
**/obj

View file

@ -0,0 +1 @@
wwwroot