Answers: <p>In majority of circumstances I wouldn't recommend this in production, but to restart docker container from another container, you need to install docker in that container and then run that "master" container with</p>
  
  <pre><code>docker run -v /var/run/docker.sock:/var/run/docker.sock 
  </code></pre>
  
  <p>as described here: <a href="https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/" rel="nofollow noreferrer">https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/</a> (look for "The solution" part of the article).</p>
  
  <p>But check your architecture and see if you really need to restart one container from another or if there is some other way.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-04-30T10:41:06.877Z
Answers: (Remember that any entity that can get access to the host's Docker socket can pretty trivially root the host.  Be extra careful around basic security issues, and especially when you're making Docker-related calls.)
Answers: David Maze
Answers: CC BY-SA 4.0
Answers: 2020-04-29T19:13:34.630Z
Answers: 2020-04-29T19:13:34.630Z
Answers: taleodor
Answers: <p>You can open a socket to have the docker command available inside a container:</p>
  
  <p><a href="https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337" rel="nofollow noreferrer">https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337</a></p>
  
  <p>But...even if technically possible docker experts suggest to not do because of security reasons.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-05-01T00:34:15.640Z
Answers: 2020-05-01T00:34:15.640Z
Answers: g3k0
Body: <p>I'm in a planning phase for a little tool for myself. I have to say that I have close to 0 experience with docker.</p>
  
  <p><strong>My setup</strong></p>
  
  <ul>
  <li>1 x Nodejs App in a Container (lets call it <em>App</em>)</li>
  <li>n x Instances/Containers of the same image (lets call it <em>Agent</em>)</li>
  </ul>
  
  <p><strong>What I want</strong>  </p>
  
  <p>I'm looking for a way to restart one of the <em>Agents</em> from within my <em>App</em>.</p>
  
  <p><strong>The problem</strong><br>
  I just managed to create my first docker-compose. From here I'm stuck. I just have no idea how to cross-access the container from my <em>Apps</em> container (by script) or if this is even possible and then force to restart it (like <code>docker restart #hash</code> does).</p>
  
  <p><strong>My current docker-compose.yml</strong> (if that helps in any way)  </p>
  
  <pre><code>version: '3'
  services:
      app:
          container_name: app
          volumes: 
              - .:/usr/src/app
          restart: always
          build: .
          ports:
              - '8080:3000'
          links:
              - agent
  
      # no control over this as this is an existing image from docker hub
      agent:
          restart: always
          image: image/from/dockerhub
          ports:
              - '1234'
  </code></pre>
  
  <p>... which I would run as <code>docker-compose up --scale agent=n</code>.  </p>
  
  <p>I'm not necessarily looking for a complete solution. If someone could guide me into the right direction I would really appreciate it.</p>
  
  <p>Thanks</p>
  
Comments: CC BY-SA 4.0
Comments: 2020-04-29T17:02:58.357Z
Comments: What's the IP address of containers? And, how you implemented the networking section?
Comments: Mohi Rostami
Comments: CC BY-SA 4.0
Comments: 2020-04-29T19:09:03.757Z
Comments: What do you mean which IP address? Its virtual. Which networking section? I've got nothing more than in the question.
Comments: C4d
Tags: <docker><docker-compose>
Title: Restart docker container from another container
UserDisplayName: C4d
ThreadText: # Restart docker container from another container
  
  (Asked by: username_0 on 2020-04-29)
  
  I'm in a planning phase for a little tool for myself. I have to say that I have close to 0 experience with docker.
  
  **My setup**
  
  * 1 x Nodejs App in a Container (lets call it *App*)
  * n x Instances/Containers of the same image (lets call it *Agent*)
  
  **What I want**
  
  I'm looking for a way to restart one of the *Agents* from within my *App*.
  
  **The problem**  
  I just managed to create my first docker-compose. From here I'm stuck. I just have no idea how to cross-access the container from my *Apps* container (by script) or if this is even possible and then force to restart it (like `docker restart #hash` does).
  
  **My current docker-compose.yml** (if that helps in any way)
  
  ```
  version: '3'
  services:
      app:
          container_name: app
          volumes: 
              - .:/usr/src/app
          restart: always
          build: .
          ports:
              - '8080:3000'
          links:
              - agent
  
      # no control over this as this is an existing image from docker hub
      agent:
          restart: always
          image: image/from/dockerhub
          ports:
              - '1234'
  
  ```
  
  ... which I would run as `docker-compose up --scale agent=n`.
  
  I'm not necessarily looking for a complete solution. If someone could guide me into the right direction I would really appreciate it.
  
  Thanks
  
  **Tags:** docker, docker-compose
  
  > **Comments:**
  > **username_1**: What's the IP address of containers? And, how you implemented the networking section?  
  > **username_0**: What do you mean which IP address? Its virtual. Which networking section? I've got nothing more than in the question.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2020-04-29. Score: 2 | Accepted answer
  
  In majority of circumstances I wouldn't recommend this in production, but to restart docker container from another container, you need to install docker in that container and then run that "master" container with
  
  ```
  docker run -v /var/run/docker.sock:/var/run/docker.sock 
  
  ```
  
  as described here: <https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/> (look for "The solution" part of the article).
  
  But check your architecture and see if you really need to restart one container from another or if there is some other way.
  
  > **Comments:**
  > **username_3**: (Remember that any entity that can get access to the host's Docker socket can pretty trivially root the host.  Be extra careful around basic security issues, and especially when you're making Docker-related calls.)  
  
  ---
  
  ### Answer by username_4 on 2020-05-01. Score: 0
  
  You can open a socket to have the docker command available inside a container:
  
  <https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337>
  
  But...even if technically possible docker experts suggest to not do because of security reasons.
  
  
  ---
  
  