Sunday, April 06, 2014

Using Docker for MOOCs - part 1


A while back I decided to use RedHat's OpenShift PaaS to perform my homework for a MongoDB homework from MongoDB University.  The difficulty I had was due to my inexperience with OpenShift, but I was able to do my homework.

Recently I've been looking at Docker.  The more I learn about Docker the more I want to do with it.
There are Docker images available for "everything".
There's Dockenstack for OpenStack testing, hadoop images, Django images, .. etc ...
Thursday I wanted to try out Java8 during Stephen Chin's presentation here in Grenoble, so I just downloaded a Java8 Docker image.

Hopelessly addicted to MOOCs, I wanted to see if I could create environments for current and future MOOCs under Docker.

The MOOC
First attempt is for my first weeks homework on Coursera's ...



The first homework essentially has us setting up a minimal blog application.
It only requires about 5 actual command-lines to do the homework, but before that you have to have a functional Rails environment.

VMs are so heavy, so 1990's ...
The MOOC provides a VM which is a very good solution, but VMs are so heavy, so 1990's ...
The MOOC also provides instructions to install the environment manually under Linux, OSX, Windows.
The Windows users have had a bit of a struggle and I hope they haven't lost too many students because of this.

Using Docker
I wanted to do the same using Docker.

This was a good experiment for me, still learning the basics of Docker.
It gave me experience of writing a non-trivial (well quite trivial really, but the details of rails installation, and the need for a login shell presented some problems along the way) Dockerfile to build an image, experimentation to actually do my homework in that environment and of archiving this to github to trigger a trusted build on index.docker.io.

The result has been positive, I submitted my homework today.

My Dockerfile is on github here,

FROM ubuntu:saucy MAINTAINER Michael Bright <dockerfiles@mjbright.net> RUN apt-get update ADD .bashrc /root/.bashrc # Default commandCMD bash # Install packagesRUN DEBIAN_FRONTEND=noninteractive apt-get -y install openssh-server #RUN mkdir /var/run/sshd RUN /etc/init.d/ssh start RUN DEBIAN_FRONTEND=noninteractive apt-get -y install tar build-essential curl git-core # Add Python stuffRUN DEBIAN_FRONTEND=noninteractive apt-get -y install python python-dev python-distribute python-pip # Add Node.js for Ruby web server:RUN DEBIAN_FRONTEND=noninteractive apt-get -y install nodejs # Add Ruby Version Manager: RVM#RUN curl -L https://get.rvm.io | bash -s stable --rubyRUN curl -L https://get.rvm.io > install_rvm.sh RUN chmod +x install_rvm.sh ## Ruby installation: takes a while ...RUN bash -c /install_rvm.sh stable --ruby RUN bash -lc "rvm list known"RUN bash -lc "rvm get stable"RUN bash -lc "rvm install ruby-2.1.0"RUN bash -lc "rvm use ruby-2.1.0" ## Rails installation: takes a while ...RUN bash -lc "rvm all do ruby --version"RUN bash -lc "gem install rails"RUN bash -lc "rails -v" RUN adduser --gecos "" --ingroup rvm --disabled-password user ADD .bashrc /home/user/.bashrc RUN echo 'user:user' |chpasswd EXPOSE 22 EXPOSE 3000
Most of the actual rvm, ruby, rails installation is simply following the MOOC instructions.  Note that each RUN instruction is preceded by, bash -lc, which was necessary so that each instruction is run in a login shell.  On the #docker IRC channel I was advised *not* to go this route, but I really wanted to stick with the MOOC instructions as closely as possible.  To do this I had to ensure that each RUN command was run under a login shell with the appropriate environment.  This seems to be a deficiency in Docker, that because each RUN command is run in a fresh environment, environment state does not persist across commands as would normally happen in the shell.

Otherwise this script also installs an openssh-server, and adds a user 'user' with password 'user' so that we can login and run commands in parallel to the rails server itself.

Building or downloading the Docker image
To use this image, either build from this Docker file with the command:
docker build -t my/rails github.com/mjbright/DockerMOOCCourseraWAA
or download the "Trusted build" from the Docker registry:
https://index.docker.io/u/mjbright/docker-mooc-coursera-waa/
using the command
docker pull mjbright/docker-mooc-coursera-waa
Using the image
Once the image is created start the image as follows:
docker run -i -t -p 2222:22 -p 3000:3000 -v /home/mjb/DOCKER/WAA:/home/user mjbright/docker-mooc-coursera-waa bash -lc /etc/init.d/ssh start; bash
This will create a container from the image, and automatically start the ssh server.
Note that the arguments
    -i -t: allow for an interactive session
    -p 2222:22 maps the openssh server port 22 of the container to 2222 on localhost
   -p 3000:3000 maps the rails server port 3000 of the container to 3000 on localhost
   -v maps the directory /home/user in the container to /home/mjb/DOCKER/WAA on the host

Then login to the container using ssh as follows:
ssh -p 2222 user@localhost
and finally do your homework !!

Next steps
In practise I ended up scripting the actual homework, as I was having some problems at the command-line.

An advantage with the use of Docker is that when an attempt fails, you can try again manually, make the script change and then relaunch a new container very quickly, starting from scratch.
This allows for a very rapid development cycle.

I'll see if there are improvements to be made for next weeks homework.

I hope to complete the 6-week course in this way, now the hard setup work is done - I spare you the details of the problems I had installing/building ruby/rvm/rails/gem before using "bash -lc" in the Dockerfile.

I also hope to extend and reuse this environment for future MOOCs, such as Edx's SaaS part1 which comes up in a couple of weeks.

I'll also create a separate environment for Coursera's High Performance Scientific Computing course (not Rails based).

Some people have been using Docker to setup a Desktop environment.
I'm not sure how useable this will be, but I intend to try it.
I really want my base OS (Fedora 19 today, but may change ...) to be just that, a stable base on which I can run other environments, even Desktops in Virtual Machines and Containers.

I like VMs, but I *love* Docker, it's so nice having such a rapid environment.
It's really nice being able to share my work here, on github and in the Docker registry.










1 comment:

davido said...
This comment has been removed by a blog administrator.

[Conference - CodeEurope.pl] Developing Micro-services on Kubernetes

In April I had the chance to present at CodeEurope.pl , first in Warsaw on Apr 24th, and then in Wroclaw ("wroslof" was my best at...