让Linux容器支持Systemd

有时候我们需要Linux系统镜像支持Systemd,以Ubuntu为例,目前官方的Docker镜像都不支持。为此,我们构建一个可以支持Systemd的Ubuntu镜像,Dockerfile如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
FROM ubuntu:18.04

ENV container docker
ENV LC_ALL C
ENV DEBIAN_FRONTEND noninteractive

RUN sed -i 's/# deb/deb/g' /etc/apt/sources.list

RUN apt-get update \
&& apt-get install -y systemd

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/*; \
rm -f /lib/systemd/system/plymouth*; \
rm -f /lib/systemd/system/systemd-update-utmp*; \
rm -rf /var/lib/apt/lists/*; \
rm -rf /tmp/*; \
rm -rf /var/tmp/*; \
apt-get clean packages; \
apt-get clean all; \
apt-get autoremove -y

VOLUME ["/sys/fs/cgroup"]

# CMD ["/lib/systemd/systemd"]
CMD ["/sbin/init"]

源码地址:ubuntu-systemd

总结

转载请包括本文地址:https://allenwind.github.io/blog/12019
更多文章请参考:https://allenwind.github.io/blog/archives/