首页 > 解决方案 > solita/docker-systemd docker-compose

问题描述

我想在 docker-compose 中使用带有 systemctl 的图像,我在互联网上看到了这个图像https://github.com/solita/docker-systemd,它工作得很好,但是当我尝试将它与 docker- 一起使用时撰写它不工作(它工作但 systemctl 不工作,它给出此错误“系统尚未使用 systemd 作为初始化系统(PID 1)启动。无法运行。”)

  test1:
container_name: 'test1'
build: './test'
volumes:
  - /:/host
  - /sys/fs/cgroup:/sys/fs/cgroup:ro
security_opt:
  - "seccomp=unconfined"  
tmpfs:
  - /run
  - /run/lock
privileged: true

构建文件是 test.sh

#!/bin/sh
set -eu
if nsenter --mount=/host/proc/1/ns/mnt -- mount | grep /sys/fs/cgroup/systemd >/dev/null 2>&1; then
  echo "The systemd cgroup hierarchy is already mounted at /sys/fs/cgroup/systemd."
else
  if [ -d /host/sys/fs/cgroup/systemd ]; then
    echo "The mount point for the systemd cgroup hierarchy already exists at /sys/fs/cgroup/systemd."
  else
    echo "Creating the mount point for the systemd cgroup hierarchy at /sys/fs/cgroup/systemd."
    mkdir -p /host/sys/fs/cgroup/systemd
  fi
  echo "Mounting the systemd cgroup hierarchy."
  nsenter --mount=/host/proc/1/ns/mnt -- mount -t cgroup cgroup -o none,name=systemd /sys/fs/cgroup/systemd
fi
echo "Your Docker host is now configured for running systemd containers!"

标签: dockerdocker-compose

解决方案


如果您想在 docker 中运行“systemctl”来启动/停止服务,那么您可以在没有 systemd 的情况下执行此操作。docker -systemctl-replacement就是为此而设计的。


推荐阅读