docker: mono and C# hosted in docker container example.


Setup Guide

This guide assumes you are running the latest version of Ubuntu (currently 14.04.3 LTS) , although you can likely get away with any modern variant of Linux. We will start by getting docker setup, then we’ll move on to creating a container to host Mono, then finally we will create a basic console app in that container and compile & run it using mono.

Step 1 – Update apt-get

Fire up a terminal.

We need to update apt-get so that it has the latest package index. To do that, type:

$ sudo apt-get update

00_update_apt_get

Step 2 – Install docker.io

After apt-get is updated, we can go ahead and install docker. To do that, type:

$ sudo apt-get install docker.io

01_install_docker_io

Step 3 – Sanity test that Docker is running

At this point docker should be installed and running. The first test you’ll want to do is to run docker:

$ docker

02_docker_sanity

Step 4 – docker ps

Now we need to sanity test that docker is running. We can ask docker to list out all the running containers using:

$ docker ps

04_docker_ps

There’s a good chance you’ll run into the issue I did too:

Get http:///var/run/docker.sock/v.12/containers/json: dial unix /var/run/docker.sock: permission denied<br>

The reason for this is the user you are running as does not have access to the docker usergroup. You can add yourself to this usergroup, or simply start a new shell session as root.

Step 5 – docker ps as root

First jump into a root shell:

$ sudo bash

Next, try our previous command again:

$ docker ps

04_sudo_bash_docker_ps

Step 6 – Start up an Ubuntu container

Now we can fire up a new Docker container using the base Ubuntu image:

$ docker run -it ubuntu bash

  • run: tells docker to start a new container
  • -it: required for interactive processes like bash (shell)
  • ubuntu: the container image to run
  • bash: the command to run in the container after launching

05_run_ubuntu

Notify me when there’s a new post

Keep up to date on the latest .NET cloud topics

Email address

Step 7 – Update apt-get inside the container

Now that we’re inside our new Ubuntu container, we have to start installing Mono. But before we can do that, we need to ensure apt-get is up to date:

$ apt-get update

Note: We didn’t have to use sudo here since our container session runs as root by default.

06_update_apt_get_container

Step 8 – Install mono-complete

Now we can use apt-get to install the mono-complete package:

$ apt-get install mono-complete

07_install_mono_complete
This process can take a while as it has to download quite a few dependencies.

Step 9 – Sanity test – run mono

At this point we should have mono installed. You can verify that by typing:

$ mono

08_sanity_mono

Step 10 – Suspend container, back to host shell

At this point we need to jump out of our container session and back to our root shell on the host machine. We don’t want to exit out of the container because all changes are lost between container sessions. Instead we can temporarily suspend our running container session and commit our changes to a new image in the next step.

To suspend, type:

$ <Control-P><Control-Q>

You should see your existing shell on the host machine by typing:

$ docker ps

09_suspend

Step 11 – Commit changes to new Docker image

Next we want to commit the container as a new image. We can give it the following command:

$ docker commit <container-id> <container name>

10_commit_mono_image

Step 12 – Run new Docker image

Now that we’ve committed our container with Mono installed as a new image, we can fire up a new container using that image:

$ docker run -it ashimoon/monodev bash

11_run_monodev

Step 13 – Create a hellomono.cs file

Let’s verify that mono is installed and working. First you might want to install vim (or use any other text editor):

$ apt-get install vim

12_install_vim

Next, fire up vim and enter in a basic hello world:

$ vim hellomono.cs

13_create_hellomono

(Note: Hit ‘i’ to enter insert mode. Type until you’re done. Hit ESC. Then type ‘:wq’)

13_fill_out_hellomono

Step 14 – Compile

Now we can compile our app using the mono compiler:

$ mcs hellomono.cs

14_compile

Step 15 – Run

Finally we can run our app using mono:

$ mono hellomono.exe

Hello Mono!

15_run

https://youtu.be/dWYpz7IyDUw

Thanks to Armen Shimoon for wonderful step by step tutorial on docker & C#.


Now to export and import this image – 

You will need to save the docker image as a tar file:

docker save -o <save image to path> <image name>

Then copy your image to a new system with regular file transfer tools such as cp or scp. After that you will have to load the image into docker:

docker load -i <path to image tar file>

PS: You may need to sudo all commands.

Happy Coding Smile


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s