Creating a container image using a Dockerfile

Now that we have our web API in place, we can create our Docker container image. We can generate this from Visual Studio directly. To do so, follow these steps:

  1. Right-click the project file in the Visual Studio Solution Explorer. 
  2. Then, select Add | Docker Support:

Creating a Dockerfile
  1. A wizard will open where you can generate a Dockerfile. Here, select Linux.
  2. Visual Studio creates a Dockerfile for you. This defines how to create the container image for your Web API project.
  1. Your Dockerfile will now look as follows:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["PacktACIApp/PacktACIApp.csproj", "PacktACIApp/"]
RUN dotnet restore "PacktACIApp/PacktACIApp.csproj"
COPY . .
WORKDIR "/src/PacktACIApp"
RUN dotnet build "PacktACIApp.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "PacktACIApp.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "PacktACIApp.dll"]
  1. Now, when you run your project from Visual Studio, it will run inside a Docker container. Make sure that Docker is running and click the Docker button on the toolbar:

Running the Dockerfile from Visual Studio
  1. The browser will open once more, just like in the previous step, except now, your app will run in a container.
If you get a Docker Volume sharing is not enabled error when you first run your project, then open PowerShell as an administrator and run the following line of code: docker run --rm -v c:/Users:/data alpine ls /data. Restart Visual Studio and run the project again.

Now that we have created our Dockerfile, the next step is to deploy the image to the Azure Container Registry.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.161.188