solita/docker系统docker compose

solita/docker系统docker compose,docker,docker-compose,Docker,Docker Compose,我想在docker compose中使用一个带有systemctl的图像,我在互联网上看到了这个图像,它工作得很好,但是当我尝试将它与docker compose一起使用时,它不工作(它工作,但systemctl不工作,它给出了这个错误“系统没有以systemd作为初始系统启动(PID 1)。无法运行。”) 构建文件是test.sh #!/bin/sh set -eu if nsenter --mount=/host/proc/1/ns/mnt -- mount | grep /sys/fs/c

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

构建文件是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!"

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

如果可能的话,我建议不要使用systemd。尤其是Dockerfile有一个特权容器,其中装载了整个主机文件系统;您几乎与主机没有任何隔离。最佳实践是将服务作为前台进程作为容器的默认CMD运行。
#!/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!"