简体   繁体   English

使用docker-py从现有容器运行Docker容器

[英]Run a docker container from an existing container using docker-py

I have a Docker container which runs a Flask application. 我有一个运行Flask应用程序的Docker容器。 When Flask receives and http request, I would like to trigger the execution of a new ephemeral Docker container which shutdowns once it completes what it has to do. 当Flask收到并发出http请求时,我想触发一个新的临时Docker容器的执行,该容器在完成其工作后便会关闭。

I have read Docker-in-Docker should be avoided so this new container should be run as a sibling container on my host and not within the Flask container. 我已经读过Docker-in-Docker应该避免使用,因此这个新容器应作为同级容器在我的主机上而不是在Flask容器内运行。

What would be the solution to do this with docker-py ? docker-py做到这一点的解决方案是什么?

we are doing stuff like this by mounting docker.sock as shared volume between the host machine and the container. 我们通过将docker.sock作为主机和容器之间的共享卷挂载来进行类似的操作。 This allows the container sending commands to the machine such as docker run 这允许容器将命令发送到机器,例如docker run

this is an example from our CI system: 这是我们CI系统的一个示例:

 volumes:
 - /var/run/docker.sock:/var/run/docker.sock

Answering my own question. 回答我自己的问题。 Here is a complete setup which works. 这是一个完整的设置,可以正常工作。 In one folder, create the following files: 在一个文件夹中,创建以下文件:

  • requirements.txt requirements.txt
  • Dockerfile Docker文件
  • docker-compose.yml docker-compose.yml
  • api.py api.py

requirements.txt requirements.txt

docker==3.5.0
flask==1.0.2

Dockerfile Docker文件

FROM python:3.7-alpine3.7


# Project files
ARG PROJECT_DIR=/srv/api
RUN mkdir -p $PROJECT_DIR
WORKDIR $PROJECT_DIR
COPY requirements.txt ./

# Install Python dependencies
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

docker-compose.yml docker-compose.yml

Make sure to mount docker.sock in volumes as mentioned in the previous answer above. 确保按以上上一个答案中所述将docker.sock安装在卷中。

version: '3'
services:
  api:
    container_name: test
    restart: always
    image: test
    build:
      context: ./
    volumes:
      - ./:/srv/api/
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      FLASK_APP: api.py
    command: ["flask", "run", "--host=0.0.0.0"]
    ports:
      - 5000:5000

api.py api.py

from flask import Flask
import docker
app = Flask(__name__)


@app.route("/")
def hello():
    client = docker.from_env()
    client.containers.run('alpine', 'echo hello world', detach=True, remove=True)
    return "Hello World!"

Then open your browser and navigate to http://0.0.0.0:5000/ 然后打开浏览器并导航到http://0.0.0.0:5000/

It will trigger the execution of the alpine container. 它将触发高山容器的执行。 If you don't already have the alpine image, it will take a bit of time the first time because Docker will automatically download the image. 如果您还没有高山映像,那么第一次将花费一些时间,因为Docker将自动下载该映像。

The arguments detach=True allows to execute the container asynchronously so that Flask does not wait for the end of the process before returning its response. 参数detach=True允许异步执行容器,以便Flask在返回响应之前不会等待过程结束。

The argument remove=True indicates Docker to remove the container once its execution is completed. 参数remove=True表示Docker一旦执行完毕就将其删除。

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

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