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

Sunday, 12 April 2015

AllPixel and Raspberry Pi WS2812 strip first setup using USB cable

10:07 Posted by Jon Carlos , , , 2 comments

My AllPixel arrived today from ManiacalLabs the first think you need to do when you get it is install the included components. As described not all of the components included are required but if you know what you're planning to do it should be pretty easy to work out what you need.

Next you need to make sure when you connect up the USB cable to the Raspberry Pi it sees your AllPixel board. I attached the USB before I had started the Pi and first off it did not see the board using;

ls /dev/ttyACM*

I restarted a couple of times and then I just unplugged it and plugged it back it an ta-da there is was listed.

Once I knew the board was recognised I was ready to connect up the LEDs. I have a short strip of WS2812 I was using, I connected the wires like this;

GND to GND
DIN to DATA
V+ to +5v

Once you've do this you should be able to just press the button on the AllPixel and you'll see the LEDs light up and if you press it again they should change configuration.

Now you're ready for installing the python library, the raspberry pi comes with pip installed so it should just be a case of running;

sudo pip install BiblioPixel

Now at this point you have a couple of options, you should have everything you need to start coding against the python library however if you'd also like to use the AllPixel with the GPIO pins on your Pi then you might want to run these commands.

curl https://raw.githubusercontent.com/ManiacalLabs/BiblioPixel/master/requirements.txt > requirements.txt

sudo pip install -r requirements.txt


I installed them just in case I wanted to work with the GPIO at a later date. There are further options you might need but these are all I had to run on my Raspberry Pi.

Now I wanted to do a test of my of my LED strip, the guys at ManiacalLabs have provided a great little script to run as quick start. All you need to do is download it and then run it and you'll see your LEDs come to life.

curl https://raw.githubusercontent.com/wiki/ManiacalLabs/AllPixel/examples/AllPixelStripTest.py > AllPixelStripTest.py

python AllPixelStripTest.py

You should see the first 10 of the LEDs in you strip come to life and the 3 LEDs at the end change colour. Details of exactly what should happen is available on the AllPixel Documentation

Note: If you're using a different strip from you'll need to change the enum at the top to select the strip you are using. On line 7 change LEDTYPE.WS2812B to one of the following properties.
  • LPD8806
  • WS2801
  • WS2811
  • WS2812
  • WS2812B
  • NEOPIXEL
  • APA104
  • WS2811_400
  • TM1809
  • TM1804
  • TM1803
  • UCS1903
  • SM16716
  • APA102
  • LPD1886
  • P9813
That should be you all set-up, you should have some LEDs flashing something like this.

A video posted by Jon Carlos (@webmonger) on