如何使用 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-compose up --build
运行这个项目时,只有最后一个 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.
推荐答案
如documentation,docker 文件中只能有一个 CMD,如果有更多,最后一个覆盖其他并生效.使用 docker 的一个关键点可能是隔离您的程序,因此乍一看,您可能希望将它们移动到单独的容器中并使用共享卷或 docker 网络相互通信,但如果您真的需要它们运行同一个容器,将它们包含在 bash 脚本中,并用 CMD 替换最后一个 CMD run.sh
将它们一起运行:
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
将 COPY 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屋!