Docker 容器 yum 安装软件,systemd 程序无法启动

当我们创建容器后,使用yum 安装一个 httpd 服务,在启动时会报错没有权限

  • 创建容器
[root@localhost ~]# docker run -itd --name httpd centos /bin/bash
fd71b36db40e91b88b7d55998def0a09cc3cc6ce1709f210ce42406f2124b0f7

  • 进入容器并安装 httpd
[root@localhost ~]# docker exec -it httpd /bin/bash
[root@fd71b36db40e /]# yum -y install httpd

  • 启动 http的
    • 当我们使用 systemctl 启动httpd 服务
    • 警告我们权限不足
[root@fd71b36db40e /]# systemctl start httpd
Failed to get D-Bus connection: Operation not permitted

1. 通过 --privileged 进行提权

  • 可以在创建容器的进行提权
    • 通过 --privileged 参数进行提权
    • --privileged 运行于/sbin/init 环境当中
[root@localhost ~]# docker run -itd --name http --privileged centos /sbin/init 
26fd217eab9ea04219f3ea1043fbae6f046d9b7b6ffb2ad7f52a2a0714395f4a
[root@localhost ~]# docker exec -it http /bin/bash
[root@26fd217eab9e /]# yum -y install httod

  • 启动 httpd
[root@26fd217eab9e /]# systemctl start httpd
[root@26fd217eab9e /]# netstat -anpt | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      188/httpd      
  • 删除 容器 http
    • 如果不及时退出容器,且删除,会长时间占用权限
[root@localhost ~]# docker rm -f http
http

2. 第二种

  • 通过查看/usr/lib/systemd/system/ 目录下 服务启动文件,来找到 ExecStart 启动方法
/usr/lib/systemd/system/
  • 创建容器
    • 且安装httpd
[root@localhost ~]# docker run -itd --name http centos /bin/bash
772e06c9f4d4b94f1b8b1c9c303de0f1124eae67f162999b43a0fea25c3863c7
[root@localhost ~]# docker exec -it http /bin/bash
[root@772e06c9f4d4 /]# yum -y install httpd

  • 查看 httpd 服务的启动方法
[root@772e06c9f4d4 /]# cat /usr/lib/systemd/system/httpd.service 
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND			//  启动方法
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful			//  重载方法
ExecStop=/bin/kill -WINCH ${MAINPID}					//  关闭方法
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target

  • 启动httpd 服务
[root@772e06c9f4d4 /]# /usr/sbin/httpd 
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message

[root@772e06c9f4d4 /]# netstat -anpt | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      98/httpd   
### 回答1: 可以参考这篇文档:https://docs.docker.com/config/containers/start-containers-automatically/,里面介绍了如何使用 Dockerfile 来定义一个容器,然后利用Cron或systemd 来实现定时执行Python程序。 ### 回答2: 在Docker容器中定时执行Python程序,你可以遵循以下步骤: 1. 创建一个Dockerfile来构建你的容器。在Dockerfile中,使用适当的基础镜像,例如python:latest。 2. 在Dockerfile中,将Python程序复制到容器中。可以使用COPY命令将.py文件复制到容器的适当位置。 3. 在Dockerfile中,安装Python程序所需的依赖。使用RUN命令运行适当的pip安装命令。 4. 在Dockerfile中,将cron安装容器中。使用RUN命令运行适当的apt-get或yum安装命令,具体取决于你所选择的基础镜像。 5. 在Dockerfile中,将cron的配置文件复制到容器中。使用COPY命令将cron配置文件复制到适当的位置。 6. 在Dockerfile中,使用CMD命令运行cron守护进程。CMD命令应该指向cron配置文件所在的位置。 7. 构建Docker镜像。在终端中,导航到包含Dockerfile的目录,并使用docker build命令构建镜像,例如:docker build -t my_python_cron . 8. 运行Docker容器。使用docker run命令运行容器,例如:docker run -it my_python_cron。 9. 检查容器中的cron任务是否正常运行。你可以通过docker logs命令查看cron守护进程是否启动,并通过docker exec命令进入容器查看Python程序是否按计划运行。 请注意,cron的配置文件可以指定Python程序在何时运行。你可以在配置文件中编写合适的cron表达式,以设置Python程序的执行时间。例如,使用cron表达式 * * * * *可以每分钟执行一次Python程序。 以上是在Docker容器中定时执行Python程序的详细文档。希望对你有所帮助。 ### 回答3: 在Docker容器中定时执行Python程序,你可以遵循以下步骤: 1. 构建Docker镜像:首先,在Dockerfile中指定基础镜像,并将Python程序和定时任务脚本(例如shell脚本)添加到镜像中。例如: ``` FROM python:3.9 COPY my_python_program.py /app/my_python_program.py COPY cron_script.sh /app/cron_script.sh ``` 2. 定义cron脚本:在cron_script.sh中,编写你的定时任务脚本。例如,可以使用crontab语法指定Python程序在每天上午10点运行: ``` #!/bin/bash echo "0 10 * * * python /app/my_python_program.py >> /app/log.txt" | crontab - cron -f ``` 3. 构建Docker容器:使用docker build命令构建你的Docker镜像,并创建Docker容器: ``` docker build -t my_python_app . docker run -d my_python_app ``` 4. 运行定时任务:使用docker exec命令进入正在运行的容器,并运行cron_script.sh脚本来设置定时任务: ``` docker exec -it <container_id> /bin/bash chmod +x /app/cron_script.sh /app/cron_script.sh ``` 现在,你的Python程序将会在每天上午10点自动执行。你可以通过检查/app/log.txt文件来查看程序的输出日志。 请注意,如果你需要在容器中持久保存日志和数据文件,应将其挂载到宿主机或使用Docker卷。此外,确保正在运行的容器和cron_script.sh文件具有可执行权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值