systemctl failed to connect to bus - docker ubuntu:16.04 container

I'm trying to use the systemctl command in a ubuntu:16.04 docker container. I'm running the following command...

systemctl status ssh

However I'm getting the error...

Failed to connect to bus: No such file or directory

Why is this not working? Is this related to Ubuntu running in a docker container? How can I get systemctl to work correctly?


I assume you start your docker container with something like

docker run -t -i ubuntu:16.04 /bin/bash

The problem now is that your init process PID 1 is /bin/bash, not systemd. Confirm with ps aux.

In addition to that you are missing dbus with would be the way to communicate. This is where your error message is coming from. But as your PID 1 is not systemd, it will not help to install dbus.

Best would be to re-think the way you plan to use docker. Do not rely on systemd as a process manager but have the docker container run your desired application in the foreground.


Others have reported a similar problem. Start up the terminal and type:

$ env

Do you see an environment variable like this?

XDG_RUNTIME_DIR=/run/user/`id -u`

Where id -u is enclosed in backticks not single quotes. This variable is reinterpreted into a number usually 1000 for regular users and 0 for super user (sudo).

If the environment variable XDG_RUNTIME_DIR does not exist you need to create it. The full discussion is in launchpad systemd answers.