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 Unknown , , , , 27 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.

27 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.
john jersy said...

Read all the information that i've given in above article. It'll give u the whole idea about it.
python Online training in chennai
python Online training in bangalore
python interview question and answers

sudha P said...

I am so proud of you and your efforts and work make me realize that anything can be done with patience and sincerity. Well I am here to say that your work has inspired me without a doubt.
Java training in Bangalore | Java training in Kalyan nagar

Java training in Bangalore | Java training in Jaya nagar


Selenium training in Chennai | Selenium training institute in Chennai | Selenium course in Chennai

Selenium training in Bangalore | Selenium training institute in Bangalore | Selenium course in Bangalore

john brito said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
rpa training in Chennai | rpa training in bangalore | best rpa training in bangalore | rpa course in bangalore | rpa training institute in bangalore | rpa training in bangalore | rpa online training

john brito said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
rpa training in Chennai | rpa training in bangalore | best rpa training in bangalore | rpa course in bangalore | rpa training institute in bangalore | rpa training in bangalore | rpa online training

john brito said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
rpa training in Chennai | rpa training in bangalore | best rpa training in bangalore | rpa course in bangalore | rpa training institute in bangalore | rpa training in bangalore | rpa online training

gowsalya said...

Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
Best Devops training in sholinganallur
Devops training in velachery
Devops training in annanagar
Devops training in tambaram

ram said...

I am definitely enjoying your website. You definitely have some great insight and great stories. 
Data Science course in Chennai | Best Data Science course in Chennai
Data science course in bangalore | Best Data Science course in Bangalore
Data science course in pune | Data Science Course institute in Pune
Data science online course | Online Data Science certification course-Gangboard
Data Science Interview questions and answers

anitha said...

You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us
angularjs-Training in pune

angularjs-Training in chennai

angularjs Training in chennai

angularjs Training in chennai

angularjs-Training in tambaram

angularjs-Training in sholinganallur

angularjs-Training in velachery

alex said...

Tired of boring work? Play and win with BGAOC top online casino come in now and take your winnings.

SRI said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
microsoft azure training in bangalore
rpa training in bangalore
best rpa training in bangalore
rpa online training

SRI said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
microsoft azure training in bangalore
rpa training in bangalore
best rpa training in bangalore
rpa online training

SRI said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
microsoft azure training in bangalore
rpa training in bangalore
best rpa training in bangalore
rpa online training

Belago said...

Подскажите где найти блок питания для светодиодной ленты, нашел в компании Ekodio вроде хорошие, буду тестить.

Aditi Ray said...

Thank you for sharing this Information.
Check out the best
coffee table nz
bookshelves
balance bike

Rathinam said...

Amazing idea...! you gave a post very depth content about this topic and surely this post helps to in my future. Please continue this great job...
Corporate Training in Chennai
Corporate Training institute in Chennai
Corporate Training in Chennai
Embedded System Course Chennai
Oracle DBA Training in Chennai
Linux Training in Chennai
job Openings in chennai
Oracle Training in Chennai
Power BI Training in Chennai
Corporate Training in Tambaram

Kerrthika K said...

Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea.
TOEFL Coaching in Chennai
TOEFL Training in Chennai
spanish language in chennai
TOEFL Coaching Centres in Chennai
French Language Classes in Chennai
pearson vue exam centers in chennai
TOEFL Coaching in Tnagar
TOEFL Coaching in vadapalani
TOEFL Coaching in Thiruvanmiyur

The India said...

Rice Bags Manufacturers
Pouch Manufacturers
wall putty bag manufacturers
fertilizer bag manufacturers
seed bag manufacturers
gusseted bag manufacturers
bopp laminated bags manufacturer
Lyrics with music

The India said...

we have provide the best ppc service.
ppc company in gurgaon
website designing company in Gurgaon
PPC company in Noida
seo company in gurgaon
PPC company in Mumbai
PPC company in Chandigarh
Digital Marketing Company

The India said...

we have provide the best fridge repair service.
Washing Machine Repair In Faridabad
LG Washing Machine Repair In Faridabad
Videocon Washing Machine Service Centre In Faridabad
IFB Washing Machine service centre in faridabad
Samsung Washing Machine Repair In Faridabad
Washing Machine Repair in Noida
godrej washing machine repair in noida
whirlpool Washing Machine Repair in Noida
IFB washing Machine Repair in Noida
LG Washing Machine Repair in Noida

The India said...

iso certification in noida
iso certification in delhi
ce certification in delhi
iso 14001 certification in delhi
iso 22000 certification cost
iso consultants in noida

The India said...

iso 27001 certification services
iso 27001 certification in delhi
ISO 9001 Certification in Noida
iso 22000 certification in Delhi