AZ-204: Developing Solutions for Microsoft Azure

Managing Container Images in Azure Container Registry

Exploring Elements of a Dockerfile

A Dockerfile is a script composed of instructions that automate the creation of a Docker image. Each command in the Dockerfile defines a specific step to set up an environment where your application can run smoothly. In this guide, we will walk you through the essential elements of a Dockerfile used to containerize an ASP.NET Core application.

Step 1: Specify the Base Image

The Dockerfile begins by setting the base image using the ASP.NET Core runtime. The FROM instruction selects the base image, while the WORKDIR and EXPOSE commands establish the working directory and expose the appropriate port for container networking.

# Step 1: Use the official ASP.NET Core runtime as the base image
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

In this step, version 6.0 of the ASP.NET Core runtime is pulled from the Microsoft Container Registry. The container’s working directory is set to /app, and port 80 is exposed to handle HTTP traffic.

Note

Ensure that the base image version matches the runtime requirements of your ASP.NET Core application.

Step 2: Set Up the Build Environment

Next, the Dockerfile leverages the .NET SDK image, which is essential for compiling and building your application. By setting the working directory to /src, the container is prepared to receive the application’s source code.

# Step 2: Use the .NET SDK image to build the app
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

Step 3: Restore Dependencies

In this step, the project file (for example, MyWebApp.csproj) is copied from the host to the container. This allows the Docker build process to restore any project dependencies before compiling the application.

# Step 3: Copy the project file and restore any dependencies
COPY ["MyWebApp/MyWebApp.csproj", "MyWebApp/"]
RUN dotnet restore "MyWebApp/MyWebApp.csproj"

The COPY instruction transfers the project file into the container, and the RUN command executes dotnet restore to install the dependencies specified in the project configuration.

Step 4: Build the Application

After dependency restoration, the Dockerfile copies the remaining project files into the container. It then changes the working directory to the application’s folder and builds the project in Release mode. The build output is stored in the /app/build folder within the container.

# Step 4: Copy the remaining project files and build the application
COPY . .
WORKDIR "/src/MyWebApp"
RUN dotnet build "MyWebApp.csproj" -c Release -o /app/build

Step 5: Publish the Application

To create a deployable package, this step publishes the application using the dotnet publish command. The published output is directed to the /app/publish directory, making it ready for production deployment.

# Step 5: Publish the application
FROM build AS publish
RUN dotnet publish "MyWebApp.csproj" -c Release -o /app/publish

Deployment Tip

Publishing the application consolidates all necessary files into a single directory, ensuring that only the compiled output is used in the final Docker image.

Step 6: Create the Final Image

Finally, the Dockerfile constructs the final image. It reuses the initial runtime environment defined in Step 1. The process involves setting the working directory to /app, copying the published application from the previous stage, and defining the container's entry point. When you run the container, the application starts automatically.

# Step 6: Create the final image with the app
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyWebApp.dll"]

This complete Dockerfile ensures that your ASP.NET Core application is built, containerized, and deployed efficiently.

Next Steps

Now that you understand the elements of a Dockerfile, you can further explore how to build a container image and push it to the Azure Container Registry (ACR). ACR Tasks, for example, offer an automated way to build and deploy your container images.

Happy containerizing!

For more information, please refer to the following resources:

Watch Video

Watch video content

Previous
Discovering Azure Container Registry