You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac环境下Docker容器重启后httpd服务无法访问问题求助

容器中systemd管理httpd重启后无法访问的问题解决

问题现象

系统重启后首次启动容器可正常运行,但执行docker stop my_website再docker start my_website后,访问http://localhost无响应,wget返回无数据,仅重启Mac系统后容器才能恢复,再次重启容器问题复现。

操作环境与配置

系统信息

Client: Docker Engine - Community
  Version:           20.10.21
  API version:       1.41
  Go version:        go1.19.2
  Git commit:        baeda1f82a
  Built:             Tue Oct 25 17:53:02 2022
  OS/Arch:           darwin/arm64
  Context:           default
  Experimental:      true

System Software Overview:
  System Version: macOS 12.6 (21G115)
  Kernel Version: Darwin 21.6.0

镜像构建与容器运行命令

构建命令:

docker build --rm --no-cache -t my_image .

运行命令:

docker run --privileged --name my_website -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 80:80 -d my_image

Dockerfile内容

FROM centos/systemd
RUN yum -y install httpd; yum clean all; systemctl enable httpd.service
EXPOSE 80
CMD ["/usr/sbin/init"]

解决方案

方案1:遵循容器化最佳实践,直接运行httpd(推荐)

放弃用systemd管理httpd,直接在容器中前台运行httpd,由Docker负责进程管理,容器重启后服务自动启动。

修改后的Dockerfile:

FROM centos:latest
RUN yum -y install httpd && yum clean all
EXPOSE 80
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]

构建和运行命令:

docker build --rm --no-cache -t my_image .
docker run --name my_website -p 80:80 -d my_image

方案2:修复systemd在容器重启后的服务启动问题

若必须使用systemd,可通过调整容器参数和添加监控脚本解决:

  1. 修改Dockerfile,添加httpd状态监控脚本:
FROM centos/systemd
RUN yum -y install httpd && yum clean all
RUN systemctl enable httpd.service
# 创建监控脚本,确保httpd始终运行
RUN echo '#!/bin/bash\nwhile true; do systemctl is-active --quiet httpd || systemctl start httpd; sleep 10; done' > /usr/local/bin/monitor-httpd.sh
RUN chmod +x /usr/local/bin/monitor-httpd.sh
# 设置脚本随系统启动
RUN echo "/usr/local/bin/monitor-httpd.sh &" >> /etc/rc.d/rc.local
RUN chmod +x /etc/rc.d/rc.local
EXPOSE 80
CMD ["/usr/sbin/init"]
  1. 调整容器运行命令,优化cgroup相关参数:
docker run --privileged --cgroupns host --name my_website -v /sys/fs/cgroup:/sys/fs/cgroup:rw -p 80:80 -d my_image

内容的提问来源于stack exchange,提问作者John Difool

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 14:55:16