0

Dockerfile:

FROM python:3.10-slim
WORKDIR /watch
ADD requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir -r /app/requirements.txt
ADD webwatcher /app/webwatcher

VOLUME /watch
ENV IS_DOCKER=true
ENV PYTHONPATH "/app"
ENTRYPOINT python3 -m webwatcher

My __main__.py in the module

import signal
from time import sleep
from .watchdog import register_observer

observer = register_observer()

def finish(signum, frame):
    print('\nExiting application...')
    observer.stop()
    observer.join()
    exit(0)

print('Registering signals')
signal.signal(signal.SIGTERM, finish)
signal.signal(signal.SIGINT, finish)

print('Running module')
observer.start()
while True:
    sleep(1)

I then build the container

docker build -t webwatcher:latest .

And run

docker run --rm --name web -v path:/watch/path webwatcher:latest

But when I run, it shows no output at all. It also does not let me quit with Ctrl+C from the run command, nor does docker stop work (it times out then kills it).

Why isn't the module being run properly from the container?

0

1 Answer 1

1

I think there is two separate issues here. By default, docker runs containers without TTY allocation, and some programs run slightly differently in those cases. Python will buffer stdout stream in this case. To prevent this, you can run docker run -it instead.

Another problem is that the Dockerfile has "shell-form" ENTRYPOINT python3 -m webwatcher instead of JSON notation ENTRYPOINT ["python3", "-m", "webwatcher"].

From hadolint docs:

The shell form prevents any CMD or run command line arguments from being used, but has the disadvantage that your ENTRYPOINT will be started as a subcommand of /bin/sh -c, which does not pass signals. This means that the executable will not be the container’s PID 1 - and will not receive Unix signals - so your executable will not receive a SIGTERM from docker stop.

If you enjoy using linters you might find hadolint useful to catch errors like this.

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

1 Comment

When I tried using that form, it tells me that python isn't found. Searching online seemed to point towards using exec if that is an issue.

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.