简体   繁体   English

如何在同一图像的并行容器中运行不同的python脚本

[英]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. 尝试这些步骤,它应该可以工作。

  • Create a Dockerfile with contents: 创建一个包含以下内容的Dockerfile
FROM python:3.7-alpine
COPY script1.py /script1.py
COPY script2.py /script2.py
CMD ["/bin/sh"]
  • In script1.py script1.py
print("Hello")
  • In script2.py script2.py
print("World")
  • Build docker image docker build -t myimage:v1 . 构建docker build -t myimage:v1 . image docker build -t myimage:v1 .
  • Run the containers 运行容器
$ 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命令中更改了入口点。

More info here . 更多信息在这里

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.

相关问题 Docker:从同一映像运行多个脚本(容器) - Docker: run multiple scripts (containers) from the same image 如何使用ProcessBuilder从Java运行并行python脚本 - How to run parallel python scripts from java using the ProcessBuilder 从 python 脚本并行运行 bash 脚本 - Run bash scripts in parallel from python script 如何在 TestStand 中并行执行相同的 Python 脚本? - How to execute the same Python scripts in parallel in TestStand? 如何让两个不同的python脚本在同一个端口上运行? - How to make two different python scripts to run on the same port? 如何并行运行具有不同参数的相同函数的N个线程? - 蟒蛇 - How to run N threads of the same functions with different parameters in parallel? - python SLURM:如何为目录中的不同 $arg 并行运行相同的 python 脚本 - SLURM: how to run the same python script for different $arg from a catalogue in parallel 如何从 tkinter 运行两个并行脚本? - How to run two parallel scripts from tkinter? 如何在Linux中提交2个作业(以并行方式运行位于2个不同目录中的2个python脚本)? - How to submit 2 jobs (to run 2 python scripts in parallel located in 2 different directories) in linux? 如何使用不同的参数并行运行相同的函数? - How to run the same function in parallel with different arguments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM