简体   繁体   English

Docker - 从内部容器运行容器

[英]Docker - Run Container from Inside Container

I have two applications: 我有两个应用程序:

  • a Python console script that does a short(ish) task and exits 执行简短(ish)任务并退出的Python控制台脚本
  • a Flask "frontend" for starting the console app by passing it command line arguments Flask“frontend”,用于通过传递命令行参数来启动控制台应用程序

Currently, the Flask project carries a copy of the console script and runs it using subprocess when necessary. 目前,Flask项目带有控制台脚本的副本,并在必要时使用subprocess运行它。 This works great in a Docker container but they are too tightly coupled. 这在Docker容器中运行良好,但它们耦合得太紧。 There are situations where I'd like to run the console script from the command line. 在某些情况下,我想从命令行运行控制台脚本。

I'd like to separate the two applications into separate containers. 我想将两个应用程序分成不同的容器。 To make this work, the Flask application needs to be able to start the console script in a separate container (which could be on a different machine). 为了使这项工作,Flask应用程序需要能够在一个单独的容器(可能在不同的机器上)启动控制台脚本。 Ideally, I'd like to not have to run the console script container inside the Flask container, so that only one process runs per container. 理想情况下,我不想在Flask容器中运行控制台脚本容器,因此每个容器只运行一个进程。 Plus I'll need to be able to pass the console script command line arguments. 另外,我需要能够传递控制台脚本命令行参数。


Q: How can I spawn a container with a short lived task from inside a container? 问:如何从容器内部生成具有短期任务的容器?


You can just give the container access to execute docker commands. 您可以让容器访问执行docker命令。 It will either need direct access to the docker socket or it will need the various tcp environment variables and files (client certs, etc). 它要么需要直接访问docker socket,要么需要各种tcp环境变量和文件(客户端证书等)。 Obviously it will need a docker client installed on the container as well. 显然,它还需要在容器上安装docker客户端

A simple example of a container that can execute docker commands on the host: 可以在主机上执行docker命令的容器的简单示例:

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

It's important to note that this is not the same as running a docker daemon in a container. 重要的是要注意,这与在容器中运行docker守护程序不同。 For that you need a solution like jpetazzo/dind . 为此你需要像jpetazzo / dind这样的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM