I made a fastAPI docker container, and it's running fine, unless 1 command...
I need it to restart another docker container when some event happen.

I have a server[host], running docker, docker is running containerA (fastAPI) and containerB ...
How can I make containerA run a command that will make the host execute this command:

docker restart containerB

Note, the host OS is linux.

I tried to use python's subprocess.run() , but it didn't work of course , containerA doesn't have docker installed, so of course it has no docker commands.
I've read articles saying that mounting unix socket will work, I tried mounting '/var/run/docker.sock:/var/run/docker.sock' but it didn't work as well.

If you have an idea how to make it work, I'd be grateful.

  • 1
    Mounting /var/run/docker.sock is indeed the usual approach for this; what didn’t work in your case? Commented Feb 16 at 13:03
  • I simply got "subprocess.CalledProcessError: Command '['docker', 'restart', 'v2fly']' returned non-zero exit status 127." It didn't found "docker" command Commented Feb 16 at 13:08

2 Answers 2


Mounting /var/run/docker.sock inside the container is the usual approach for this, as long as you’re comfortable exposing your host’s Docker daemon to the container. For this to work, you also need to ensure the Docker client binary (docker) is available inside the container; in your case it isn’t, you need to add it to the image.

  • 1
    This is not the docker's recommended way. Mine is. Commented Feb 16 at 13:17
  • 1
    I explained what the caveat is. Commented Feb 16 at 13:25
  • Both give me insights to possible solutions... to install docker client to container you mean "RUN apt install docker-ce" inside the container? Or something else? Commented Feb 16 at 13:27
  • 1
    @AbdAlhaleemBakkor apt install docker.io if you’re using a Debian base image (or a derivative), dnf install moby-engine if you’re using a Fedora base image (or a derivative). Commented Feb 16 at 13:29
  • problem solved, it worked using after installing docker.io Commented Feb 16 at 16:42

This would be a better design to mount bind a directory, then create a file on the mount between host and container, and from host look changes on this directory, then restart docker container.

To bind mount, as usual from host:

docker run ... -v /host/path:/container/path:rw

On host, check directory:

while true; do
    if test -e "$dir/test"; then
        docker restart containerX
        rm -f "$dir/test"
    sleep 1

From the container:

touch "$dir/test"


Even if it's technically possible to share the docker's socket to run docker commands inside containers, docker's experts suggest to not do because of security reasons.

Read about DooD and DinD, maybe podman is better for security if you insist to use that approach. podman don't have a root service running like docker.

  • you mean making the container execute a shell script? I'ts still the same the container doesn't have a docker installed.. It's not about mounting a volume, I already know how to do that, it's about restarting a container running on the host machine. Commented Feb 16 at 13:02
  • 1
    Added example. Read again Commented Feb 16 at 13:06
  • I see, but is there any better way to do it? I hate endless loops, many things could go wrong there, and it's a waste of resources. Commented Feb 16 at 13:09
  • 1
    This is just an example, you could use inotifywait (from inotify-tools) to trigger actions on changes... Commented Feb 16 at 13:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .