systemctl failed to connect to bus - docker ubuntu:16.04 container
Solution 1
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.
Solution 2
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.
Solution 3
Just start the dbus
service:
/etc/init.d/dbus start
Solution 4
If you're getting this error in the Windows Subsystem for Linux (WSL), I've found it's because Docker is unsupported. This is due to lack of cgroups and other prerequisites.
Solution 5
Try this:
docker run -ti -d --privileged=true images_docker "/sbin/init"
or
docker run -ti -d --privileged=true images_docker
will be same result.
Here I get from the doc of Docker:
By default, Docker containers are “unprivileged” and cannot, for example, run a Docker daemon inside a Docker container. This is because by default a container is not allowed to access any devices, but a “privileged” container is given access to all devices (see the documentation on cgroups devices).
When the operator executes docker run --privileged, Docker will enable access to all devices on the host as well as set some configuration in AppArmor or SELinux to allow the container nearly all the same access to the host as processes running outside containers on the host. Additional information about running with --privileged is available on the Docker Blog.
Related videos on Youtube
Duncan Gravill
Updated on September 18, 2022Comments
-
Duncan Gravill over 1 year
I'm trying to use the
systemctl
command in aubuntu: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?-
Admin almost 7 yearsUse
service ssh start
-
Admin about 4 yearsFor those who have the same problem with
sudo systemctl --user
... just remove thesudo
! -
Admin over 3 yearsHoly shit. I had
systemctl
aliased tosudo systemctl --no-pager
. Thanks for the tip.
-
-
Duncan Gravill over 7 yearsI tried this without success. As my Ubuntu 16.04 instance has the form of a docker container and I have not set up any users I am working as
root
, so I used the variableXDG_RUNTIME_DIR=/run/root/0
, without success. Then I checked the folder/run
and found that there is no subfolder/run/root
. Is there anyway I can get a more verbose error message? I had a look atsystemctl --help
but couldn't see a way of getting detailed error messages. -
Roeland over 7 yearsI am having the same issue and this also did not resolve my problem. Did you ever figure this one out @DuncanGravill
-
Duncan Gravill over 7 years@Roeland Yes. I asked a similar question on SO which had a stronger response. Also I recommend watching the Self-Paced tutorials on the Docker website. It is explained (slightly vaguely) in those videos how
PID 1
which is usuallysystemd
is replaced in a Docker container with the container Entrypoint. -
yop83 over 6 yearsBut this an Ubuntu container, which by default doesn't have systemd and wouldn't have upstart.
-
Zanna over 6 yearsyou shouldn't need
sudo
for that. Seems like a coincidence. Can you please re-test? -
Saif over 6 years@Zanna
saif@sr-server:~$ systemctl status ssh
Failed to connect to bus: No such file or directory
saif@sr-server:~$ sudo systemctl status ssh
[sudo] password for saif:
● ssh.service - OpenBSD Secure Shell server Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2018-01-19 23:38:14 PKT; 4min 4s ago Main PID: 18222 (sshd) Tasks: 15 Memory: 32.7M CPU: 488ms
-
Saif over 6 yearsWhy -1? I just posted what worked for me.
-
Zanna over 6 yearsdownvote was not mine...
-
knocte over 6 yearswhat? ubuntu has systemd by default
-
Adrian Günter almost 6 yearsBrilliant, thanks! I needed this to start/manage a user unit from a system unit.
-
Melebius almost 6 yearsCould you explain your command and the difference to the accepted question?
-
Elder Geek almost 6 yearsWelcome to AskUbuntu! Thank you for trying to help! A quick review of the documentation leads me to believe you may have made an error or 2 in this command. If you would be so kind as to edit it and explain what you are doing and how it solves the problem, ping me and I'll come back and give you an upvote!
-
Hugh Buntu almost 6 yearsStefan: I believe you are correct in the case of Docker.
-
Hugh Buntu almost 6 yearsknocte: my comment covers the case of upgrading from 14.04 (Upstart) to 16.04 (systemd). When performing the version upgrade, Upstart is not replaced with systemd for understandable reasons (like: not breaking the system). In retrospect, I realize the release upgrade process wouldn't be used in Docker. See the link I called out. I see that a number of answers and comments fail to consider the specific case of Docker, and I'll look for that when answering in the future.
-
Parth Shah over 5 yearsWhen you say images_docker, do you mean the vanilla ubuntu:16.04? Or something else?
-
Parth Shah over 5 yearsServices like openssh-server are configured to log to the default syslog facility. How can I get sshd logs without relying on systemctl?
-
user228505 over 5 years@ParthShah please check out sshd man page. Mine has the following options: By caling it with -D you can keep it in the foreground. with -e you instruct it to directly print the logs. these can then later be inspected the docker way with
docker log
. -
DimG over 5 years[FYI] was getting this error with
/sbin/init
being PID=1 process. Adding--privileged=true
as suggested by @sonjaya sonjaya below solved the isssue. -
Sachin Verma almost 5 yearsbeautiful answer!!
-
Dan almost 4 yearsThe question is about getting the
systemctl status ssh
command to run in theubuntu:16.04
docker container. SSH is not installed in that container by default. You also don't needsudo
to run these commands in a docker container. Normal best practices in permission management are different between docker containers and normal OS's. I assume your answer is for normal Ubuntu installation. -
Efren over 3 yearsthis is for system V not systemd
-
am70 over 3 years@DuncanGravill that's because the directory should be
/run/user/0
and not/run/root/0