« Back to home

Ubuntu + Docker + Mono = Hello World

Posted on

Below are the steps I followed to compile and execute a C# hello world application, running on Mono, in a Docker container, on Ubuntu running in a virtual machine. I thought it would be a good idea to document the process. I hope it helps.

Disclaimer: I am new to a lot of these technologies, so it is very likely that I am doing something inefficiently or wrong. Keep that in mind. One alternative way to accomplish the same goal would be to use Vagrant.

Get your Virtual Machine up and running

  1. Install VirtualBox

  2. Download the installation ISO for Ubuntu. I’ve been using Ubuntu 12.04 LTS 64-bit, the long term support release. As of this writing, the latest version is 12.04.3, which includes the 3.8 Linux kernel. I believe 12.04.2 has an older kernel which requires an update to run Docker.

  3. Create your new image, mount the ISO and install Ubuntu.

  4. Want to save your self some typing? In the virtual machine settings, under General section, Advanced tab select “bidirectional” for the shared clipboard.

Install Docker

Read Docker’s installation instructions for Ubuntu 12.04 LTS, but ignore the bit about updating the kernel. You installed 12.04.3 right? The kernel included with 12.04.3 does not deed to be updated.

Do you want to know what kernel you are running? execute rname -u On Ubuntu 12.04.3 the kernel is “3.8.0-29-generic”

Follow the installation instructions. I think they used a backslash to wrap one of their commands. They are:

# Set up the Docker repository
sudo sh -c "echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
sudo apt-get update

# Install Docker
sudo apt-get install lxc-docker

# Check the installation
docker version

Now run the Docker equivalent of hello world. This will download the Ubuntu Docker image if you don’t have it already.

# Start a Docker container and echo back hello world
sudo docker run ubuntu /bin/echo hello world from docker container

Docker + mono

Now that you have Docker installed, you could start configuring your docker container and installing mono by hand. One nice thing about docker is that there is a collection of pre-built docker images that you can use.

I found this mono docker image and have been using it. Some docker images have associated Dockerfiles that are the source for the image. Unfortunately, this one does not have an associated Dockerfile. I did however find this gist that I think is the source used to create the image.

Some Docker image sources are trusted, some are not. For a hello world exercise I don’t really care that much. If this were a more serious application I might be more concerned about how the image was made.

Docker + Mono hello world

Start up a new Docker container with mono and connect using bash. This will download the mono image if you don’t already have it. Your command prompt should change to indicate you are in the container.

# start the container (command prompt should change)
sudo docker run -i -t pjvds/mono /bin/bash 

# Check the mono installation in the container.
mono --version

Here is the output:

{% highlight text %} Mono JIT compiler version 3.2.3 (tarball Sun Oct 27 17:39:53 UTC 2013) Copyright © 2002-2012 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com TLS: __thread SIGSEGV: altstack Notifications: epoll Architecture: amd64 Disabled: none Misc: softdebug LLVM: supported, not enabled. GC: sgen


Create a hello world file. (Taken from the [Mono Basics page](http://www.mono-project.com/Mono_Basics) )

bash

create source file

echo ‘using System; public class HelloWorld { static public void Main () { Console.WriteLine (“Hello Mono World”); } }’ > hello.cs

Compile

gmcs hello.cs

Execute

mono hello.exe


The output should be:

{% highlight text %}
Hello Mono World

Exit your container. (Your command prompt should change back.)

exit

That’s it, you’re done for now. Go celebrate.

Conclusion

I think this was a very painless process. I’m impressed with Docker and the image ecosystem.

Docker is currently at version 0.7 and they warn you it is not ready for production use. However, several companies are already using it in production. I find that a bit odd. Docker explains what is required to get to 1.0 but that page is already a couple months old and a couple minor versions behind.

Next, I would like to try deploying some compiled .Net binaries to the container, then a simple website.