Now that Docker is becoming a must have when creating small services and needing them to be resilient and easy to deploy, I wanted to create a simple how-to for using ASP.NET Core 2.2 and a Dockerfile with multiple build steps.
This tutorial assumes you know how to create an ASP.NET Core 2.2 project but for context on how the Dockerfile is written, here is my project structure.
The Dockerfile is short and sweet but there is a lot of things going on and a few gotchas that you may want to clean up before using it as a production Dockerfile.
This chunk from the file tells Docker to use image microsoft/dotnet:2.2-sdk as the build image. We use the “AS build” to reference this point later on in the Docker build process.
WORKDIR is telling the Docker build where to work out because of my project structure I need to start in src to be able to access all my projects.
This tells Docker to copy ALL files from the working directory to a directory in the Docker build pipeline called src
These RUN commands are telling Docker to execute a dotnet restore to pull in all dependencies and restore any packages needed for the application. It is then building the application in Release mode.
Here we are telling Docker to “start” from the build pipeline and allow it to be referenced later as the publish step. We are then telling it to run the dotnet publish command putting the compiled binary(Api.dll) in the /app directory publish pipelines “app” directory.
This snippet is doing pretty much the same thing as the build step with a few minor changes. One being the use of the runtime image instead of the SDK. This allows us to reduce the overall image size by not including all the things needed to compile and build the DLL.
The other difference is ENTRYPOINT. This tells Docker how to start a container running your image.
I think this file could be improve by limiting the files being copied in the build step. Right now we are copying all files which means if your working directory has appsettings.production.json or appsettings.staging.json those will both be copied over as well as anything else environmental specific.
One way to change this would be to pass in Docker parameters and use those to specify what should be copied or better yet might be to avoid having those files copied in the image and allow them to be put in a Docker volume that your application is running on.
I liking having a multi-step process as it allows for smaller images and prevents having bloat where we don’t need it. It does take a bit of fidgeting with it depending on project structure and how to get all the projects/packages to be imported correctly but once done it makes building and testing the application much easier!