I was recently looking at an example that had provided a VirtualBox VM image with “all the stuff” packed into it to work out of the box. I didn’t want to muck around much with VirtualBox, since I already have Docker working fine on my Mac. So I had to build up an Ubuntu image to help run the examples.
Systemd
The first major thing to notice was that the example wanted to run some simple processes as services, using systemctl. This doesn’t come with the Ubuntu base image in Docker, because it really doesn’t make sense to run processes this way in a world where you really only want to run one process, and have the container die if that process dies. So we need to get that enabled. Systemd needs to be the root process, so we need to build a new base image to get that set up.
docker run --name ubuntu-systemd -d --rm --tmpfs /run --tmpfs /run/lock -v /sys/fs/cgroup:/sys/fs/cgroup:ro jrei/systemd-ubuntu
This uses some random base image on Docker Hub that has done a lot of the work to get systemd set up and running inside the container. We just have to provide a few things to kind of trick the container into liking the Docker environment.
That works, but it would be nice to have more dev tools installed.
Dev Tools
Because this isn’t an image that we will be taking to production or anything; it is just a playground for this specific example I was working through, we can take some shortcuts.
FROM jrei/systemd-ubuntu:18.04 RUN apt-get update \ && apt-get install -y vim curl apache2 nginx make sudo VOLUME ["/run", "/run/lock"]
This is very simple, we just install some useful tools, and then declare some volumes. The VOLUMES
command is a bit of a shortcut so we don’t have to keep typing the --tempfs
args in the run command. Notice also that I do not clean up the apt-get stuff this time, contrary to Docker best practice. I don’t want to clean it up in this case, as I may want to install something in the running container too, so this will make that a bit faster.
Now we can build and run that container. And exec into it to do some work.
docker build . -t ubuntu-systemd docker run --name ubuntu-systemd -d --rm -v /sys/fs/cgroup:/sys/fs/cgroup:ro ubuntu-systemd docker exec -it ubuntu-systemd bash