简体   繁体   中英

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. 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.

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.

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:

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. You can also set the restart option to always.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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