4

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)

Wie
  • 422
  • 4
  • 20
Prashant Shetage
  • 167
  • 1
  • 3
  • 16
  • 1
    You can’t do this without giving the “centos-2” container unrestricted root-level access over the host: be very very careful with security concerns if you try one of the answers below. A better approach might be an HTTP endpoint on “centos-1” that caused it to shut itself down, without anything Docker-specific. – David Maze May 21 '19 at 13:39

2 Answers2

6

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.

grapes
  • 8,185
  • 1
  • 19
  • 31
3

There is a approach from the CI-context to control the Docker Daemon on System from a running container called Docker-out-of-Docker (DooD):

  1. you have to install docker inside your container
  2. Map you docker installation from your system inside your container using volumes
  -v /var/run/docker.sock:/var/run/docker.sock

Now each docker command inside your container are execute on the system docker installation. E.g. if you type docker image list inside your container there should be the same list as if your type the command on your system.

Wie
  • 422
  • 4
  • 20