1

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
2
  • When you say it runs for a short time, do you mean it starts for a second and then crashes? Or is it running for a few minutes before stopping? Commented Jul 29, 2021 at 21:06
  • @JeffGruenbaum no it actually does what's it's supposed to do...but only temporarily. It's supposed to run forever, which it does when I run it manually. Commented Jul 29, 2021 at 21:09

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I'll try removing the entrypoint, although I did start with just the CMD portion of the Dockerfile originally. As for restarting "always" -- what does that imply? That the container will shut down? The container actually stays up, just the Python stops doing anything.
Ok so now the Python starts properly from docker-compose but it still stops working after a few loops. I have no idea why it stops looping.
If the script starts, the problem is in your python script. You're keeping some kind of log to know where the program exits? The restart parameter is to restart the program if something goes wrong, like you program crash for some reason.
the script runs completely fine when I run it manually though. That indicates to me it's a problem with the way it's run from the Docker tooling, no?
Try running the python unbuffered. Substitute the line CMD ["python3", "/mqtt_client/mqtt_client.py"] by CMD ["python3","-u", "/mqtt_client/mqtt_client.py"] in your Dockerfile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.