Linux Containers and Docker

A New Sort-Of Virtualization Framework That Will Leave You Confused, Yet Excited For The Future Of Virtualization Technologies, If That's The Sort Of Thing You Usually Get Confused and/or Excited By

What is virtualization?

kvm, xen, virtualbox, etc

VMs are (usually)

  • Machine based
  • Hardware abstracted
  • Memory bound
  • (sometimes) Bound to hardware features

VMs are GREAT when you

  • Need multiuser environments
  • Need to run different architecture from your host
  • Need to do many things at once
  • Live in a wonderland where there is only dev
  • ...and possibly testing
  • Have gobs and gobs and gobs and gobs and gobs of RAM and/or disk space

Hey, remember Java?

FAIL FAIL FAIL

What are Linux Containers?

Containers are...

  • 64bit Linux feature since ~v2.6 / 2009
  • OS-level virtualization
  • share kernel and other deps with host machine
  • Process isolation
So they are way more lightweight than traditional VMs, at a cost of abstraction
Which makes them fast

What is Docker?

Sorta git-ish...

If working with LXC is like...


.
./82
./82/292af05a4aebff056a5117d197be0ff72f35a2
./1f
./1f/8f113d7e0fe8b5f3bcace1b8928fab8ba2fa6e
./10
./10/177fde06bb32c3c45d981a23706df745f3c5c4
./a6
./a6/b4da7272be250131317b3bf13c0ee4c9ee14e9
./47
./47/98aa54278c0c09a5fa68bf1bffa7a7e804fc04
./cf
./cf/ddc1a87ae05b178badbef43b02a2803a113492
./2a
./2a/eae8de7ed3ac0c2ef19463183ee26ca7a4c97c
./16
./16/0f9beb0bb760257cf11ecdd645ee78eee16e24
./13
./13/bbd56328a6edfe0f6f44bec2f63013ba950e70
./info
./93
./93/53167c9b5d016e0617f9747641f9aae7b07413
./93/10067c662520fb8f66942e48cad17904010845
./0c
./0c/f76a31b1a28dceea540d421eb086e83cbd98af
./12
./12/98f8b6ad83e09cc86fca39811be56b42d89267
./ea
./ea/bd742987a34aa44ac2dce01ac72bbb4e834675
./86
./86/dbbab228f75d61d3604086871489037188379a
./pack
./pack/pack-8a8219aa3b6e9ea31087bf1b80cb7bbd5ce23de0.idx
./pack/pack-8a8219aa3b6e9ea31087bf1b80cb7bbd5ce23de0.pack
./6b
./6b/8039fefa546f172097cfcc7ed54a262efe8381

Then Docker is sort of like...


jbfink@soapy:~/work/dockerpreso$ git log
commit d3e33df5e15d0b24d5691de2c13f32d77a7f3ba8
Author: jbfink 
Date:   Wed Aug 14 11:00:46 2013 -0400

    Then Docker is sort of like...

commit 82292af05a4aebff056a5117d197be0ff72f35a2
Author: jbfink 
Date:   Wed Aug 14 10:43:31 2013 -0400

    More slides equals more fun.

commit 2aeae8de7ed3ac0c2ef19463183ee26ca7a4c97c
Author: jbfink 
Date:   Wed Aug 14 10:29:01 2013 -0400

    Java FAIL FAIL FAIL

commit eabd742987a34aa44ac2dce01ac72bbb4e834675
Author: jbfink 
Date:   Wed Aug 14 10:22:11 2013 -0400

    More changes equals more fun.

Docker is built on

  • Go
  • Linux >3.8
  • AUFS

dotCloud sez:

“Docker enables any application and its dependencies to be packaged up as a lightweight, portable, self-sufficient container. Containers have standard operations, thus enabling automation. And, they are designed to run on virtually any Linux server. The same container that that a developer builds and tests on a laptop will run at scale, in production, on VMs, bare-metal servers, OpenStack clusters, public instances, or combinations of the above.”

Docker containers are...

  • Small (thanks to AUFS)
  • Fast to set up, fast to tear down
  • repeatable
  • native to 64bit Linux but can run inside a Vagrant

So a Docker container can...

  • run any Linux distro
  • be built any way you like (Chef, Puppet, apt-get, yum)
  • and once built, will run exactly like you built it on any host you move the container to

This potentially means an END to every neckbeard holy war ever.

An example Dockerfile


FROM ubuntu:latest
MAINTAINER John Fink 
RUN apt-get update
RUN apt-get -y upgrade
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install mysql-client mysql-server apache2 libapache2-mod-php5 pwgen python-setuptools vim-tiny php5-mysql
RUN easy_install supervisor
ADD ./start.sh /start.sh
ADD ./foreground.sh /etc/apache2/foreground.sh
ADD ./supervisord.conf /etc/supervisord.conf
RUN rm -rf /var/www/
ADD http://wordpress.org/latest.tar.gz /wordpress.tar.gz
RUN tar xvzf /wordpress.tar.gz 
RUN mv /wordpress /var/www/
RUN chown -R www-data:www-data /var/www/
RUN chmod 755 /start.sh
RUN chmod 755 /etc/apache2/foreground.sh
EXPOSE 80
CMD ["/bin/bash", "/start.sh"]

And a startup script


#!/bin/bash
if [ ! -f /var/www/wp-config.php ]; then
#mysql has to be started this way as it doesn't work to call from /etc/init.d
/usr/bin/mysqld_safe & 
sleep 10s
# Here we generate random passwords (thank you pwgen!). The first two are for mysql users, the last batch for random keys in wp-config.php
WORDPRESS_DB="wordpress"
MYSQL_PASSWORD=`pwgen -c -n -1 12`
WORDPRESS_PASSWORD=`pwgen -c -n -1 12`
#This is so the passwords show up in logs. 
echo mysql root password: $MYSQL_PASSWORD
echo wordpress password: $WORDPRESS_PASSWORD
echo $MYSQL_PASSWORD > /mysql-root-pw.txt
echo $WORDPRESS_PASSWORD > /wordpress-db-pw.txt
#there used to be a huge ugly line of sed and cat and pipe and stuff below,
#but thanks to @djfiander's thing at https://gist.github.com/djfiander/6141138
#there isn't now.

sed -e "s/database_name_here/$WORDPRESS_DB/
s/username_here/$WORDPRESS_DB/
s/password_here/$WORDPRESS_PASSWORD/
/'AUTH_KEY'/s/put your unique phrase here/`pwgen -c -n -1 65`/
/'SECURE_AUTH_KEY'/s/put your unique phrase here/`pwgen -c -n -1 65`/
/'LOGGED_IN_KEY'/s/put your unique phrase here/`pwgen -c -n -1 65`/
/'NONCE_KEY'/s/put your unique phrase here/`pwgen -c -n -1 65`/
/'AUTH_SALT'/s/put your unique phrase here/`pwgen -c -n -1 65`/
/'SECURE_AUTH_SALT'/s/put your unique phrase here/`pwgen -c -n -1 65`/
/'LOGGED_IN_SALT'/s/put your unique phrase here/`pwgen -c -n -1 65`/
/'NONCE_SALT'/s/put your unique phrase here/`pwgen -c -n -1 65`/" /var/www/wp-config-sample.php > /var/www/wp-config.php

chown www-data:www-data /var/www/wp-config.php
mysqladmin -u root password $MYSQL_PASSWORD 
mysql -uroot -p$MYSQL_PASSWORD -e "CREATE DATABASE wordpress; GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'localhost' IDENTIFIED BY '$WORDPRESS_PASSWORD'; FLUSH PRIVILEGES;"
killall mysqld
sleep 10s
fi
supervisord -n

OK. Demo?

Any questions?

  • John Fink
  • Digital Scholarship Librarian
  • McMaster University
  • http://github.com/jbfink
  • @adr
  • john.fink@gmail.com