From 7eeb14fe9c0d13ec60ae1600d1c8ed4649ea92a8 Mon Sep 17 00:00:00 2001 From: Casper Thygesen Date: Wed, 24 Jan 2024 10:33:11 +0000 Subject: [PATCH] fix docker buildx --- src/.dockerignore => .dockerignore | 17 +++++++++++++ dockerfiles/Dockerfile | 31 +++++++++++++++++++++++ src/Cheetah.WebApi/.dockerignore | 26 ------------------- src/Cheetah.WebApi/Dockerfile | 40 ------------------------------ src/docker-compose.yml | 7 ++++-- 5 files changed, 53 insertions(+), 68 deletions(-) rename src/.dockerignore => .dockerignore (64%) create mode 100644 dockerfiles/Dockerfile delete mode 100644 src/Cheetah.WebApi/.dockerignore delete mode 100644 src/Cheetah.WebApi/Dockerfile diff --git a/src/.dockerignore b/.dockerignore similarity index 64% rename from src/.dockerignore rename to .dockerignore index d16e1a4..58f1d7a 100644 --- a/src/.dockerignore +++ b/.dockerignore @@ -24,3 +24,20 @@ **/.nuget LICENSE README.md +.vs/ +.vscode/ +**/bin/ +**/obj/ +**/.nuget/ +secrets.dev.yaml +values.dev.yaml +LICENSE +README.md +docker-compose* +Dockerfile* +*.*proj.user +.classpath +.dockerignore +.env +.git +.gitignore \ No newline at end of file diff --git a/dockerfiles/Dockerfile b/dockerfiles/Dockerfile new file mode 100644 index 0000000..4934fa6 --- /dev/null +++ b/dockerfiles/Dockerfile @@ -0,0 +1,31 @@ +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base +WORKDIR /app + +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +WORKDIR /build + +COPY src/NuGet-CI.Config ./NuGet.Config +COPY src/*/*.csproj ./ +RUN for file in $(ls *.csproj); do mkdir -p ${file%.*}/ && mv $file ${file%.*}/; done + +# This is only needed because swagger uses .NET 7 +ENV DOTNET_ROLL_FORWARD=LatestMajor + +ARG projectFile +RUN test -n "$projectFile" +RUN --mount=type=secret,id=GITHUB_TOKEN \ + GITHUB_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" \ + dotnet restore --packages $HOME/app/.nuget "$projectFile" + +COPY ./src ./ + +RUN dotnet publish "$projectFile" -c Release -o /app/publish --no-restore --source $HOME/app/.nuget + +FROM base AS final + +WORKDIR /app +COPY --from=build /app/publish . + +# 64198 +USER $APP_UID +ENTRYPOINT ["dotnet", "Cheetah.WebApi.dll"] diff --git a/src/Cheetah.WebApi/.dockerignore b/src/Cheetah.WebApi/.dockerignore deleted file mode 100644 index 20da0a0..0000000 --- a/src/Cheetah.WebApi/.dockerignore +++ /dev/null @@ -1,26 +0,0 @@ -**/.classpath -**/.dockerignore -**/.env -**/.git -**/.gitignore -**/.project -**/.settings -**/.toolstarget -**/.vs -**/.vscode -**/*.*proj.user -**/*.dbmdl -**/*.jfm -**/azds.yaml -**/bin -**/charts -**/docker-compose* -**/Dockerfile* -**/node_modules -**/npm-debug.log -**/obj -**/secrets.dev.yaml -**/values.dev.yaml -**/.nuget -LICENSE -README.md \ No newline at end of file diff --git a/src/Cheetah.WebApi/Dockerfile b/src/Cheetah.WebApi/Dockerfile deleted file mode 100644 index 2535336..0000000 --- a/src/Cheetah.WebApi/Dockerfile +++ /dev/null @@ -1,40 +0,0 @@ -#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. - -FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base -WORKDIR /app -EXPOSE 80 -EXPOSE 443 - -FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build - -ARG GITHUB_ACTOR -ARG GITHUB_TOKEN - -WORKDIR /src -COPY "src/NuGet-CI.Config" "NuGet.config" -COPY "src/.config/" ".config/" -COPY ["src/Cheetah.WebApi/Cheetah.WebApi.csproj", "Cheetah.WebApi/"] - -# This is only needed because swagger uses .NET 7 -ENV DOTNET_ROLL_FORWARD=LatestMajor - -RUN --mount=type=secret,id=GITHUB_TOKEN \ - GITHUB_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" \ - dotnet restore "Cheetah.WebApi/Cheetah.WebApi.csproj" - -COPY src . -WORKDIR "/src/Cheetah.WebApi" -RUN dotnet build "Cheetah.WebApi.csproj" -c Release -o /app/build - -FROM build AS publish -RUN dotnet publish "Cheetah.WebApi.csproj" -c Release -o /app/publish - -FROM base AS final -ENV COMPlus_EnableDiagnostics=0 - -WORKDIR /app -COPY --from=publish /app/publish . - -# 64198 -USER $APP_UID -ENTRYPOINT ["dotnet", "Cheetah.WebApi.dll"] \ No newline at end of file diff --git a/src/docker-compose.yml b/src/docker-compose.yml index dd1139d..f50b562 100644 --- a/src/docker-compose.yml +++ b/src/docker-compose.yml @@ -9,8 +9,12 @@ services: test: ["CMD", "curl", "-f", "http://cheetahwebapi:80/health"] build: context: ../ - dockerfile: src/Cheetah.WebApi/Dockerfile + dockerfile: dockerfiles/Dockerfile + args: + - projectFile=Cheetah.WebApi/Cheetah.WebApi.csproj + - assemblyName=Cheetah.WebApi secrets: + # Nuget restore outside Visual Studio - GITHUB_ACTOR - GITHUB_TOKEN environment: @@ -29,7 +33,6 @@ services: - "KafkaProducerConfig__Topic=cheetahwebapi" - "KafkaConsumerConfig__Topic=cheetahwebapi" - "KafkaConsumerConfig__ConsumerName=cheetahwebapi" - networks: - cheetah-infrastructure ports: