如何使用Docker有效地输入文件 [英] How to efficiently input files with docker

查看:48
本文介绍了如何使用Docker有效地输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始接触docker,并尝试将我使用的某些应用容器化.多亏了本教程,我才能够创建docker映像和容器,但是现在我正在尝试采用最高效,最实用的方法来做事.

I am starting to get a hand on docker and try to containerized some of the applications I use. Thanks to the tutorial I was able to create docker images and containers but now I am trying to thing about the most efficient and practical ways to do things.

要展示我的用例,我有一个python代码(我们称它为process.py),它将单个.jpg图像作为输入,对该图像进行一些操作,然后输出处理后的.jpg图像.

To present my use-case, I have a python code (let's call it process.py) that takes as an input a single .jpg image, does some operations on this image, and then output the processed .jpg image.

通常我会通过以下方式运行它:

Normally I would run it through :

python process.py -i path_of_the_input_image -o path_of_the_output_image

然后,我与docker进行连接输入/输出的方式如下.首先,我创建docker文件:

Then, the way I do the connection input/output with my docker is the following. First I create the docker file :

FROM python:3.6.8
COPY . /app
WORKDIR /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
CMD python ./process.py -i ./input_output/input.jpg -o ./input_output/output.jpg

然后在构建映像之后,我运行docker run映射一个本地文件夹与docker的input_output文件夹:

And then after building the image, I run docker run mapping the a local folder with the input_output folder of docker:

docker run -v C:/local_folder/:/app/input_output my_docker_image

这似乎可行,但并不实际,因为我必须在本地创建一个特定的文件夹才能将其安装到docker容器.所以这是我在问自己的问题:

This seems to work, but is not really practical, as I have to create locally a specific folder to mount it to the docker container. So here are the questions I am asking myself :

有更实用的做事方法吗?要直接发送一个输入文件并直接从docker容器的输出接收一个输出文件?

Is there a more practical ways of doings things ? To directly send one single input file and directly receive one single output files from the output of a docker container ?

当我运行docker映像时,发生的事情(如果我理解正确)是它将创建一个docker容器,该容器将在一次process.py一次运行我的程序,然后坐在那里什么也不做.即使在完成运行process.py之后,它仍将在命令"docker ps -a"中列出.这是预期的行为吗?有没有一种方法可以自动删除完成的容器?我使用docker是否以正确的方式运行?

When I run the docker image, what happens (If I understand correctly) is that it will create a docker container that will run my program once process.py once and then just sits there doing nothing. Even after finishing running process.py it will still be there listed in the command "docker ps -a". Is this behaviour expected ? Is there a way to automatically delete finished container ? Am I using docker run the right way ?

是否有一种更实用的方法来使容器连续运行,并且可以查询以给定输入按需运行程序process.py?

Is there a more practical way of having a container running continuously and on which I can query to run the program process.py on demand with a given input ?

推荐答案

我有一个python代码(我们称它为process.py),它将单个.jpg图像作为输入,对该图像进行一些操作,然后输出处理后的.jpg图像.

I have a python code (let's call it process.py) that takes as an input a single .jpg image, does some operations on this image, and then output the processed .jpg image.

在没有Docker的情况下,这是最有效的方法;只需运行您已有的 python 命令即可.如果您的应用程序具有有趣的Python库依赖关系,则可以将它们安装在虚拟环境,以避免与系统Python安装冲突.

That's most efficiently done without Docker; just run the python command you already have. If your application has interesting Python library dependencies, you can install them in a virtual environment to avoid conflicts with the system Python installation.

当我运行Docker映像时...

When I run the Docker image...

...容器运行其主命令( docker run 命令参数,Dockerfile CMD ,可能与某些来源的入口点结合使用),以及何时执行该命令退出,容器退出.它会在 docker ps -a 输出中列出,但显示为"Stopped".(可能状态为0表示成功完成).您可以 docker运行--rm 来使容器自动删除自身.

...the container runs its main command (docker run command arguments, Dockerfile CMD, possibly combined with an entrypoint from the some sources), and when that command exits, the container exits. It will be listed in docker ps -a output, but as "Stopped" (probably with status 0 for a successful completion). You can docker run --rm to have the container automatically delete itself.

是否有一种更实用的方法来使容器连续运行,并且可以查询以给定输入按需运行程序process.py?

Is there a more practical way of having a container running continuously and on which I can query to run the program process.py on demand with a given input ?

将其包装在网络服务中,例如Flask应用程序.只要运行,您就可以使用 curl 之类的工具执行HTTP POST,将输入的JPEG文件作为正文,并获取输出的JPEG文件作为响应.避免在任何可能的情况下都将本地文件和Docker一起使用(对于进程的输入和输出,首选网络I/O;对于本地文件存储,首选数据库).

Wrap it in a network service, like a Flask application. As long as this is running, you can use a tool like curl to do an HTTP POST with the input JPEG file as the body, and get the output JPEG file as the response. Avoid using local files and Docker together whenever that's an option (prefer network I/O for process inputs and outputs; prefer a database to local-file storage).

这篇关于如何使用Docker有效地输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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