从另一个正在运行的容器中重启Docker容器

4
我正在使用docker-compose进行部署。 我想从“centos-2”容器重新启动我的“centos-1”容器。这两个容器在同一主机上运行。 请建议,如何以最简单和自动化的方式实现此目标?
我遵循了如何从docker容器上运行主机上的shell脚本?并尝试从“centos-2”容器上运行主机上的脚本,但是脚本在容器内执行而不是在主机上。
脚本:
    #!/bin/bash
    sudo docker container restart centos-1

错误:

    line 2: docker: command not found

1
如果您不允许“centos-2”容器对主机进行无限制的根级访问,那么您将无法完成此操作:如果您尝试以下任何答案,请非常小心安全问题。更好的方法可能是在“centos-1”上创建一个HTTP端点,使其自行关闭,而不涉及任何特定于Docker的内容。 - David Maze
2个回答

6

您需要:

  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.


完美运行。谢谢! - Prashant Shetage

3

CI环境中有一种控制正在运行着的系统中的Docker守护进程的方法,称为Docker-out-of-Docker (DooD):

  1. 您需要在容器内安装docker
  2. 使用volumes将您系统内的docker安装映射到容器中
  -v /var/run/docker.sock:/var/run/docker.sock

现在,容器内的每个Docker命令都在系统的Docker安装上执行。例如,如果在容器中键入docker image list,则应该与在系统上键入该命令时看到的列表相同。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接