The personal ramblings of Jon Carlos mobile & web developer and technology enthusiast.

Saturday, 25 April 2015

Using docker containers to run .net site on linux more complex example

22:19 Posted by Jon Carlos , , , , 6 comments
 Image by Greg Bishop used under creative commons licence

Because of the work I do with Xamarin I'm quite used to working on a mac but building using .net and C#. Amongst .net developers this is quite unusual however this is about to change with the recent open sourcing of the asp.net framework and Roslyn compiler there will be the ability to run .net on any platform.

This led me to start to investigate how you go about building and deploying asp.net applications on Linux. With Xamarin and mono this has been possible for some time though I don't think this is used quite to the extent developers use it for building mobile applications.

I guess the first thing I'd like to say about this process is that I found the set-up relatively easy but the complexity of the example apps I could find were really low so I started to hunt for something more complex so I could really get an idea of how .net ran within a Docker container on ubuntu.

Most of the docker commands are available on this MSDN article "Running ASP.NET 5 applications in Linux Containers with Docker" and a bit more detail about what you're actually doing is also available there. I'm going to concentrate on a couple of things, first I had quite a few issues getting boot2docker to allow me to access containers on my mac, secondly when building containers with boot2docker I had some issues with downloading NuGet packages.

Setup
  1. Get docker installed on your local machine or on an ubuntu box, there are loads of posts on how to do this but the docker site probably covers it best and most comprehensively for all platforms.
  2. Make sure git in installed on the host machine as we'll be cloning a repository that contains the asp.net app for our container.
  3. If you are running boot2docker all the following commands should work on your mac host or by working directly on a Linux box, if you're working on a Linux VM you'll need to SSH on to the box to run the following.
Getting the Application
As I said before the complexity of the projects that were used in most .net + docker examples was very low. After a little research I found the Music Store project which is a store front for CD purchases, it has a little bit of database logic and a few pages as well as login functionality and shopping cart.

To download this repository run

git clone https://github.com/aspnet/MusicStore.git

I found when building the container for this code it could not find all the NuGet packages so use a text editor to create a NuGet.config in the MusicStore project folder.

In Lunix or on a mac run

cd MusicStore/src/MusicStore

nano NuGet.config 

Paste this code in to the file


Once finished (when using nano) press ctrl+x then y then press [enter]

Building your container
To build a container you need to create a script that will tell docker how to configure the container  to run you asp.net app.

This script is called a Dockerfile create us file by running

nano Dockerfile

Now we need to add the build configuration so paste this code in to the file then press ctrl+x then y then press [enter] to save and exit nano.

FROM microsoft/aspnet

COPY . /app
WORKDIR /app
RUN ["kpm", "restore"]

EXPOSE 5004
ENTRYPOINT ["k", "kestrel"]


I'm not going to go in too too much detail about what these commands do but the 2 main ones that will become more familiar with you once asp.net vNext comes out is "kpm restore" which will restore all the packages required for the project and "k kestrel" which is a basic web server that is capable of running asp.net sites.

Next we want to build our container. At the beginning of this article I explained the different ways you can set-up docker, some times you'll need to add sudo to the front of the following commands. In my set-up they work fine without.

docker build -t musicstore .

This command will then go off and run all the commands in your Dockerfile depending on how fast you connection and machine are it might take a little while as it's downloading the microsoft/aspnet docker container and all the NuGet libraries as well.

Once that's complete you should have a new image listed in your docker image repository, to see if it's there run

docker images

You should see an image called musicstore in the list, this is the container we're now going to start. Just run

docker run -t -d -p 80:5004 musicstore

I ran in to quite a few issues with this however when I ran this command on my ubuntu server it worked perfectly every time. If you are running this within an ubuntu VM you will need to configure the NAT settings on the VM for port forwarding, I also had issues using port 80 and 8080 in this configurations so I tried using NAT to transfer port 8081 to port 80 and it worked perfectly so just play about with the following settings if you have problems.

You can find this screen in VirtualBox by right clicking on your VM selecting Settings > Network > Port forwarding (down at the bottom) then clicking the add button on the right.


Now open up the browser of your choice and with any luck hit the IP address of your server/VM/boot2docker/localhost with or without the additional port number and you should see something like this.

So I hope you'll agree, with reasonably little effort you've just managed to set-up quite a complex .net app on a docker container with little work.

I'm sure if you're a .net developer you can see how this might revolutionise the way you deploy apps in to the wild or even just develop locally on your many work machines or between multiple developers! I have managed to get this docker set-up to work across Windows, Linux and OS X which is pretty cool and the ability to scale the number of instances running your app with just that simple command is amazing.

I hope you managed to have a bit of fun with this and if you have any questions let me know.

6 comments:

Anonymous said...

great tutorial!
but unfortunately when i try to build the Dockerfile, i get:
INFO[0001] [8] System error: exec: "kpm": executable file not found in $PATH

Jon Carlos said...

It sounds like you might not be using the latest version of the aspnet image or you're using a different image. The docker instance should have that configured if I remember correctly.

Aashi siva said...
This comment has been removed by a blog administrator.
Giri Mani 2 said...
This comment has been removed by a blog administrator.
Mohana M said...
This comment has been removed by a blog administrator.
for IT the said...
This comment has been removed by a blog administrator.