1

I have a docker-compose file that includes a service configured like:

py_publisher:
    build:
        context: ./client
        dockerfile: Dockerfile
    deploy:
        mode: replicated
        replicas: 3
#    entrypoint: "python3 mqtt_client.py" 

The image is built from this 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.py"]

The home dir (/mqtt_client) of the container contains:

Dockerfile  mqtt_client.py  requirements.txt

The relevant docker-compose results:

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                        PORTS                                                                                            NAMES
61b8f5e1e0bb   e1d0fc27a640   "python3 mqtt_client…"   7 seconds ago   Up 2 seconds                                                                                                                   mqtt-demo_py_publisher_1
9992b9c9031d   e1d0fc27a640   "python3 mqtt_client…"   7 seconds ago   Up 3 seconds                                                                                                                   mqtt-demo_py_publisher_2
a197f0f3126b   e1d0fc27a640   "python3 mqtt_client…"   7 seconds ago   Up 2 seconds                                                                                                                   mqtt-demo_py_publisher_3

The issue is that the Python command is not actually "working". Simply nothing happens. However, when I exec into the containers and run that exact command, it works fine.

What's the difference in the way I'm running via docker-compose and running it manually exec'ed into the container?

Sam Dillard
  • 670
  • 1
  • 5
  • 18
  • The containers are "up" and the command shows as expected, so does your app not log anything when it starts? – OneCricketeer Aug 03 '21 at 19:01
  • @OneCricketeer no the app doesn't log anything but I know it's not working because I can check the results of what the Python script does in another app. When I run it manually when `exec`'ed, it works fine. – Sam Dillard Aug 03 '21 at 19:04
  • Does the `python -u` option, or the `PYTHONUNBUFFERED` environment variable, make a difference; is the process actually running, but buffering its output in-process? (See for example [Python app does not print anything when running detached in docker](https://stackoverflow.com/questions/29663459/python-app-does-not-print-anything-when-running-detached-in-docker).) – David Maze Aug 03 '21 at 19:15
  • I'm saying can you log/print anything in the client script's main method? Do the cotnainers actually remain up and not restart themselves? If `docker-compose logs` are empty, then it _seems_ like the command is exiting with an error – OneCricketeer Aug 03 '21 at 19:31
  • @DavidMaze no I've confirmed that the lack of output is because the script isn't being run at all. – Sam Dillard Aug 03 '21 at 19:57

1 Answers1

0

the proper way to do it, is by using entrypoint and the command. e.g.

ENTRYPOINT ["python3"]
CMD ["mqtt_client.py"]

since you are using python image already, you can drop the ENTRYPOINT instruction and only keep CMD.

Mr.
  • 9,429
  • 13
  • 58
  • 82