15

I have seen many workarounds for this to run systemd inside docker containers but looks like most of them compromise the security of the container and the host. How are most people here dealing with running systemd specific stuff inside the container.

7
  • 1
    Could you explain why you need systemd?
    – 030
    Commented Jul 31, 2017 at 14:49
  • 2
    My application is setup in production using some bash scripts which use systemd extensively. I cannot change them but i would want to dockerize the solution.
    – lakshayk
    Commented Aug 1, 2017 at 4:32
  • Could you add the information you have found and add the pros and cons for every approach?
    – 030
    Commented Aug 2, 2017 at 8:38
  • 1
    Clients time and again want docker images to test out in their CI/CD pipelines
    – lakshayk
    Commented Aug 3, 2017 at 4:23
  • 2
    Business decision does not change on small technical limitations :)
    – lakshayk
    Commented Aug 4, 2017 at 5:55

3 Answers 3

6

You didn't mention what distribution you're using inside your container (which would have implications w/r/t which version of systemd you're using), but the following will successfully boot a CentOS container running systemd:

docker run -it --rm \
  -e container=docker \
  --tmpfs /run \
  --tmpfs /tmp \
  -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
  --cap-add SYS_ADMIN \
  centos /sbin/init

This is with Docker 17.05.0-ce; older versions may require additional flags. Using a stock centos:7 image, your initial environment inside the container looks like this:

# systemctl status
    State: running
     Jobs: 0 queued
   Failed: 0 units
    Since: Mon 2017-12-04 17:47:15 UTC; 45s ago
[...]

And:

# ps -fe
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 17:47 ?        00:00:00 /sbin/init
root        16     1  0 17:47 ?        00:00:00 /usr/lib/systemd/systemd-journald
dbus        26     1  0 17:47 ?        00:00:00 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root        28     1  0 17:47 ?        00:00:00 /usr/lib/systemd/systemd-logind
root        30     1  0 17:47 console  00:00:00 /sbin/agetty --noclear --keep-baud console 115200 38400 9600 vt220
root        32     0  0 17:47 ?        00:00:00 bash
root        58    32  0 17:48 ?        00:00:00 ps -fe

Note that I'm using --rm here not because it's necessary but because I'm terrible at cleaning things up after the fact. It's not necessary to get the container to run.

but looks like most of them compromise the security of the container and the host

Well, running systemd does require privileges beyond those granted to a typical Docker container (hence the --cap-add). Whether this has security implications for your environment or not depends on what you're doing.

1
  • 1
    The security hole doesn't really depend on what you're doing, it is here, it's more about the fact you acknowledge and accept it for what you're doing. (sorry but I need to stress the point that mounting /sys/'anything' is a large security hole as it gives the container access to a large part of the underlying host for which the container has been isolated)
    – Tensibai
    Commented Jan 3, 2018 at 19:49
2

For example, if you want to run systemd in a Arch Linux container, run this command: docker run -it --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro --name=ArchLinux archlinux /bin/sh -c "if [ -x /etc/docker-start ]; then exec /etc/docker-start; else exec /bin/sh; fi"

Then run these commands in the container:

echo 'Server = https://THE_FASTEST_MIRROR_FOR_YOU/archlinux/$repo/os/$arch' >/etc/pacman.d/mirrorlist
pacman -Sy --noconfirm systemd systemd-sysvcompat
passwd -d root
echo -en '#!/bin/sh\numount /etc/hostname; umount /etc/hosts; umount /etc/resolv.conf; exec /usr/lib/systemd/systemd' >/etc/docker-start
chmod 700 /etc/docker-start
echo -e 'nameserver 8.8.8.8' >/etc/resolv.conf
exit

Then start the container and attach to it: docker start -ia ArchLinux

You need to run with --privileged, and this may cause some security problems. Please try to avoid it if possible.

0

According to this blog, it is possible to run systemd inside a docker container by building the following dockerfile:

FROM fedora:rawhide
MAINTAINER "Dan Walsh" <[email protected]>
ENV container docker
RUN yum -y update; yum clean all
RUN yum -y install systemd; yum clean all;
(cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done);
rm -f /lib/systemd/system/multi-user.target.wants/*;
rm -f /etc/systemd/system/*.wants/*;
rm -f /lib/systemd/system/local-fs.target.wants/*;
rm -f /lib/systemd/system/sockets.target.wants/*udev*;
rm -f /lib/systemd/system/sockets.target.wants/*initctl*;
rm -f /lib/systemd/system/basic.target.wants/*;
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]

using this command:

docker build -t httpd_rawhide .

The author indicates that the built docker image could be used as a base image

FROM systemd_rawhide
RUN yum -y install httpd; yum clean all; systemctl enable httpd.service
EXPOSE 80
CMD ["/usr/sbin/init"]

and if the container is run

docker run –privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 80:80 httpd_rawhide

systemd will run inside the docker container. It is also possible to run multiple services using systemd. According to the creator of this blog it would be possible to run both mariadb and http inside the same container.

Based on this blog and other articles I read my conclusion is that it is technically possible to run systemd inside a docker container, but I would recommend to avoid running systemd inside a container.

First of all, if systemd is able to run inside a container then this means that it is possible to run multiple services like the author did. From a docker perspective this is not recommended as docker is meant to scale horizontally, i.e. if the load increases of http then addtional docker images should be started.

Second, if such a systemd container will be deployed on an orchestration platform like docker swarm. Will that work? I have some doubts whether that will work.

Third, Running systemd by mounting the cgroup in a privileged container does not look very secure.

In conclusion, although you indicated that the script requires systemd, either rewrite the code or use something else. Running systemd inside a docker container should be avoided in my opinion.

1
  • 1
    Note that the blog post referenced here is from 2014, which given the pace of Docker (and systemd) development is comparatively ancient.
    – larsks
    Commented Dec 4, 2017 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.