How to restart a systemd service from docker container?

Hi all,
Maybe somebody knows how to restart a daemon (which is running as a systemd service) from the docker container? I developed a python script to do that and built an image using a simple Dockerfile:

FROM python:3.8
CMD [ "python3", "./" ]

and container built using docker-compose:

version: '2.4'
    image: my-script-image:latest
    container_name: my-script
    restart: unless-stopped
      - network
      driver: json-file
        max-size: "50m"
        max-file: "10"
    driver: "bridge"
    driver_opts: <NETWORK-NAME>

Should I describe any specific user in my Dockerfile? When I execute a script from the command line (RHEL8) - everything works fine, it can restart the daemon (but only if you use the ‘root’ user), but when a script is running inside a container - it doesn’t work (while ‘docker logs --tail’ show that everything works fine, but daemon doesn’t restart).

Without seeing what is in your python script, I don’t know how you try to restart a service on the host, but the point of a container is that it is isolated from the host so you can’t directly restart anything outside the container. Maybe if you use the process namespace of the host and also use privileged container or proper capabilities.

Similarly to the “black box” python script, I don’t know what that command says, but the fact that your container works, doesn’t mean that it has access to the host.

It doesn’t matter.

If you use the host’s process namespace (pid: host) you can send a signal to the processes on the host. If you know which signal restarts the process, you can do that without accessing systemd. The command with which you can send signals is “kill” or “pkill”. The first expects process ID, the other expects a process name, but that could mean multiple processess.

1 Like