docker中使用systemctl方法

发布时间 2023-10-13 11:39:13作者: 清风6661

想在docker中使用Flexmonster,但是在配置环境后,发现Flexmonster需要使用systemctl来管理服务,然而在docker容器中没有systemctl可用,于是开始折腾之旅!

以下是解决办法:

1、下载systemctl
wget https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py

2、修改dockerfile

#安装wget

USER root

ARG INSTALL_WGET=true

RUN if [ ${INSTALL_WGET} = true ]; then \
    apt-get install -y wget \
;fi

#build镜像时将当前目录中下载的systemctl.py文件拷贝到镜像/bin/systemctl,记得要将下载来的systemctl.py文件放到当前build目录,或修改dockerfile

#------------------------------------------------------------------------
# systemctl
#------------------------------------------------------------------------

USER root

ARG INSTALL_SYSTEMCTL=true

COPY ./systemctl.py /bin/systemctl

RUN if [ ${INSTALL_SYSTEMCTL} = true ]; then \
    chmod +x /bin/systemctl \
;fi

#在镜像中安装python,因为默认镜像中没有安装python

USER root

ARG INSTALL_PYTHON=true

RUN if [ ${INSTALL_PYTHON} = true ]; then \
    apt-get install python \
;fi

#给镜像安装sudo命令,因为镜像默认使用root,未安装sudo命令

ARG INSTALL_SUDO=true

RUN if [ ${INSTALL_SUDO} = true ]; then \
    apt-get install sudo \
;fi

3、build镜像