11

I installed docker image and built a image successfully.

When I ssh to the container and run the command service xxx start, an error popped:

service nginfra start

Redirecting to /bin/systemctl start nginfra.service /sbin/service: line 79: /bin/systemctl: No such file or directory

Actually, fakesystemd is installed in the container instead of systemd.

So I removed fakesystemd and installed systemd with the command: yum swap -- remove fakesystemd -- install systemd systemd-libs

But I still can't start the service:

service nginfra start

Redirecting to /bin/systemctl start nginfra.service Failed to get D-Bus connection: No connection to service manager.

Does anyone ever meet and solved this issue?

2
  • Please learn how to format posts to make it readable for others.
    – arogachev
    Feb 13, 2015 at 8:33
  • If you share your Dockerfile, it might be easier for others to help
    – ibotty
    Jul 2, 2015 at 12:33

2 Answers 2

8

I've managed to fix this issue in a CentOS:7 Docker container. I've followed mainly the Guide on CentOS Docker image project.

FROM centos:7

ENV container docker
RUN (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/*;

# Install anything. The service you want to start must be a SystemD service.

CMD ["/usr/sbin/init"]

Now, build the image, and run it using at least the following arguments to docker run command: -v /run -v /sys/fs/cgroup:/sys/fs/cgroup:ro

Then main point is that /usr/sbin/init must be the first process inside the Docker container.

So if you want to use a custom script that executes some commands before running /usr/sbin/init, launch it at the end of your script using exec /usr/sbin/init (in a bash script).

Here is an example:

ADD cmd.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/cmd.sh

CMD ["/usr/local/bin/cmd.sh"]

And here is the content of cmd.sh:

#!/bin/bash

# Do some stuffs

exec /usr/sbin/init # To correctly start D-Bus thanks to https://forums.docker.com/t/any-simple-and-safe-way-to-start-services-on-centos7-systemd/5695/8

You could have System is booting up. See pam_nologin(8) if your using the PAM system, in that case, delete /usr/lib/tmpfiles.d/systemd-nologin.conf in your Dockerfile because it creates the file /var/run/nologin which generates this specific error.

3
  • From github.com/docker/docker/issues/7459#issuecomment-283888347, Valuable info. Reading the Guide and trying with centos7&Dockerfile, the same error didn't go away. Seems my docker version 1.12 is too old, I gave up and fallback to run --privillaged.
    – kaorukobo
    Mar 3, 2017 at 9:59
  • Not sure why, but it seems it's still failing in my case - see github.com/moby/moby/issues/7459#issuecomment-341907198 Failed to get D-Bus connection: Operation not permitted and a [!!!!!!] Failed to mount API filesystems, freezing. Nov 4, 2017 at 19:05
  • @loretoparisi I had this message when forgetting to add -v /run -v /sys/fs/cgroup:/sys/fs/cgroup:ro to docker run did you have it?
    – Anthony O.
    Sep 19, 2018 at 8:13
6

This is known issue with systemd-based OSes inside Docker containers.

Short answer: as well as replacing fakesystemd with systemd you need to attach /sys/fs/cgroup as a read-only volume into the container, build the image and then run it in "privileged" mode.

This is the best guide I've found for this. It uses Centos as the example, but should work with any systemd-based OS.

3
  • So if your running Docker on top of Mac or Windows, then your basically screwed??? :( Oct 26, 2017 at 5:29
  • 1
    @jerseybean No, this issue is with the OS that is running inside the Docker container, not with the type of host that the container is running on.
    – ocean
    Nov 2, 2017 at 9:33
  • This doesn't work for me in a Windows docker environment (using RHEL and CentOS as container OS).
    – Harlin
    Nov 11, 2020 at 0:17

Your Answer

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

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