One of the major features of Microsoft .NET core is its ability to run as a cross platform framework on multiple OS(Linux, Mac, Windows) and on containerized environments. An effort by Microsoft to continue to support containerized environments and enhance CI/CD pipelines. And to help get started, Microsoft provided a number of different docker images available for use based on common use cases. In this post I will talk about working with containers and Dockerfiles in .NET Core.
Essentially, A Dockerfile is a plain text containing set of instructions, each instruction lists the steps required to create a working image.The .NET Dockerfile is no exemption. For example: set an environment variable, copy a file, or run a script. Whenever an instruction is run, a new layer is created on top of the initial Docker base images. Docker base image is the starting point image on which layers are built on (usually consisting of a file System structure with OS configuration) to create all of your container images. For example, in order to run a LAMP stack as a Docker containers, you might use either an Ubuntu 14.04 or CentOS 7, or any of your Linux OS as a base image. Then, you would install Apache, MySQL and PHP on it and the result would be your final LAMP Docker image, which can be run as a container.
Naïve (BASIC) dockerfile sample.
FROM ubuntu:15.04 COPY . /app RUN make /app CMD python /app/app.py
FROM: creates a layer from the ubuntu:15.04 Docker image.
COPY: add’s (copy) files from your project current directory to Docker the host path.
RUN: builds your application with make.
CMD: specifies what command to run within the container.
FROM: The FROM instruction indicates the base image and initialize a new build stage for subsequent instructions and operation. For .NET core apps, it is important that the right root image be selected
use for deploying self-contained deployment apps (compiles into DLLs)
use for deploying .NET Core console apps
use for deploying ASP.NET Core apps (compiles into DLLs and runs app)
– use for building .NET Core (or ASP.NET Core apps) Sets the default URL for apps to http://+:80
COPY: The COPY instruction copies new files or directories from
... COPY . /app
RUN: The RUN instruction will execute any command, on top of the current image and continue to the next instruction. The resulting committed image will be used for the next step in the Dockerfile. .NET core commands like dotnet restore, dotnet build commands are would be executed using the RUN command.
RUN dotnet restore RUN dotnet build
CMD: The CMD commands is to provide defaults for an executing container. There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.
ENTRYPOINT: An Entrypoint allows you to configure a container that will run as an executable.
WORKDIR: The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.
If a relative path is provided, it will be relative to the path of the previous WORKDIR instruction. For example:
WORKDIR /app WORKDIR src WORKDIR assets
The output path Dockerfile would be /app/src/assets.
.NETcore docker file
FROM microsoft/aspnetcore-build:2.1 WORKDIR /app COPY . ./app RUN dotnet restore RUN dotnet build WORKDIR /app/src/MyApponDocker ENTRYPOINT dotnet run
The listen above is a functional .net docker file for a project named. The note above should help explain the files.
BUILDING THE IMAGE
From the root of our sample solution using PowerShell
docker build -t dockerApp .
Docker will now begin to build my image. Downloading and installing several components and packages.
RUNNING THE CONTAINER
After running a successful, build
docker run -p 8080:80 dockerApp
This tells Docker to run the image called dockerApp.
The .NET Core SDK – This is what you need to build .NET Core applications.
The .NET Core Runtime – This is what you need to run .NET Core applications.
Docker for dotnet now enables .net apps to run completely on containerized environments without any code changes.coupled with the advantages that comes with containerization. This post explains some of the common Docker file commands the images used in .NET Core development. Each of the images have a set of specific use-cases, and it’s important you use the right one for your requirements.