从其他容器启动和停止Docker容器 [英] Starting and stopping docker container from other container

查看:206
本文介绍了从其他容器启动和停止Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从另一个容器内部启动,停止和重新启动容器。

I need to start, stop and restart containers from inside another container.

例如:


Container A->启动容器B

Container A -> start Container B

容器A->停止Container C

Container A -> stop Container C

我的Dockerfile:

My Dockerfile:

FROM node:7.2.0-slim

WORKDIR /docker
COPY . /docker
CMD [ "npm", "start" ]

Docker Version 1.12.3

我想避免使用ssh连接。有想法吗?

I want to avoid using a ssh connection. Any Ideas?

推荐答案

容器本身在隔离的环境中运行(例如,具有自己的文件系统或网络堆栈),因此无法直接与其运行的主机进行交互。

Per se a container runs in an isolated environment (e.g. with its own file system or network stack) and thus has no direct way to interact with the host it is running on. This is of course intended that way to allow for real isolation.

但是有一种方法可以运行具有更多特权的容器。要与主机上的docker守护进程对话,您可以例如将主机系统的docker套接字安装到容器中。

But there is a way to run containers with some more privileges. To talk to the docker daemon on the host, you can for example mount the docker socket of the host system into the container. This works the same way as you probably would mount some host folder into the container.

docker run -v /var/run/docker.sock:/var/run/docker.sock yourimage

例如,请参阅docker traefik代理的-compose文件,该过程侦听主机上容器的启动和停止,以激活到它们的一些代理路由。您可以在traefik代理存储库中找到示例

For an example, please see the docker-compose file of the traefik proxy which is a process that listenes for starting and stopping containers on the host to activate some proxy routes to them. You can find the example in the traefik proxy repository.

要能够与主机上的docker守护进程通信,您还需要在容器中安装docker客户端或使用某些docker api进行编程语言。在docker docs中有一个此类库针对不同编程语言的官方列表

To be able to talk to the docker daemon on the host, you then also need to have a docker client installed in the container or use some docker api for your programming language. There is an official list of such libraries for different programming languages in the docker docs.

当然,您应该知道为容器赋予了哪些特权。设法利用您的应用程序的人可能会关闭您的其他容器,或者甚至更糟的是,在系统上启动自己的容器,这些容器很容易用来控制系统。在构建应用程序时请记住这一点。

Of course you should be aware of what privileges you give to the container. Someone who manages to exploit your application could possibly shut down your other containers or - even worse - start own containers on your system which can easily be used to gain control over your system. Keep that in mind when you build your application.

这篇关于从其他容器启动和停止Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆