docker中无法使用systemctl的问题的解决

问题

docker里面无法使用systemctl

root@0d6129fc601a:/# systemctl start ndpd
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
root@0d6129fc601a:/# 

centos

这个容易解决

admin@test:~$ docker run -itd --name test --privileged=true centos:centos8 /usr/sbin/init
9b3b32475a4e1c94018eb82ca5df37b49c274ca713fca9f8039ac836e5022008
admin@test:~$ 

admin@test:~$ docker exec -it test /bin/bash
[root@9b3b32475a4e /]# 
[root@9b3b32475a4e /]# 
[root@9b3b32475a4e /]# systemctl start lldpd

Ubuntu(18/20)

在网上搜了很多方法,都不行

admin@test:~$ docker run -itd --privileged --name test ubuntu:18.04 /usr/sbin/init
0fd49180f44beb1246760dd213a9f18d0eff5b3abb5a27aea1385f0583777981
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"/usr/sbin/init\": stat /usr/sbin/init: no such file or directory": unknown.`
admin@test:~$ docker run -itd --privileged --name test ubuntu:18.04 /sbin/init
a2b206249e1b3febfe9ca310386140513b401614da634b131391ade75408ed0b
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"/sbin/init\": stat /sbin/init: no such file or directory": unknown.
admin@test:~$ 

最后在这里找到了解决方法
这样来看,跟当初创建Ubuntu docker镜像的时候有关,需要使用特殊的镜像

admin@test:~$ docker run -d --name linuxamination --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro jrei/systemd-ubuntu:20.04
Unable to find image 'jrei/systemd-ubuntu:20.04' locally
20.04: Pulling from jrei/systemd-ubuntu
16ec32c2132b: Pull complete 
6daf711395b1: Pull complete 
6c1d92d48249: Pull complete 
4b5388c246dd: Pull complete 
78823b6538a5: Pull complete 
Digest: sha256:ba4b34db6b88a45b64c1f3dc45e1d519550c628ebc10c53420f8930926eda802
Status: Downloaded newer image for jrei/systemd-ubuntu:20.04
4abb2206952bc62e56d051a1d284daf9dff04ce10088dd48d71023ad35f738ec
admin@test:~$ 
admin@test:~$ docker exec -it linuxamination bash
root@4abb2206952b:/# ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.1  0.0  19948  9492 ?        Ss   00:52   0:00 /lib/systemd/systemd
root          21  0.2  0.0  26636 12048 ?        S

贴出docker镜像所在地址,Ubuntu的16/18/20都有
https://hub.docker.com/r/jrei/systemd-ubuntu

你可能感兴趣的:(Linux,虚拟化,docker,ubuntu)