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

查看:95
本文介绍了在 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 构建:

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 时,init 系统(例如 SystemD)不会'不要开始(你的启动脚本也没有,因为你传递的命令覆盖了 Dockerfile 中的 CMD).尝试将您使用的命令更改为 /sbin/init,使用 -d 以守护模式启动容器,然后使用 docker exec 在 shell 中查看-it <容器ID>嘘.

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天全站免登陆