简体   繁体   English

Docker:从同一映像运行多个脚本(容器)

[英]Docker: run multiple scripts (containers) from the same image

I made a Python project with several packages and several scripts that depend upon the packages. 我制作了一个Python项目,其中包含几个软件包和依赖于这些软件包的几个脚本。 The structure looks something like this: 结构看起来像这样:

app/
  package1/
    __init__.py
    ...
  package2/
    __init__.py
    ...
  script1.py
  script2.py

So I dockerized the app, that is, I made an image with some Dockerfile, wherein the default script is, say, script1.py: 因此,我对应用程序进行了docker化,也就是说,我使用一些Dockerfile制作了映像,其中默认脚本为script1.py:

FROM ...
ADD ...
...
CMD python script1.py

My question: is it a good practice to start two different containers from this single image, one that runs scrip1, the other for script2? 我的问题:从这个单一映像启动两个不同的容器(一个运行scrip1,另一个运行script2)是一种好习惯吗?

docker build -t my_image .
docker run -d -it my_image
docker run -d -it my_image python script2.py

Note that, in my case, script1 and script2 are independent of one another (they do not communicate and can be started independently). 请注意,在我的情况下,script1和script2彼此独立(它们不通信,可以独立启动)。

Thanks for your help 谢谢你的帮助

The Docker documentation lists as a best practice that "Each container should have only one concern". Docker 文档列出了最佳实践,即“每个容器应该只关注一个问题”。 In your example it sounds as though you effectively have two tasks being ran by the two separate Python scripts, but packaged within the same image. 在您的示例中,听起来好像您实际上有两个任务由两个单独的Python脚本运行,但打包在同一张图像中。

Ideally you would avoid this and have two separate images for your two tasks. 理想情况下,您应该避免这种情况,并为两个任务使用两个单独的图像。 If the two scripts share a large number of common dependencies you could create your own parent image to base the two script images on. 如果两个脚本共享大量公共依赖项,则可以创建自己的父映像,以将两个脚本映像作为基础。

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

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