Using SYSTEMd in docker

Label DockerOpenstack
2366 people read comment(0) Collection Report
Classification:
Due to the following several reasons, docker's official CentOS mirror did not provide SYSTEMd services:

  • Requires the SYSTEMdCAP_SYS_ADMINCapability. Means running docker with This--privilegedGood for a base image. Not
  • Requires access to the SYSTEMdCgroupsFilesystem.
  • SYSTEMdA number has of unit files that don t matter in a container and they cause errors if they re Removed not

But in a controlled environment, we still want to use SYSTEMd to manage our services, how to open SYSTEMd?

First create a Dockerfile to create the base image systemd:

Centos:latest FROM
Feisky MAINTAINER
Yum -y install SYSTEMd systemd-libs RUN
Yum -y update #RUN
Yum clean all RUN; \
(/lib/systemd/system/sysinit.target.wants/ for; I in CD *; \
Do [$i] || RM -f = = systemd-tmpfiles-setup.service $i; done) \;
-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;
VOLUME ["/sys/fs/cgroup"]
CMD ["/usr/sbin/init"]

implementBuild --rm -t centos:systemd docker.To create a base image: SYSTEMd
[Root@feiDocker build --rm -t centos:systemd ~]#.
Build context to Docker Sending daemon 490 MB
Build context to Docker daemon Sending
Step 0: centos:latest FROM
I b157b77b1a65
Step 1: feisky MAINTAINER
I Using cache
I 4add1a12ff67
Step 2: Yum -y install SYSTEMd systemd-libs RUN
I Using cache
I 2bc5274f7dd2
Step 3: run Yum clean all; (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/*;
I Running in d132481218d7
Plugins: fastestmirror Loaded
Repos: base extras updates Cleaning
Up everything Cleaning
Up list of fastest mirrors Cleaning
I 7a9492819396
Intermediate container d132481218d7 Removing
Step 4: /sys/fs/cgroup VOLUME
I Running in d5a93c8ee296
I 10b06676ea4d
Intermediate container d5a93c8ee296 Removing
Step 5: /usr/sbin/init CMD
I Running in 6ab276a1974b
I b519e838367f
Intermediate container 6ab276a1974b Removing
Built b519e838367f Successfully

The following can be based on the mirror to create a mirror image of the HTTP service, of course, here you need another Dockerfile:

Centos:systemd FROM
Feisky MAINTAINER
Yum -y install httpd systemctl; enable httpd.service RUN
Yum -y install mariadb-server MariaDB systemctl; enable RUNMariaDB.service
Yum clean all RUN
EXPOSE 80
CMD ["/usr/sbin/init"]

implementBuild -t http_server docker.To create a HTTP image.

After the image is created, you can use the following command to start docker http.--privileged):

Run --privileged -d -v docker /sys/fs/cgroup:/sys/fs/cgroup:ro -h server.http.local -p 8081:80 -t http_server


Through the following command you can enter the shell docker3df7d8c59f1eFor container ID):

Nsenter --uts --ipc --net --pid --target --mount $(inspect --format docker "{{.State.Pid}}" "605bae76456d")

Or you can use the following command to execute commands in the container:

# docker exec 605bae76456d PS -ef
C的时间估计PID PPID UID tty命令
1 0 0 07:33根?00:00:00 / / usr/ init
根15 1 0 07:33?00:00:00/usr/lib/systemd / systemd - journald
16:33 07 1 0根?00:00:00 / usr/ / httpd 我dforeground
APACHE 0 07 18 16:33?00:00:00 / usr/ / httpd 我dforeground
16:33 19 0 07 Apache?00:00:00 / usr/ / httpd 我dforeground
APACHE 0 07 20 16:33?00:00:00 / usr/ / httpd 我dforeground
16:33 21 0 07 Apache?00:00:00 / usr/ / httpd 我dforeground
16:33 22 0 07 Apache?00:00:00 / usr/ / httpd 我dforeground
dbus 1 187 0 07:41?00:00:00 /斌/ - dbus程序 address=—————————systemd nofork nopidfile - systemd活化
248 41 1 0 07:MySQL吗?00:00:00 SH/usr/bin//bin/_ mysqld safe - basedir=/usr
MySQL 404 1 07:248 41?00:00:00/usr/libexec / mysqld - basedir==/usr/var/- datadir -插件-库/ MySQL / /usr目录= lib64 / MySQL /插件-日志/日志/误差=/var/mariadb.log mariadb—PID文件/ var /运行/ mariadb=/=/var mariadb.pid -插座mysql.sock MySQL库/ / /
41 0 0 07:458根?00:00:00 PS—弗

访问服务器的8081端口发现apache正常启动起来了。

0
0
猜你在找
查看评论
*以上用户言论只代表其个人观点,不代表csdn网站的观点或立场
    个人资料
    • 访问:321208次
    • 积分:7852
    • 等级:
    • 排名:第1361名
    • 原创:472篇
    • 转载:126篇
    • 原文:2篇
    • 评论:17条
    文章分类