lechat
lechat

Reputation: 467

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?

Upvotes: 0

Views: 2270

Answers (3)

Guido U. Draheim
Guido U. Draheim

Reputation: 3271

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.

Upvotes: 1

Tejas Sarade
Tejas Sarade

Reputation: 1212

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/

Upvotes: 0

larsks
larsks

Reputation: 312770

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.

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

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).

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:

CMD ["httpd", "-DFOREGROUND"]

Upvotes: 1

Related Questions