Free Web Development Online Tutorials, Learn to Code
Docker container in core, docker core tutorial, docker build application

Docker Core Example

In this tutorial you will learn how to use docker container in core.

docker for core

What is Docker?

Docker is the container technology, which helps in operating system level virtualization to move software product from one environment to another environment, like development to staging or production.

We need to download Docker and install in local machine before we can use it with core application.

After Docker installation, you will be able to see Docker icon on windows task bar, just click that icon to see the menu like example below, make sure Docker Desktop is running, if not start the docker, you acan restart, change container, check for update so on.

docker desktop

What problem is solved by Docker?

Before we start using Docker we need to understand what problem does it solve and how?

Just think, how we deploy software product from development environment to staging, and then from staging to production.

Often we experience a situation, when application stops working after deployment, because of various reasons, sometimes missing a file, sometimes-wrong version of file etc.

Now, using Docker, we need not to worry about any of above situation, because in local environment we can run the application on Docker, and then just ship the container to any environment, so all files will remain same inside the container, it will work perfectly the same way on any different environment.

In Docker, there are two type of container setting for different OS. Windows and Linux.

How to use Docker in Core

To use docker container in core project we need to add a dockerfile in the project.

You can either add at the time of creating project or if want to add in any existing project,

This is how the content will look like in a dockerfile, you can edit or add anything, Everything is considered as command.

FROM AS base
FROM AS build
WORKDIR /docker/src
COPY ["DockerWeb.csproj", ""]
RUN dotnet restore "./DockerWeb.csproj"
COPY . .
WORKDIR "/docker/src/."
RUN dotnet build "DockerWeb.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DockerWeb.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /docker/app
COPY --from=DockerWeb/bin/Release/netcoreapp3.1/publish /app/publish .
ENTRYPOINT ["dotnet", "DockerWeb.dll"]

Everything you see in above dockerfile, are commands. We can change them as per need, let's understand few basic command.

Before we add your .NET Core app to the Docker image, we should publish the project, so we know if there is any compilation error, and also the latest change will be updated.

Just run the following command in visual studio command prompt, first go to current folder location

 CD G:\RND\DockerWeb>
dotnet publish -c Release

Once you run the above command, your project will be compiled and published under folder path below.


now check, if all deployable files are created successfully, just run the command

dir bin\Release\netcoreapp3.1\publish

You should be able to see all deployable files are created successfully like example below

   Directory of G:\RND\DockerWeb\bin\Release\netcoreapp3.1\publish
04-02-2020  22:13    <DIR>          .
04-02-2020  22:13    <DIR>          ..
04-02-2020  18:56               162 appsettings.Development.json
04-02-2020  18:56               192 appsettings.json
04-02-2020  22:12           107,130 DockerWeb.deps.json
04-02-2020  22:12             8,704 DockerWeb.dll
04-02-2020  22:12           169,984 DockerWeb.exe
04-02-2020  22:12             2,072 DockerWeb.pdb
04-02-2020  22:12               224 DockerWeb.runtimeconfig.json
04-02-2020  22:12            36,352 DockerWeb.Views.dll
04-02-2020  22:12             3,616 DockerWeb.Views.pdb
04-02-2020  22:14               553 web.config
04-02-2020  22:12    <DIR>          wwwroot
              10 File(s)        328,989 bytes
               3 Dir(s)  150,130,073,600 bytes free

Check the Dockerfile

Though earlier, I have shared an example of how the default Dockerfile will look like, but now we will look at each parameter value that we need to create a docker image.

"dockerfile" is basically a text file without extension

First, Open developer command prompt and go to current project directory.

There are many command for docker image building, through parent command remain docker image , here are all commands with options you can experiment with!

FROM AS runtime
COPY bin/Release/netcoreapp3.1/publish/DockerWeb.dll ./
ENTRYPOINT ["dotnet", "DockerWeb.dll"]

docker build command

So when we run the docker build command, it actually execute each command we have specified in dockerfile.

docker build -t myimage -f Dockerfile .
docker build -t myimage 

After -f Dockerfile there is . (dot) in that command, indicates all command in the docker file to be executed.

docker run command

$ docker run -d -p 8080:80 --name DockerWeb myimage

Manual Build Process

Open command window (.NET Core CLI)

Step 1: Go to working folder like G:\RND\DockerWeb
CD G:\RND\DockerWeb
Step 2: Execute dotnet publish command

In below command "published" is the name of directory, after executing the command, you will see one folder is created with name "published" with all required files and folder in it.

dotnet publish -c Release -o published
Step 3: Check if app is running successfully

Run the dotnet command for the main dll which has entry point, in following example dockerweb.dll is the name of my dll, your dll name may be different.

dotnet published\dockerweb.dll

Now you see in command prompt, it will show the url http://localhost:5000, where your app is running, just open the url in browser to confirm.

Step 4: Build Docker Container with docker build .

We have already discussed earlier in this post that docker build . command will execute each command we specify in "dockerfile".

However, you may experience different type of errors while executing all commands in "dockerfile", so let’s start with small set of commands and execute, so we know what type of error comes, and what would be the solution.

WORKDIR is your current working directory, like "dockerweb", you need to set your directory name.

Let's write following few line of code in your "dockerfile". and execute docker build . Command Prompt

FROM AS runtime
WORKDIR /dockerweb
COPY published/dockerweb.dll ./
ENTRYPOINT ["dotnet", "dockerweb.dll"]

As you can see the result in below window, the container is build successfully after executing docker build .

G:\RND\DockerWeb>docker build .
Sending build context to Docker daemon 9.111MB
Step 1/4 : FROM AS runtime
---> 33689ed40fe9
Step 2/4 : WORKDIR /dockerweb
---> Using cache
---> cb0fb5e1a0f0
Step 3/4 : COPY published/dockerweb.dll ./
---> Using cache
---> 63e9f384d65f
Step 4/4 : ENTRYPOINT ["dotnet", "dockerweb.dll"]
---> Running in b3803447be33
Removing intermediate container b3803447be33
---> 445622b39675
Successfully built 445622b39675

Run the application from docker container

docker run -it --rm --name aspnetcore-project dockerweb

At this point you may get error "The virtual machine could not be started because a required feature is not installed.", if you are using windows 10, Enabling Hyper-V for use on Windows 10

Now, so far we have seen how to build docker container in windows 10, So far everything is running smooth, now i try alter the "dockerfile" add some more command option to create image, will update this post soon.

How to add bundleconfig in Core
ASP.NET MVC Interview Questions Answers

online courses
Asp.Net Core C# Examples | Join Asp.Net MVC Course