docker-compose: can’t see container logs produced by Python logger.info and print() messages, for an application run inside a container

I prepare this very simple docker-compose file, so I can test out if my hello_world.py’s logs are being sent to the container logs bucket. I run hellow_world.py inside the python 3.8.1 Frustratingly, whatever I try, basing on suggestions I found from googling, it just simply refuses to work! So frustrating, working few days already on this. I really appreciate if you can help me out!

Below are the files, very simple by intention, in the same folder. After container is launched, I went inside the container, did ‘python -u hello_world.py’, saw the log/print messages on the screen/stdout, but I can’t see them via running (from the container host’s terminal) ‘docker logs logs_web_1’.

```  
docker-compose.yml

version: '3.7'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - PYTHONUNBUFFERED=1
      - LOGLEVEL=DEBUG
    tty: true

Dockerfile

FROM python:3.8.1

WORKDIR /opt/app

RUN apt-get update && apt-get upgrade -y

ADD . /opt/app

RUN pip3 install --upgrade pip
```

```python
hello_world.py

import logging

logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
logging.getLogger(__name__).setLevel(logging.DEBUG)
logger = logging.getLogger()

logger.info('logger - hello world')
print('print - hello world', flush=True)
```

———- logs —

~/DevOps/project-tests/logs > docker ps

CONTAINER ID   IMAGE      COMMAND     CREATED          STATUS          PORTS     NAMES
b95cc6d70beb   logs_web   "python3"   34 minutes ago   Up 34 minutes             logs_web_1

~/DevOps/project-tests/logs > docker-compose ps

   Name      Command   State   Ports
------------------------------------
logs_web_1   python3   Up       

~/DevOps/project-tests/logs > docker exec -it logs_web_1 bash

[email protected]:/opt/app# python -u hello_world.py

2020-11-14 03:26:11,714 logger - hello world
print - hello world
[email protected]:/opt/app# 
[email protected]:/opt/app# exit
exit

~/DevOps/project-tests/logs > docker logs logs_web_1

Python 3.8.1 (default, Feb  2 2020, 08:37:37) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

Source: Dockerfile Questions