问题背景
1)最开始,我们在笔记本中运行 KVM 虚拟机。当每次休眠恢复时,在 KVM 中的虚拟机就会带来大量的 CPU 负载;
2)另外,我们使用的 Linux 发行版是 Debian Testing 分支。由于某些服务是特定于发行版的,所以这些服务没有办法安装;
3)可以使用 Docker 运行服务,但是最佳时间要求一个容器启动一个服务,那么我们有很多服务,岂不是要启动很多容器;
因此萌生该想法:启动容器,运行 systemd 进程,使用 systemctl 进行服务管理,把容器当作虚拟机使用。(这可能违背容器的最佳实践,但现实问题总是多种多样的)
该笔记将记录:在 Docker Container 中,运行 systemd 进程,来进行系统及服务管理,把容器当作虚拟机来使用。
解决办法
systemd within CentOS
执行如下命令,运行 systemd within CentOS 容器:
docker run -it \ --name vm-centos \ --hostname vm-centos \ --env container=docker \ --tmpfs /run \ --tmpfs /tmp \ --volume /sys/fs/cgroup:/sys/fs/cgroup:ro \ --cap-add SYS_ADMIN \ centos:7 /sbin/init
systemd within Ubuntu
执行如下命令,运行 systemd within Ubuntu 容器:
docker run -d --name vm-ubuntu \ --privileged \ -v /sys/fs/cgroup:/sys/fs/cgroup:ro \ jrei/systemd-ubuntu # ubuntu-18.04 docker run -d --name vm-ubuntu-18.04 \ --privileged \ -v /sys/fs/cgroup:/sys/fs/cgroup:ro \ jrei/systemd-ubuntu:18.04
参考文献
Is there any concrete and acceptable solution for running systemd inside the docker container?