Getting More Familiar with Docker


I’ve been spending this week getting more familiar with Docker. I’ve learned how to build new images, spin up a collection of related containers with Docker Compose, and push an image to AWS.

I’m pretty excited about how this will help my team, but I’m also nervous about the unknowns we’ll encounter. My goal is to free up time, not take up more.

It’s important to know what Docker is and isn’t. To be honest, I’m still a little fuzzy. I wrote about this in my last post.

Docker utilizes virtualization technology to run processes in isolated environments called Containers. It has some similarities to virtual machine technology, such as VMWare and VirtualBox.

Docker Containers do not hold an OS, but they do have libraries and binaries from other OSes.

After working with Docker a little, I’m now much familiar with commands to setup a container and access the container (docker exec -it is my friend). I’ve also gotten more familiar with docker-compose (docker-compose up/down are pretty cool).

The documentation for Drupal on Docker provides info on both setting up Drupal and MySQl.

I’ve been able to spin up a Redis and MySQL server with Docker, it has simplified the setup and given my team a consistent toolset.

Manage Your Daemons With Upstarts


I am finding the need for custom Linux service scripts more and more. This is in the case where a program I want to run in the background does not already have one for one reason or another. Maybe I’m compiling instead of using apt, or sometimes I am creating my own app.

In the past I’ve used the traditional init script format that exists in /etc/init.d. This proved to be tedious. Unless I already had the init script on hand, I would need to code it out in bash script, like so:

#!/bin/bash
#
# Debug SMTP Service
#
case "$1" in
start)
if [ -f "/var/run/debugsmtp.pid" ]
then
    echo "Service already running"
else
    echo "Starting service..."
    python -m smtpd -n -c DebuggingServer localhost:25 &
    echo "$!" > /var/run/debugsmtp.pid
    echo "Service started"
fi
;;
stop)
if [ -f "/var/run/debugsmtp.pid" ]
then
    PID=`cat /var/run/debugsmtp.pid`
    kill -9 "$PID"
    rm /var/run/debugsmtp.pid

if [ ! -f "/var/run/debugsmtp.pid" ]
then
    echo "Service stopped"
fi
else
    echo "Service not running"
fi
;;
*)
echo "Service"
echo $"Usage: $0 {start|stop|status}"
exit 1
esac
exit 0

As you can see, there are sections to handle different commands, in this case it is just start and stop. With other programs there may also be restart, status and reload.

Start checks if the process is already running by checking if a PID file exists. If it does, then the service is assumed to already be running. If not, the script goes on to start the process and stuffs the process ID into a newly created pid file at the same location.

Stop is similar, as in it works on the PID file. It will check if the process is running by checking the pid file, and if it does, it will run a kill command on the process.

Phew.

Setting this up for multiple systems is a pain, and it is more technical than I would like to be doing on a routine basis. Specifically, managing processes with PIDs and using the kill command makes me a little nervous.

But there are other problems I could run into that would run into some shortcomings of init.d. One being the ability to base the daemon’s start on the network interface being up, or the filesystem being ready. This would apply to applications such as web and SMTP servers.

Enter upstart.

Upstart came into play in 2006 (at least in the Ubuntu world). It is a replacement of the previously mentioned init system where scripts are placed in /etc/init.d and /etc/rc*.d folders. It has a more accurate boot sequence through event based start up, and take less effort to implement due to some tasks like PID management being automatically handled.

Upstarts are configured with stanzas.  Two of those being start and stop, where you use runlevels or events (network up, filesystem ready) to define when the daemon should be started and shutdown. When it’s all set it looks like this:

description “nginx http daemon”

# Start daemon when filestyem and network interface is up
start on (filesystem and net-device-up IFACE=lo)

# Stop daemon when runlevel is not specified levels
stop on runlevel [!2345]

# Daemon binary location
env DAEMON=/usr/sbin/nginx

# Daemon pid location
env PID=/var/run/nginx.pid

# Indicate daemon has child processes
expect fork

# Restart if daemon ends prematurely
respawn

# Max respwans
respawn limit 10 5

# Commands to run before daemon starts
pre-start script
$DAEMON -t
if [ $? -ne 0 ]
then exit $?
fi
end script

# Run daemon
exec $DAEMON

Conclusion

So there you have it. Upstarts make life easier by cutting your daemon initialization scripts in half with more control, and less being in the weeds. It’s ideal for custom or compiled applications (packages often install their own init scripts) and removes the need to boot up a daemon every time you start your machine.