Docker's official centos image does not provide the systemd service for the following reasons:
- Systemd requires
CAP_SYS_ADMIN
Capability. This means running docker--privileged
. Not good for a base image.
- Systemd requires access to the cgroups filesystem.
- Using EMD has a number of unit files that don't matter in a iner, and they cause errors if They're not removed
However, in a controllable environment, we still want to use systemd to manage our services. How can we enable systemd?
First, create a dockerfile to create the base image of systemd:
From centos: latestmaintainer feiskyrun Yum-y install systemd-libs # Run Yum-y updaterun Yum clean all; \ (CD/lib/systemd/system/sysinit.tar get. 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.tar get. wants/* udev *; \ Rm-F/lib/systemd/system/sockets.tar get. wants/* initctl *; \ Rm-F/lib/systemd/system/basic.tar get. wants/*; \ Rm-F/lib/systemd/system/anaconda.tar get. wants/*; volume ["/sys/fs/cgroup"] cmd ["/usr/sbin/init"]
Run docker build -- Rm-T centos: systemd. To create a systemd base image: [email protected] ~] # Docker build -- Rm-T centos: systemd.
Sending build context to docker daemon 490 MB
Sending build context to docker daemon
Step 0: From centos: Latest
---> B157b77b1a65
Step 1: maintainer feisky
---> Using Cache
---> 4add1a12ff67
Step 2: Run Yum-y install systemd-libs
---> Using Cache
---> 2bc5274f7dd2
Step 3: Run Yum clean all; (CD/lib/systemd/system/sysinit.tar get. 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.tar get. wants/* udev *; RM-F/lib/systemd/system/sockets.tar get. wants/* initctl *; RM-F/lib/systemd/system/basic.tar get. wants/*; RM-F/lib/systemd/system/anaconda.tar get. wants /*;
---> Running in d132481218d7
Loaded plugins: fastestmirror
Cleaning repos: Base extras updates
Cleaning up everything
Cleaning up list of fastest mirrors
---> 7a9425819396
Removing intermediate container d132481218d7
Step 4: volume/sys/fs/cgroup
---> Running in d5a93c8ee296
---> 10b06676ea4d
Removing intermediate container d5a93c8ee296
Step 5: CMD/usr/sbin/init
---> Running in 6ab276a1974b
---> B519e838366f
Removing intermediate container 6ab276a1974b
Successfully built b519e838366f
You can create an HTTP Service Image Based on this image. Of course, another dockerfile is required here:
From centos: systemdmaintainer feiskyrun Yum-y install httpd; systemctl enable httpd. servicerun Yum-y install mariadb-server mariadb; systemctl enable mariadb. servicerun Yum clean allexpose 80cmd ["/usr/sbin/init"]
Run docker build-T http_server. To create an HTTP image.
After the image is created, run the following command to start HTTP docker (note that -- privileged is required ):
Docker run -- privileged-D-V/sys/fs/cgroup: Ro-H server. http. Local-P 8081: 80-T http_server
Run the following command to enter the docker shell (where 3df7d8c59f1e is the container ID ):
Nsenter -- Mount -- UTs -- IPC -- Net -- PID -- target $ (docker inspect -- format "{. state. PID}" "605bae76456d ")
You can also run the following command in the container:
# Docker exec 605bae76456d PS-efuid PID ppid C stime tty time cmdroot 1 0 0 07:33? 00:00:00/usr/sbin/initroot 15 1 0 07:33? 00:00:00/usr/lib/systemd-journaldroot 16 1 0? 00:00:00/usr/sbin/httpd-dforegroundapache 18 16 0 07:33? 00:00:00/usr/sbin/httpd-dforegroundapache 19 16 0 07:33? 00:00:00/usr/sbin/httpd-dforegroundapache 20 16 0 07:33? 00:00:00/usr/sbin/httpd-dforegroundapache 21 16 0 07:33? 00:00:00/usr/sbin/httpd-dforegroundapache 22 16 0 07:33? 00:00:00/usr/sbin/httpd-dforeground.pdf 187 1 0? 00:00:00/bin/logs-daemon -- System -- Address = systemd: -- nofork -- nopidfile -- systemd-activationmysql 248 1 0? 00:00:00/bin/sh/usr/bin/mysqld_safe -- basedir =/usrmysql 404 248 1? 00:00:00/usr/libexec/mysqld -- basedir =/usr -- datadir =/var/lib/MySQL -- Plugin-Dir =/usr/lib64/MySQL/plugin -- log-error =/ var/log/mariadb. log -- PID-file =/var/run/mariadb. PID -- socket =/var/lib/MySQL. sockroot 458 0 0 07:41? 00:00:00 PS-ef
Access port 8081 of the server and check that Apache is started properly.
Use systemd in docker