Docker Community Forums

Share and learn in the Docker community.

Linux service command in docker container

What do I need to do to properly run the linux service command in a docker container?

I mounted /sbin/service and /etc/init.d/functions and /usr/lib/systemd/system when running the container, but am not sure what else I need. When I run service [service_name] [command] inside the container (eg. service crond stop), I get no response and nothing happens.

I want the effect to occur on the host system. I am using RHEL 7

I used to run systemd in my containers and for that I needed to run the containers in privileged mode.

In general I would say that this is a Docker anti-pattern. But more specifically, when you’re running the system command in your container, that command is running in your container’s process namespace, not in the host’s process namespace. There is a --pid=host option in the docker run command but I believe it’s only for examining the host’s processes. No idea whether you could actually use this to start a process on the host, but might be worth at least testing. You probably will need to use the --privileged option in order for this to work at all.

This --pid option isn’t even isolated to making host processes visible, it can also make visible processes from other containers by way of --pid=container:<name or id of container>

Please report back if this does what you’re intending. I’d test it but it’s too much of a pain to do that in the Docker for Mac environment that I’ve get setup at the moment.