在docker容器中使用systemctl启动服务 [英] Start service using systemctl inside docker container

查看:1058
本文介绍了在docker容器中使用systemctl启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Dockerfile中,我试图安装多个服务,并希望在启动容器时使它们全部自动启动。
服务之一是mysql,当我启动容器时,我看不到mysql服务正在启动。当我尝试手动启动时,出现错误:
无法获得D-Bus连接:不允许操作

In my Dockerfile I am trying to install multiple services and want to have them all start up automatically when I launch the container. One among the services is mysql and when I launch the container I don't see the mysql service starting up. When I try to start manually, I get the error: Failed to get D-Bus connection: Operation not permitted

Dockerfile:

Dockerfile:

FROM centos:7

RUN yum -y install mariadb mariadb-server

COPY start.sh start.sh

CMD ["/bin/bash", "start.sh"]

我的start.sh文件:

My start.sh file:

service mariadb start

Docker build:

Docker build:

docker build --tag="pbellamk/mariadb" .

Docker运行:

docker run -it -d --privileged=true pbellamk/mariadb bash

我检查了 centos:systemd 图像,它也无济于事。如何使用通过systemctl / service命令启动的服务启动容器。

I have checked the centos:systemd image and that doesn't help too. How do I launch the container with the services started using systemctl/service commands.

推荐答案

执行使用 bash 作为命令的docker run 命令,初始化系统(例如SystemD)不会启动(启动脚本也不会启动),因为该命令您传递的内容将覆盖Dockerfile中的 CMD )。尝试将您使用的命令更改为 / sbin / init ,使用 -d 在守护程序模式下启动容器,然后然后使用 docker exec -it< container id>在shell中环顾四周sh

When you do docker run with bash as the command, the init system (e.g. SystemD) doesn’t get started (nor does your start script, since the command you pass overrides the CMD in the Dockerfile). Try to change the command you use to /sbin/init, start the container in daemon mode with -d, and then look around in a shell using docker exec -it <container id> sh.

这篇关于在docker容器中使用systemctl启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆