Docker 与 systemctl

问题描述

我创建了一个基于 Ubuntu 20.04 的 dockerfile,它更改 .bashrc 并从 github 克隆存储库并开始安装。 (在存储库中存在安装程序的文件 server_install.sh。)

Dockerfile:

FROM ubuntu:20.04  
USER root  
LABEL maintainer="zarooba"  
ENV TZ=Europe/Warsaw  
ENV DEBIAN_FRONTEND=noninteractive   
ADD aamks_bashrc.txt /root  
RUN touch /root/.bashrc && cat /root/aamks_bashrc.txt >> /root/.bashrc  
RUN apt-get update  
RUN apt-get install -y python3  
RUN apt-get install -y git  
workdir /usr/local  
RUN git clone https://github.com/aamks/aamks.git  
RUN ["chmod","+x","/usr/local/aamks/installer/server_install.sh"]  
RUN apt-get update -y && \  
    apt-get -y install sudo && \  
    apt-get install -y systemd   
CMD /usr/local/aamks/installer/server_install.sh 

安装开始,一切正常,但在安装过程中的某个时候出现了一个错误

Bug with systemctl: System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: Host is down

systemctl 的安装如 docefile 中所述。我不知道如何摆脱这个错误

包含 systemctl 的文件 server_install.sh 的行:

sudo systemctl daemon-reload
sudo systemctl restart gearman-job-server.service

解决方法

所有 docker RUN 脚本都将在您无法控制 PID 1 的容器中启动。它实际上是您尝试在其中执行的脚本。如果它是另一个服务,那么在每个 stp 中启动/停止进程有时会很有帮助。

RUN dbctl start ; execdb -c "CREATE USER"; dbctl stop

有一种解决方法是使用 docker-systemctl-replacement 脚本拦截 systemctl 执行,并且还可以根据需要启动/停止所需的服务。这允许您在容器中运行面向 VM 的“server_install”(无需修补)