Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart a docker container from another running container

I am using docker-compose for deployment. I want to restart my "centos-1" container from "centos-2" container. Both containers are running on the same host. Please suggest, How could I achieve this in a simplest and automated way?

I followed How to run shell script on host from docker container? and tried to run a script on Host from "centos-2" container, but the script is executing inside a container and not on the host.

Script:

    #!/bin/bash
    sudo docker container restart centos-1

Error:

    line 2: docker: command not found

(Docker isn't installed inside any centos-2 container)

like image 973
Prashant Shetage Avatar asked Jan 27 '23 07:01

Prashant Shetage


1 Answers

You need:

  1. Install docker CLI (command line interface) on second container. Do not confuse with full scale installation - you dont need docker daemon, only command line tool (docker executable)

  2. Share you host's docker daemon (service) to make it accessible in second container. That is achieved with simply sharing /var/run/docker.sock when launching 2nd container, example:

    docker run ... -v "/var/run/docker.sock:/var/run/docker.sock" container2 ...
    
  3. Now you can execute any docker command, like docker stop from second container and these commands are happily passed to your main (and the only) docker daemon.

like image 80
grapes Avatar answered Jan 30 '23 05:01

grapes