[英]How to run different python scripts in parallel containers from same image
I am trying to do something very simple (I think), I want do build a docker image and launch two different scripts from the same image in parallel running containers. 我正在尝试做一个非常简单的事情(我认为),我想构建一个docker映像并在并行运行的容器中从同一映像启动两个不同的脚本。
Something as simple as 像这样简单
Container 1 -> print("Hello") 容器1-> print(“ Hello”)
Container 2 -> print('World") 容器2->打印(“世界”)
I did some research but some techniques seem a little over engineered and others do something like 我做了一些研究,但有些技术似乎有些过分设计,而有些则做了类似的事情
CMD ["python", "script.py"] && ["python", "script2.py"]
which, isn't what I'm looking for. 这不是我想要的。
I would love to see something like 我很想看到类似的东西
$ docker ps
CONTAINER ID IMAGE CREATED STATUS NAMES
a7e789711e62 67759a80360c 12 hours ago Up 2 minutes MyContainer1
87ae9c5c3f84 67759a80360c 12 hours ago Up About a minute MyContainer2
But running two different scripts. 但是运行两个不同的脚本。
I'm still fairly new to all of this, so if this is a foolish question, I apologize in advance and thank you all for working with me. 对于这一切,我还是很陌生,因此,如果这是一个愚蠢的问题,我事先表示歉意,并感谢大家与我一起工作。
If you are set on using the same image then I would suggest you set the ENTRYPOINT to python and then use docker run command to start the containers by providing the scrips as the CMD like so: 如果您设置使用相同的图像,那么我建议您将ENTRYPOINT设置为python,然后使用docker run命令通过提供脚本作为CMD来启动容器,如下所示:
Dockerfile: Dockerfile:
FROM python
...
ENTRYPOINT ["python"]
And the use the docker run command like so 然后像这样使用docker run命令
docker run -d my_image script.py && docker run -d my_image script2.py
Which would start two containers each running separate scripts 这将启动两个容器,每个容器运行单独的脚本
BUT - I like to keep the images clean in terms of not having any additional scripts or packages that are not necessary for my service to work, so in this case I would simply create two separate images each having one of the scripts and then run them similarly. 但是-我希望保持图像干净,因为没有任何其他脚本或程序包对于我的服务正常工作是不必要的,因此在这种情况下,我将简单地创建两个单独的图像,每个图像或脚本都包含一个脚本,然后运行它们同样。
Example: 例:
FROM python
COPY script.py script.py
ENTRYPOINT ["python"]
CMD ["script.py"]
And the second image: 第二张图片:
FROM python
COPY script2.py script2.py
ENTRYPOINT ["python"]
CMD ["script2.py"]
And then just build them as separate images and run the the same way as before 然后只需将它们构建为单独的图像,并以与以前相同的方式运行
Try these steps, it should work. 尝试这些步骤,它应该可以工作。
Dockerfile
with contents: Dockerfile
: FROM python:3.7-alpine
COPY script1.py /script1.py
COPY script2.py /script2.py
CMD ["/bin/sh"]
script1.py
script1.py
print("Hello")
script2.py
script2.py
print("World")
docker build -t myimage:v1 .
docker build -t myimage:v1 .
image docker build -t myimage:v1 .
$ docker run -it --rm --entrypoint python myimage:v1 /script1.py
Hello
$
$ docker run -it --rm --entrypoint python myimage:v1 /script2.py
World
$
NOTE: Here we're using same docker image myimage:v1
and just changing the entrypoint in every docker run command. 注意:这里我们使用相同的
myimage:v1
映像myimage:v1
,只是在每个docker run命令中更改了入口点。
Hope this helps. 希望这可以帮助。
You can do this easily using Docker Compose . 您可以使用Docker Compose轻松做到这一点。 Here is a simple
docker-compose.yml
file just to show the idea: 这是一个简单
docker-compose.yml
文件,仅用于说明这一点:
version: '3'
services:
app1:
image: alpine
command: >
/bin/sh -c 'while true; do echo "pre-processing"; sleep 1; done'
app2:
image: alpine
command: >
/bin/sh -c 'while true; do echo "post-processing"; sleep 1; done'
As you see both services use the same image, alpine
in this example, but differ in commands they run. 如您所见,两个服务使用相同的映像(在此示例中为
alpine
,但是它们运行的命令不同。 Execute docker-compose up
and see the output: 执行
docker-compose up
并查看输出:
app1_1 | pre-processing
app2_1 | post-processing
app2_1 | post-processing
app2_1 | post-processing
app1_1 | pre-processing
app2_1 | post-processing
app1_1 | pre-processing
app2_1 | post-processing
...
In your case, you just change the image to your image, say myimage:v1
, and change the service commands so that the first service definition will have command: python script1.py
line and the second one command: python script2.py
. 在您的情况下,只需将映像更改为映像,例如
myimage:v1
,然后更改服务命令,以便第一个服务定义将具有command: python script1.py
行,第二个服务定义将具有command: python script2.py
。
Any command you put at the end of the docker run
command (or the Docker Compose command:
field) replaces the CMD in the Dockerfile. 您在
docker run
命令(或Docker Compose command:
字段)末尾添加的任何命令都将替换Dockerfile中的CMD。 I would suggest still putting in some useful default CMD, but you can always just 我建议仍然放入一些有用的默认CMD,但是您始终可以
docker run --name hello myimage python script.py
docker run --name world myimage python script2.py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.