compose icon indicating copy to clipboard operation
compose copied to clipboard

[BUG] Docker compose not showing python logs until container finishes.

Open sorenwacker opened this issue 1 year ago • 2 comments

Description

Docker compose is holding back python log messages until the container finishes.

Steps To Reproduce

docker-compose.yml

services:
  python-app:
    image: python:3.9-slim
    container_name: python-container
    command: python -c "print('# This is a Python comment'); import time; time.sleep(20)"
$ sudo docker compose up
WARN[0000] /mnt/home/ubuntu/workspace/docker-compose-logging-issue/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion 
[+] Running 1/0
 ✔ Container python-container  Created                                                                                  0.0s 
Attaching to python-container
python-container  | # This is a Python comment  # This will only show when the program ends and the container finishes. 
python-container exited with code 0

Compose Version

Docker Compose version v2.29.1

docker-compose version 1.25.0, build unknown
docker-py version: 4.1.0
CPython version: 3.8.10
OpenSSL version: OpenSSL 1.1.1f  31 Mar 2020

Docker Environment

Client: Docker Engine - Community
 Version:    27.1.1
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.16.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.29.1
    Path:     /usr/libexec/docker/cli-plugins/docker-compose
  scan: Docker Scan (Docker Inc.)
    Version:  v0.23.0
    Path:     /usr/libexec/docker/cli-plugins/docker-scan

Server:
ERROR: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.46/info": dial unix /var/run/docker.sock: connect: permission denied
errors pretty printing info

Anything else?

No response

sorenwacker avatar Aug 06 '24 14:08 sorenwacker

bug

the attribute version is obsolete, it will be ignored, please remove it to avoid potential confusion

icetech233 avatar Aug 19 '24 06:08 icetech233

Hi @sorenwacker,

I'm pretty sure that it is not a bug. The print function is buffered by default so you need to use the -u argument or the PYTHONUNBUFFERED=1 env variable to make it print something inside a Docker container. It is a standard practice when you use the Python image. An example of a containerized Python application is available in the documentation: https://docs.docker.com/language/python/containerize/

Have a nice day !

BergLucas avatar Aug 20 '24 16:08 BergLucas