简体   繁体   English

Docker 容器中的 Python 脚本在不通过 Docker CLI 手动调用时运行良好

[英]Python script in Docker container runs fine when called manually not via Docker CLI

Sorry if the title is unclear.如果标题不清楚,请见谅。 I have a custom image that runs a Python script on a loop.我有一个在循环中运行 Python 脚本的自定义图像。 The script continues to run as expected when I call it directly with python3 <script> locally or even in the Docker container that spins up from the image I've built.当我直接在本地使用python3 <script>调用python3 <script> ,甚至在从我构建的映像启动的 Docker 容器中调用它时,脚本会继续按预期运行。

However, when I run the containers via docker-compose, the script does nothing...and when I run it as a docker service or standalone with docker run , the script runs correctly but only for a short time and then stops.但是,当我通过 docker-compose 运行容器时,脚本什么也不做……当我将它作为docker service docker run或使用docker run独立docker run ,脚本可以正确docker run ,但只运行很短的时间,然后停止。

Dockerfile: Dockerfile:

# syntax=docker/dockerfile:1
FROM python:3.8-slim-buster
WORKDIR /mqtt_client
COPY . .
RUN pip3 install -r requirements.txt
CMD ["python3", "/mqtt_client/mqtt_client.py"]

Any ideas?有任何想法吗?

docker-compose snippet: docker-compose 片段:

py_publisher:
    image: python-mqtt-client
    deploy:
        mode: replicated
        replicas: 3
    depends_on: 
        - broker
    entrypoint: "python3 /path/to/mqtt_client.py"

image directory structure:图片目录结构:

/ <-- root
.
├── Dockerfile
├── mqtt_client.py
└── requirements.txt

remove the entrypoint from your docker-compose file, it will start by the CMD of dockerfile.从 docker-compose 文件中删除入口点,它将由 dockerfile 的 CMD 启动。 You can also set the restart option to always.您还可以将重新启动选项设置为始终。

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

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