【问题标题】:How to run systemctl enable xxx during docker build?如何在 docker build 期间运行 systemctl enable xxx?
【发布时间】:2020-04-08 02:54:22
【问题描述】:

我希望这个问题能让你一切都好。

我目前正在尝试创建 Docker 容器映像,但遇到了问题。 我最初对 Dockerfile 的想法如下:

FROM centos:7
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]

并使用docker build -t baseimage . 将其保存为基本图像。到目前为止一切顺利。

应用程序的安装程序是一个 .run,其中执行类似 systemctl start xxx.service 之类的内容(我无法更改它,如果这部分失败,安装将失败)。

我已经为第二个Dockerfile尝试了一些东西,例如:

FROM baseimage
...
COPY xxx.run xxx.run
RUN ./xxx.run # This return an error like "Failed to get D-Bus connection: Operation not permitted"
...

并更改原始 CMD 以运行此脚本:

#! /bin/bash
/usr/sbin/init # With and without & after this one
./xxx.run

有什么想法吗?

【问题讨论】:

  • 你可以在虚拟机中运行这个应用程序吗? Systemd 非常不适合在 Docker 中运行,如果安装程序期望 systemctl start 某些东西,那么在 Systemd 运行时实现这一点将非常棘手。

标签: docker dockerfile containers


【解决方案1】:

您的构建正在尝试执行特权操作。问题是,--privileged 标志在构建时不可用。这是一个有趣的问题:https://github.com/moby/moby/issues/1916

TL;DR docker 本身似乎有两种可能性

docker 之外的其他解决方案是使用buildah,但到目前为止,这是一个更长的镜头。但有趣的是,特别是如果您在 RedHat 生态系统中工作,因为他们希望摆脱 Docker 以支持 Buildah/Podman。这是get started。虽然我不清楚它是如何解决您的问题的,但它已被强调为能够处理此类问题。

【讨论】:

    【解决方案2】:

    在 docker 容器中为非 dockerized 应用程序使用安装程序的情况......是与 docker-systemctl-replacement 一起工作的成就之一。

    【讨论】:

      猜你喜欢
      • 2017-05-09
      • 2019-07-04
      • 2019-03-06
      • 2017-08-27
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 2020-10-28
      相关资源
      最近更新 更多