Creating a Dockerfile

I will break down and explain what is inside a basic default Dockerfile for an ASP.NET Core application that will be containerized.

I took the Dockerfile sample from here: https://docs.docker.com/engine/examples/dotnetcore/#create-a-dockerfile-for-an-aspnet-core-application

I will put comments with a "#" sign with my explanations. The image "microsoft/aspnetcore-build:2.0" information and location can be found here: https://hub.docker.com/r/microsoft/aspnetcore-build/

https://hub.docker.com is basically like GitHub, but for Docker images.  You cannot store Docker images in a regular GitHub (or similar) repo, they have a special format they are stored in and therefore require something like Docker Hub or Azure Container Registry.

You will require some container knowledge to understand below. Also note, below is a multi-stage build Dockerfile. For full documentation on those see here: https://docs.docker.com/develop/develop-images/multistage-build/

#################################################################
Build Phase 1
#################################################################
# The FROM command will set the "base image" and sets up a build stage
# This base contains the .NET Core SDK, .NET Core libraries, Node.js, Bower, and Gulp  
# This particular image contents can be viewed here: https://hub.docker.com/r/microsoft/aspnetcore/
# The format of images are - imagename:tag where tag is the version
FROM microsoft/aspnetcore-build:2.0 AS build-env

# Changes the working directory inside the container to /app, same as 'cd' for change directory
WORKDIR /app

# Copy any files ending in .csproj (from root local directory where Dockerfile lives).. 
# ..to the current container directory which is /app
COPY *.csproj ./

# Run the command 'dotnet restore' inside the container
RUN dotnet restore

# Copy everything else from root local directory to container's /app directory and build
COPY . ./

# Run the command 'dotnet publish -c Release -o out' inside the container
# Publishes contents to the app/out folder that can be utilized in the next build stage
RUN dotnet publish -c Release -o out

###################################################################
Build Phase 2
###################################################################
# The second FROM command sets up a second build stage. 
# Only the final build stage "counts" and anything pulled into it from previous stages. 
# If nothing is pulled in from previous build stages, then they get scrapped completely.. 
# ..and never contribute to final image. Build run-time image
# This image contains .NET Core and its libraries
FROM microsoft/aspnetcore:2.0

# Changes the working directory inside the container to /app 
# We need to do this because we are in a new build stage
WORKDIR /app

# Pulls in all files from the '/app/out' from Build Stage 1
COPY --from=build-env /app/out .

# This command will always be executed when the container starts
ENTRYPOINT ["dotnet", "aspnetapp.dll"]

Comments

Popular posts from this blog

Electron JS, Visual Studio Code, and SQL Operations Studio

Unity vs. Unreal Engine

Free & Paid Resources for Getting Started in IT