Sunday, October 12, 2014

Provisioning Docker containers with Weave and Upstart

Someday it will be nice to pick a hosting service and have all of your Docker containers magically hook together correctly, securely, and quickly scale. But it just isn't quite there yet. Weave is a nice utility for creating an overlay network which can keep your containers connected on different servers even in different data centers or different hosting providers.

Below is a Upstart configuration that will allow you to keep Weave running and come back after server reboot. Simply do "sudo start weave" to get the service running the first time. You should also edit your /etc/default/docker.io file to add in the options to keep it from restarting containers since our Upstart service is designed to do that for us: DOCKER_OPTS="-r=false"

/etc/init/weave.conf
description "Weave Docker Network Service"
start on filesystem and started docker.io
stop on runlevel [!2345]
respawn
script
 /usr/bin/docker rm -f weave || true
 /usr/local/bin/weave launch 10.100.0.1/16 {other server public ip(s) here}
 /usr/bin/docker attach weave
end script

We make sure docker is running first before starting Weave. This configuration was on Ubuntu 14.04; prior to that the docker service was just docker without the io. You can run this on each of your servers with a different 10.100.0.x ip and specifying all of the other servers ip after it so they stay connected.

Next we can start a typical Docker container like a Mongo database on one of our servers. We will keep it running with Upstart as well and make sure it starts after Weave.

/etc/init/mongo.conf
description "Mongo Database Service"
start on filesystem and started weave
stop on runlevel [!2345]
respawn
pre-start script
 mkdir -p /data/db
end script
script
 /usr/bin/docker rm -f mongo || true
 /usr/local/bin/weave run 10.100.1.1/24 --name mongo mongo:2.6
 /usr/bin/docker attach mongo
end script

Go to one of your other servers and use the script below to connect to your Mongo database running in a container on a different server.
#!/bin/sh
# connect to the mongo db from any weave enabled host

sudo docker rm -f db_connect; sudo weave run 10.100.1.99/24 --name db_connect -it mongo:2.6 /bin/sh -c "mongo 10.100.1.1"; sudo docker attach db_connect


Try it out on Digital Ocean using a couple of their smallest plans. The link will give you $10 credit - enough to run two of the smallest servers for a month.

No comments:

Post a Comment