I have the following Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build-env
WORKDIR /app
EXPOSE 80
ARG ASPNETCORE_ENVIRONMENT=Production
ENV ASPNETCORE_ENVIRONMENT=${ASPNETCORE_ENVIRONMENT}
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "OrderAPI.dll"]
All I want to do is when during build I can specify the environment. Right now I am able to build and then run in a specific environment using -e ASPNETCORE_ENVIRONMENT but I want the build to be done in a specific environment.
When I look at how others have done it using shell scripts etc. Is there a simpler way to do it within the Dockerfile. For my other dockerfiles I just pass in the arguments but unsure with appsettings.
I've added
ARG ASPNETCORE_ENVIRONMENT=Production
ENV ASPNETCORE_ENVIRONMENT=${ASPNETCORE_ENVIRONMENT}
But running the below still runs in production (of course i can change it in the dockerfile but don't want to do that)
docker build --build-arg ASPNETCORE_ENVIRONMENT=Development -t order-api:latest .
Your ENV statement is in the build part of the Dockerfile, so it's not included in the final image. The stages in a Dockerfile are very separate and pretty much nothing carries over from one stage to the next.
Move it to the final part and it should be set when you run the container.
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build-env
WORKDIR /app
EXPOSE 80
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:7.0
ARG ASPNETCORE_ENVIRONMENT=Production
ENV ASPNETCORE_ENVIRONMENT=${ASPNETCORE_ENVIRONMENT}
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "OrderAPI.dll"]