Use systemd in Docker

Source: Internet
Author: User

Use systemd in Docker
Docker's official CentOS image does not provide the systemd service for the following reasons:

  • Systemd requiresCAP_SYS_ADMINCapability. 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-f S.tar get. 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. create a systemd base image: [root @ fei ~] # 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
Under Successfully built b519e838116f, you can create an http Service Image Based on this image. Of course, you need another Dockerfile: 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 ") or you can run the following command in the container: # docker exec 605bae76456d ps-efUID pid ppid c stime tty time CMDroot 1 0? 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 accessed port 8081 of the server and found that Apache started up normally.

Install Docker in CentOS 6/7 Series

Detailed explanation of the entire process of building Gitlab CI for Docker

Docker installation application (CentOS 6.5_x64)

Use MySQL in Docker

Install Docker on Ubuntu Trusty 14.04 (LTS) (64-bit)

Docker installation application (CentOS 6.5_x64)

Docker details: click here
Docker: click here

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.