Running services with docker and systemd

I have described how to run systemd in a Docker container on Fedora in previous article but didn't give you any "real" example of how to actually use it. I guess you find it easy to figure out your own examples but let me show you mine.

WordPress & MariaDB

I like to use WP and MariaDB as example applications when I give a talk about containers. It's quite simple setup and at the same time uses some nice features of docker (f.e. links or port mapping). If you just want to see my Dockerfiles, please go to this github repository. If want more babbling, read on.

The MariaDB Dockerfile is forked from Fedora-Dockerfiles and I guess it will be "merged" there soon:). First few lines are quite boring - yum update/install. Next line I really like. How to start MariaDB properly..hmm...hey, let's use what packagers came up with - mariadb.service file.

RUN systemctl enable mariadb.service

This really presents the beauty of using systemd inside containers. Single line tells the init that we want to start the service on the container start. Easy, clean, awesome. Then some "stuff" follows. I let it up to you to figure out what it actually does - everybody likes homeworks, right?

You can also check my WordPress Dockerfile in the same repository. It's a bit longer but the most important line for this case is again enablement of the service - in this case httpd.

RUN systemctl enable httpd.service

If you build those two images...

docker build -t vpavlin/mariadb mariadb/
docker build -t vpavlin/wordpress wordpress/

You can run them with these commands

docker run -it --rm -v /sys/fs/cgroup:/sys/fs/cgroup --name mariadb vpavlin/mariadb
docker run -it --rm -v /sys/fs/cgroup:/sys/fs/cgroup -p 80:80 --name wordpress --link mariadb:mariadb vpavlin/wordpress

To describe these commands I'd say: Run a container with stdin and out attached to my tty, volume mount /sys/fs/cgroup for systemd, name containers mariadb and wordpress (respectively) and link mariadb to wordpress (which basically mean tell WP how to connect to mariadb). Oh, and map port 80 of WP container to host's port 80.

When you hit http://localhost in your browser, you should see a WP installation page.

Snímek z 2015-02-24 23:35:53

Any questions?:)

3 thoughts on “Running services with docker and systemd

  1. serhii

    I can’t use RUN systemctl enable some.service in dockerfile. Dockerfile cosist following lines
    FROM sselyvon/redhat:base3
    RUN systemctl enable sshd.service
    CMD [“/usr/lib/systemd/systemd”]

    I’ve got error message:

    docker build -t sselyvon/redhat:base4 .
    Sending build context to Docker daemon 2.048 kB
    Sending build context to Docker daemon
    Step 0 : FROM sselyvon/redhat:base3
    —> 967d77b26eb0
    Step 1 : RUN systemctl enable sshd.service
    —> Running in 0b04ffc6f3d4
    INFO[0000] The command [/bin/sh -c systemctl enable sshd.service] returned a non-zero code: 1

    Reply
  2. serhii

    In case I try to execute systemctl enable manually, I get following result:

    docker run -ti -v /sys/fs/cgroup/:/sys/fs/cgroup/ sselyvon/redhat:base3 /bin/bash
    [root@605384b6e57e /]# systemctl enable sshd
    Failed to get D-Bus connection: Failed to connect to socket /run/systemd/private: No such file or directory
    [root@605384b6e57e /]# echo $?
    1

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *