2

I am new to docker, so bear with me on this.

I have a app.py file, which simply uses apscheduler to print a sentence on the console. I have followed the structure from the official guide for the python file. When I run the file on my console, it runs as expected. (prints the Tick statement every 10 seconds.)

Now, I want to dockerize it and upload the image to dockerhub. I followed the docker documentations and this is how my DockerFile looks like:

FROM python:3
COPY requirements.txt .
COPY app.py .
RUN pip install --trusted-host pypi.python.org -r requirements.txt
CMD [ "python", "app.py" ]

I have listed the module names in requirements.txt as below:

datetime
apscheduler

The folder is flat. app.py and requirements.txt are in the same level in the directory.

|
|- app.py
|- requirements.txt

I use below commands to build the docker image:

docker build . -t app1:ver3

The docker image builds successfully and shows up when I do

docker images

Problem is, when I run the docker image with

docker run app1:ver3

the image does not show any output.

In fact the image shows as listed when I do docker ps - which is expected but the run command should show me print statements on the console every 10 seconds.

harvpan
  • 8,571
  • 2
  • 18
  • 36
  • 2
    Did you try `docker run -it app1:ver3` – Tarun Lalwani Jun 20 '19 at 18:59
  • See this also https://tarunlalwani.com/post/why-delayed-output-python-docker/ – Tarun Lalwani Jun 20 '19 at 19:00
  • Okay, that worked like a charm. Can you point me to the docs for `-it` ? – harvpan Jun 20 '19 at 19:01
  • 1
    https://docs.docker.com/engine/reference/run/ -> under foreground. -i keeps it it stdin active and -t provisions a tty for it – Dockstar Jun 20 '19 at 20:09
  • i have the same problem then i use APScheduler lib in docker container they don't show output data but if i launch script file inside working container (docker exec -it bac69ce428cc bash; python app.py) all is good, did you find answer ? – Vadim Jul 02 '20 at 08:40

1 Answers1

2

There are two things here

You need to use docker run -it app1:ver3

-i: Interactive mode
-t: Enable TTY

I believe just -t also may do the job. See the link below for details

https://docs.docker.com/engine/reference/run/

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265