rocks6
rocks6

Reputation: 172

Docker-compose flask app not printing output from 'print'

I have a flask app that has one route and nothing complex going on, running in a docker container. I cannot for the life of me get print statements to show up in the logs (docker-compose logs -f <containername>). So far, I have tried various answers that supposedly have fixed this problem for others including:

  1. Calling print("test", flush=True)
  2. Setting PYTHONUNBUFFERED=1 and verifying it is set in the actual container with echo
  3. Setting PYTHONUNBUFFERED=0
  4. Running python with the -u flag
  5. Using the logging module (logger.warning, logger.info, etc)

So far nothing has worked. The flask app is starting perfectly fine, but no output from my print statements is shown. I have sanity checked that i'm editing the correct file by adding random syntax errors and watching the app brick itself. I'm using python 3.8 and docker-compose 2

Upvotes: 7

Views: 5919

Answers (3)

ra37
ra37

Reputation: 73

I found this question while looking for answers to a similar problem. I was running a flask app in a conda environment in a container and wasn't getting any log output even though the flask app itself was working fine. I added the following lines to my Dockerfile and it starting logging as expected -

ENV PYTHONUNBUFFERED=1

RUN echo "source activate my_env" > ~/.bashrc

ENV PATH /opt/conda/envs/my_env/bin:$PATH

CMD ["python", "api.py"]

Upvotes: 4

Sai Kumar
Sai Kumar

Reputation: 285

You can see logs with docker-compose or docker

With docker-compose you have to see SERVICE

Note: you add containername but you have to add service name
NOT $ docker-compose logs -f <containername> 

USE $ docker-compose logs -f <SERVICE_NAME>)

With docker you have to add container name or container id

docker logs -f CONTAINER_ID | CONTAINER_NAME

Upvotes: 1

mee
mee

Reputation: 718

Try this:

import sys
print('It is working',file=sys.stderr)

Upvotes: 2

Related Questions