Docker: “service” command works but “systemctl” command doesn't work

Multi tool use
Multi tool use


Docker: “service” command works but “systemctl” command doesn't work



I pulled centos6 image and made a container from it. I got its bash by:


$ docker run -i -t centos:centos6 /bin/bash



On the centos6 container, I could use "service" command without any problem. But when I pulled&used centos7 image:


$ docker run -i -t centos:centos7 /bin/bash



Both of "service" and "systemctl" didn't work. The error message is:


Failed to get D-Bus connection: Operation not permitted



My question is:
1. How are people developing without "service" and "systemctl" commands?
2. If I want to use, for example, httpd.service on the centos7 container, what should I do? Or maybe running services on a container is not recommended?




3 Answers
3



There is no process supervisor running inside either container. The service command in your CentOS 6 container works by virtue of the fact that it just runs a script from /etc/init.d, which by design ultimately launch a command in the background and return control to you.


service


/etc/init.d



CentOS 7 uses systemd, and systemd is not running inside your container, so there is nothing for systemctl to talk to.


systemctl



In either situation, using the service or systemctl command is generally the wrong thing to do: you want to run a single application, and you want to run it in the foreground, so that your container continues to run (from Docker's perspective, a command that goes into the background has exited, and if that was pid 1 in the container, the container will exit).


service


systemctl



How are people developing without "service" and "systemctl" commands?



They are starting their programs directly, by consulting the necessary documentation to figure out the appropriate command line.



If I want to use, for example, httpd.service on the centos7 container, what should I do? Or maybe running services on a container is recommended?



You would start the httpd binary using something like:


httpd


CMD ["httpd", "-DFOREGROUND"]





What is the difference between CMD ["httpd", "-DFOREGROUND"] and "systemctl start httpd.service"?
– lechat
Jul 1 at 3:31





systemctl depends on systemd running, which means your container must run in privileged mode and run a heavyweight daemon that is a process manager, and runs a message bus inside the container, and provides a system-level logger, and manages several kernel-level (shared outside the container) parameters, and...; and at the end once you dig down through it, systemd ultimately runs the same httpd command.
– David Maze
Jul 1 at 11:38


systemctl


systemd


systemd


httpd





@lechat httpd -DFOREGROUND is running a program directly, just like running ls or echo hello world. systemctl start httpd.service requires communicating with a running systemd process in order to make anything happen.
– larsks
Jul 1 at 12:34


httpd -DFOREGROUND


ls


echo hello world


systemctl start httpd.service


systemd



If you like to stick with service/sytemctl commands to start/stop services then you can do that in a centos7 container by using the docker-systemctl-replacement script.



I had some deployment scripts that were using th service start/stop commands on a real machine - and they work fine with a container. Without any further modification. When putting the systemctl.py script into the CMD then it will simply start all enabled services somewhat like the init-process on a real machine.



systemd is included but not enabled by default in CentOS 7 docker image. It is mentioned on the repository page along with steps to enable it.
https://hub.docker.com/_/centos/





...but in almost all cases you don't want to enable systemd (or any other process supervisor) inside your containers.
– larsks
Jul 1 at 12:34





Yes, running systemd inside container actually defeats purpose of container. But if someone is inclined to run it, its their choice.
– Tejas Sarade
Jul 1 at 12:47






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

bOQzCLMcvDVTNC
xpFg2EBQ3XIXsvm0R6XzBbX8,D9ewYI,dMdE,BFPud FoeD9xweZRXfuJJkoJUYFbAWAolGB Pj9zR7ls00H4kT,49zTuzWzZE

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters