如何使用Docker运行多个Python脚本和一个可执行文件? [英] How to run multiple Python scripts and an executable files using Docker?
问题描述
我想创建一个包含两个Python软件包以及一个包含可执行文件的软件包的容器。
I want to create a container that is contained with two Python packages as well as a package consist of an executable file.
dockerized_project
├── docker-compose.yml
├── Dockerfile
├── exec_project
│ ├── config
│ │ └── config.json
│ ├── config.json
│ ├── gowebapp
├── pythonic_project1
│ ├── __main__.py
│ ├── requirements.txt
│ ├── start.sh
│ └── utility
│ └── utility.py
└── pythonic_project2
├── collect
│ ├── collector.py
├── __main__.py
├── requirements.txt
└── start.sh
Dockerfile内容:
FROM ubuntu:18.04
RUN apt update
RUN apt-get install -y python3.6 python3-pip python3-dev build-essential gcc \
libsnmp-dev snmp-mibs-downloader
RUN pip3 install --upgrade pip
RUN mkdir /app
WORKDIR /app
COPY . /app
WORKDIR /app/snmp_collector
RUN pip3 install -r requirements.txt
WORKDIR /app/proto_conversion
RUN pip3 install -r requirements.txt
WORKDIR /app/pythonic_project1
CMD python3 __main__.py
WORKDIR /app/pythonic_project2
CMD python3 __main__.py
WORKDIR /app/exec_project
CMD ["./gowebapp"]
docker-compose内容:
version: '3'
services:
proto_conversion:
build: .
image: pc:2.0.0
container_name: proto_conversion
# command:
# - "bash snmp_collector/start.sh"
# - "bash proto_conversion/start.sh"
restart: unless-stopped
ports:
- 8008:8008
tty: true
问题:
当我使用 docker-conpose up --build $ c $运行此项目时c>,仅运行最后一个
CMD
命令。因此,我认为以前的 CMD
命令在 Dockerfile
中被杀死,因为当我删除最后两个 CMD
,第一个 CMD
效果很好。
Problem:
When I run this project with docker-compose up --build
, only the last CMD
command runs. Hence, I think the previous CMD
commands are killed in Dockerfile
because when I remove the last two CMD
, the first CMD
works well.
有没有办法运行多个Python脚本,并且
Is there any approach to run multiple Python scripts and an executable file in the background?
我也尝试过bash文件也没有成功。
I've also tried with the bash files without any success either.
推荐答案
如文档中所述,可以只能是docker文件中的一个CMD,如果更多,则最后一个将覆盖其他CMD并生效。
使用docker的一个关键点可能是隔离程序,因此乍一看,您可能希望将它们移动到单独的容器中,并使用共享卷或docker网络相互通信,但是如果您确实需要它们可以在同一个容器中运行,包括在bash脚本中,并用CMD run.sh
替换最后一个CMD将使它们同时运行:
As mentioned in the documentation, there can be only one CMD in the docker file and if there is more, the last one overrides the others and takes effect.
A key point of using docker might be to isolate your programs, so at first glance, you might want to move them to separate containers and talk to each other using a shared volume or a docker network, but if you really need them to run in the same container, including them in a bash script and replacing the last CMD with CMD run.sh
will run them alongside each other:
#!/bin/bash
exec python3 /path/to/script1.py &
exec python3 /path/to/script2.py
添加将run.sh
复制到Dockerfile并使用 RUN chmod a + x run.sh
使其可执行。 CMD应该为 CMD [ ./run.sh]
Add COPY run.sh
to the Dockerfile and use RUN chmod a+x run.sh
to make it executable. CMD should be CMD ["./run.sh"]
这篇关于如何使用Docker运行多个Python脚本和一个可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!