Skip to content

Instantly share code, notes, and snippets.

@mikerochip
Last active March 19, 2023 22:34
Show Gist options
  • Save mikerochip/0705f53d82ce3068d19fc6251ce2b34a to your computer and use it in GitHub Desktop.
Save mikerochip/0705f53d82ce3068d19fc6251ce2b34a to your computer and use it in GitHub Desktop.
ASP.NET 7 Docker
# directories
**/bin/
**/obj/
**/out/
# files
Dockerfile*
**/*.md
docker build --pull --build-arg AspEnvironment=Development -t asp-api-test-dev .
docker run --rm -it -p 8000:80 asp-api-test-dev
docker build --pull --build-arg AspEnvironment=Production -t asp-api-test-prod .
docker run --rm -it -p 8000:80 asp-api-test-prod
# Reference: https://github.com/dotnet/dotnet-docker/blob/main/samples/aspnetapp/Dockerfile
# with ENV and ARG modifications
# build stage
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS sdk
WORKDIR /source
# copy csproj and restore as distinct layers
COPY *.csproj .
RUN dotnet restore --use-current-runtime
# copy everything else and build app
COPY . .
RUN dotnet publish -c Release -o /app --use-current-runtime --self-contained false --no-restore
# run stage
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app
COPY --from=sdk /app .
# ASP env vars must exist here since this is the run stage and these are runtime env vars
ARG AspEnvironment=Development
ENV ASPNETCORE_ENVIRONMENT=$AspEnvironment
RUN echo "ASPNETCORE_ENVIRONMENT=$AspEnvironment"
ENTRYPOINT ["dotnet", "ApiTest.dll"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment