Showcasing ASP.NET Core with Docker

Enter your credentials into the popup on the right side of the screen:Creating the projectMake a new folder in a safe directory, this is where our project files will be located.

Once inside VS Code we can open that folder and then bring up the terminal by going into View — Terminal.

There we can type: dotnet new mvc -o DockerTestThis will create a new MVC type project into a directory named “DockerTest”.

Now, type cd DockerTest, to move the terminal into the directory that contains the actual project files and then execute dotnet run.

Terminal outputThe project will now be executed and you can see on the terminal output that it is now running on two different ports: 5000 and 5001.

One of these ports has HTTPS and the other one does not.

Try opening a browser window and paste that address to see the outcome of the template.

Next we will create a new Users.

cshtml file under Views — Home, and we will add another list item in the Views — Shared — _Layout.

cshtml:This tells our program to bind this item to the Home controller and the method Users.

We can now head into the Controllers folder and open the HomeController file:As you can see, you need to add a new method which returns the view that we just created.

We will now need some data to display in this view, we will create an EntityFramework repository with an in memory database.

First, we need to run the following command to install EntityFramework to our project:dotnet add package Microsoft.


InMemoryNow we will create a folder named Entities at the root of our project, and inside that folder we will create two files: User.

cs and ApplicationDbContext.

csThe User class will be the entity that will be mapped to our temporary database, which will be accessed by ApplicationDbContext.

Now, we need to configure our DbContext in the ConfigureServices method in the Startup.

cs file:The line we want to add is number 11 in the above code snippet.

Now we want to add some seed data to our test database so that we can see something when we access out newly created users page:As you can see, we overrided a method called OnModelCreating inside our ApplicationDbContext and added some test users.

Next we should create a UserViewModel inside our Models folder, this will be used to return data to our user view:We now have everything ready to return data to our view, we will update our HomeController class to include a constructor, that will receive an ApplicationDbContext as a parameter, which will then be used by our Users method to fetch data from our temp database and return it to the view:Pay close attention to the Users method, there we are calling EnsureCreated() to make sure our database is seeded with data, then we retrieve the records and add them to a List<UserViewModel>, which will then be returned to the view.

We need to build something to display our data, so we should go into the Users.

cshtml file we created earlier.

I’ve created a really simple HTML structure combined with some Razor syntax to display our data:Now, if we run our program by executing dotnet run in the terminal or by pressing F5 in VS Code, and we click on the Users button that is displayed in the navbar we should see our data displayed in a table:As you can see, you can accomplish lots of powerful things when combining the different technologies that ASP.

NET brings to the table.

From here on out, you could add the functionality to perform CRUD operations with this table.

The Microsoft Docs are a great source of information that contain lots of examples on how to add this functionality.

Incorporating DockerLet’s say you want to deploy this app that we just created to a Docker container.

We need to create a Dockerfile, this is what tells Docker how to configure the container for the app that you want to run.

Create a new file called “Dockerfile” at the root of the project and paste the following code inside it:Let’s take a look at what the different commands in the Dockerfile mean:FROM: Sets the baseImage to use for subsequent instructions.

FROM must be the first instruction in a Dockerfile.

We use dotnet:sdk as our base image.

WORKDIR: Sets the working directory for the subsequent Dockerfile commands.

COPY: Copies the specified files from the WORKDIR to the specified directory.

RUN: Executes the following commands at the current WORKDIR.

ENTRYPOINT: Configures the container to be run as an executable.

We set it to use the app we just created, DockerTest.

dllNow that we have configured the Dockerfile, we have to build our image by executing the following command in the terminal:> docker build -t dockertest .

Give Docker a minute to build your image, you can see the progress in the terminal:Docker build processOnce the process is complete, you can check if your image was successfully built by typing docker images into the terminal:Neat!.We are now ready to run the Docker image and try our app, type the following into the terminal:> docker run -d -p 8080:80 –name DockerTest dockertestThat’s it, your Docker image is now up and running.

Head over to http://localhost:8080 and you should see your site:You can check Docker processes by running docker ps in the terminal.

As you can see, deploying a Docker image is really easy.

Don’t forget that you can check the Github repo to find this whole project!Pay attention to the underlined words in this article, there you will find very important links to information that will be very useful when you’re ready to move into more advanced topics regarding .

NET and Docker!.. More details

Leave a Reply