Monday, September 28, 2020

Updating Dockerfile and rebuilding images using VisualStudio

 In the previous post we saw on how to create a .Net Core console App and use Visual Studio to automatically add Dockerfile to the project. In this post, we will see how to change the Dockerfile created by Visual Studio and rebuild the Docker image with the modified file.

Once the Dockerfile is created we can manually exit the Docker file Add/Modify steps and rebuild the Docker image.

In this sample, we will add proxy settings to enable package restore in a Corporate firewall environment.

FROM mcr.microsoft.com/dotnet/core/runtime:3.1-nanoserver-1803 AS base

WORKDIR /app 

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1803 AS build

WORKDIR /src

COPY ["HelloExe.csproj", ""]

ARG HTTP_PROXY="http://<username>:<password>@<proxyserver>:8080"

ARG HTTPS_PROXY="http://<username>:<password>@<proxyserver>:8080"

RUN dotnet restore "./HelloExe.csproj"

COPY . .

WORKDIR "/src/."

RUN dotnet build "HelloExe.csproj" -c Release -o /app/build 

FROM build AS publish

RUN dotnet publish "HelloExe.csproj" -c Release -o /app/publish 

FROM base AS final

WORKDIR /app

COPY --from=publish /app/publish .

ENTRYPOINT ["dotnet", "HelloExe.dll"]

Save the Dockerfile.
Right-click the Dockerfile and select option Build Docker Image

This will re-build the docker image with the modified docker file. We can then list the image and run the container as follows.

C:\>docker image ls
REPOSITORY                              TAG                   IMAGE ID            CREATED             SIZE
helloexe                                latest                b8d30d26c72a        28 seconds ago      448MB

C:\>docker run helloexe:latest
Hello World from .Net Core


Search Flipkart Products:
Flipkart.com

No comments: