Sample Application

In this post, we will learn how to create our own Docker container of an ASP.NET Core application by creating a raw ASP.NET Core application, creating a Dockerfile, making a container of this application on our local machine and push it to the Docker Hub.

For this example, I've created a simple static ASP.NET Core project available to download from here.

Or you can setup your ASP.NET Core application.

You have to restore our CoreFx packages by typing "dnu restore" at the root in the terminal after cloning.

Don't get to the word Microsoft Azure, as we will run the same application with Microsoft Azure Container Services in an upcoming blog post.

Just clone this repository on our local machine and navigate to the root.

Configure our terminal for Docker-Host

Now its time to configure our Windows terminal (MSBuild or PowerShell) as Docker client to work with Docker-Host. If you've installed Docker Toolbox, you get a Docker Quickstart Terminal which is a Linux based Shell. But we are not going to use that instead we will use PowerShell (CMD or MSBuild if we want to use). At first start, run Docker Quickstart Terminal upon starting, it will create a default VM in VirtualBox with Linux kernel sufficient for running Docker-Host. After creating default machine, stop the default machine by typing

docker-machine stop default

then close the Quickstart terminal and open-up Windows PowerShell. In PowerShell, start the default machine by typing in the terminal as:

docker-machine start default

We are starting and stopping default machine in different terminals because there can only be one Docker-Client at a time for Docker-Host as current version of Docker supports. There is another version of Docker "Docker for Windows" which is currently in beta. This Docker version uses Linux Alpha and VM is created in Hyper-V instead of VirtualBox in Windows. Next type:

docker-machine env -shell powershell default

You will see something like this

Just copy the second last command shown in the terminal to finally configure your shell for Docker-Host. You have to write this command each time when you start a new instance of your terminal. This also shows common attributes of Docker machine running in VirtualBox. Note the IP address of the Docker-Host shown here. You'll run our Docker container using this IP address not on your local host because Docker-Host is not running on your Local Machine, it is running inside a VM created in VirtualBox.

Creating a Dockerfile

Create a new file in a text-editor at the root and name it as Dockerfile. Dockerfile contains a set of commands which we run to make a container in a Linux-based environment. A Dockerfile is like a recipe file that Docker-Daemon uses to make containers. It is much like a batch file which determines which command or set of commands to run in order to make container's creation successful.

Here in this example, the Dockerfile we are creating is pretty straightforward.

Lets start creating Dockerfile by adding first command as:

FROM microsoft/aspnet:1.0.0-rc1-update1-coreclr

This line represents base image of a Docker container. Each Docker container has to be derived from an image. If you want to make a container from scratch, then usually you have to use a raw Ubuntu or any other Linux distribution image as the base image. The concept of base image is the same as the base-derive classes in Object-Oriented Paradigm. The base image that we are using here in this example contains all the .NET Core components built by Microsoft so that we reuse them rather than downloading them manually while creating our container.

So .NET Core components (such as DNX, DNU and DNVM) will be available for us in our Docker container by deriving it from this base image. Capital words are Dockerfile keywords.

Next add the following optional line:

MAINTAINER Your_Name <Your_Email>

This line shows the author of the Docker image to other people on Docker Registry (Docker Hub). This line is optional.

Next add:

COPY . /app WORKDIR /app

These lines are used to copy current working directory of application to "app" folder using the COPY command and set with working directory as "app" directory using WORKDIR command. Pretty simple! Next Add:

RUN ["dnu", "restore"]

This command will restore all of our CoreFx packages while creating the container. Next, we will expose a port right from within the container to external world (To the Host-Machine) by adding:

EXPOSE 5000

And finally, we will define an entry point of our application as dnx commands and package.json file by adding:

ENTRYPOINT ["dnx", "-p", "project.json", "web"]

The final Dockerfile is given below: # This is the base image which determines from which Docker image the container should derive.

FROM microsoft/aspnet:1.0.0-rc1-update1-coreclr

# Type the author of this Docker image

MAINTAINER Your_Name <Your_Email>

 

# Copy the files and folders from current directory to "app" directory

COPY . /app

 

# Set the working directory as "app" directory

WORKDIR /app

 

# Run the following commands in Linux Terminal to restore .NET Core packages

RUN ["dnu", "restore"]

 

# Expose a port number from the container to outside world (Host-Machine)

EXPOSE 5000

 

# Determine an entry point of the application.

ENTRYPOINT ["dnx", "-p", "project.json", "web"]

Creating a Container from Dockerfile Now open-up the terminal right in this folder and type the following command syntax as:

docker buid -t [image name] [directory]

Replace [image name] with your image name and [directory] as current working directory by typing ".". Press enter and you will see that Docker has started creating your container and restoring CoreFx packages in the container. After a while, you will see that Docker has successfully created Docker image of your application for you on our local development machine. You can see the newly created image on our local machine by typing in the terminal as

docker images

 We run our Docker container by typing in the terminal as:

docker run -p 80:5000 [image name] This command says that: Run the specified image and expose port 5000 from the container to the port 80 (default port) to the outside world (to the Host-Machine). Now open-up the browser and don't navigate to your local host machine, instead type the IP address of the Linux VM where your Docker-Host is running. You can also get the IP by typing in the terminal as:

docker-machine ip If I name it as kjanshair/aspnetcore-example (for example), and you run the command in PowerShell as:

docker run -p 80:5000 kjanshair/aspnetcore-example

You will see that PowerShell has started the container:

 

 

 

And you can see your application is up and running in the browser:

 

 

Push to Docker Hub

Now it is time to push our newly created Docker image to Docker-Hub. As said earlier, Docker Hub is the default Docker Registry where we commit, push, pull and share our repositories. It is the same as GitHub in case of Git repositories.

The first thing we need to do is to login to our Docker account on our local development machine by typing:

docker login

It will our user name and password for our Docker Hub account, enter it and it will display a success message like:

 

After logging in, we have to tag our image to our Docker Hub image namespace. Usually our account name is our name of the namespace. Type in the terminal as:

docker tag [image name] [namespace] / [image name]

In this command syntax, replace your image name with your local image that you've created earlier, namespace name with your Docker Hub user name and tag with your version label or tag such as latest. After pressing enter it will tag your image to your Docker Hub namespace. Now push your Docker image to the Docker Hub by typing in the terminal as:

docker push [namespace] / [image name]

Press enter and you'll see that commands have started execution and finally your Docker image is available on your Docker Hub account. kjanshair.azurewebsites.net

  1. FROM microsoft/aspnet:1.0.0-rc1-update1-coreclr