Docker-从容器内部运行容器 [英] Docker - Run Container from Inside Container

查看:98
本文介绍了Docker-从容器内部运行容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个应用程序:

  • 一个执行简短任务并退出的Python控制台脚本
  • 一个Flask前端",用于通过传递命令行参数来启动控制台应用程序

当前,Flask项目带有控制台脚本的副本,并在必要时使用 subprocess 运行它.这在Docker容器中效果很好,但是它们之间的耦合太紧密了.在某些情况下,我想从命令行运行控制台脚本.

Currently, the Flask project carries a copy of the console script and runs it using subprocess when necessary. This works great in a Docker container but they are too tightly coupled. There are situations where I'd like to run the console script from the command line.

我想将两个应用程序分离到单独的容器中.为了使这项工作有效,Flask应用程序需要能够在单独的容器(可以在其他计算机上)中启动控制台脚本.理想情况下,我不想在Flask容器中运行控制台脚本容器,因此每个容器仅运行一个进程.另外,我将需要能够传递控制台脚本命令行参数.

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). 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. Plus I'll need to be able to pass the console script command line arguments.

问:如何从容器内部生成具有短暂任务的容器?

推荐答案

您只需授予容器访问权限即可执行docker命令.它要么需要直接访问docker套接字,要么需要各种tcp环境变量和文件(客户端证书等).显然,它还需要在容器上安装一个 docker客户端.

You can just give the container access to execute docker commands. It will either need direct access to the docker socket or it will need the various tcp environment variables and files (client certs, etc). 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 run -v /var/run/docker.sock:/var/run/docker.sock your_image

重要的是要注意,这与在容器中运行docker守护程序不同.为此,您需要一个 jpetazzo/dind 之类的解决方案.

It's important to note that this is not the same as running a docker daemon in a container. For that you need a solution like jpetazzo/dind.

这篇关于Docker-从容器内部运行容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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