Docker compose run does not preserve colored output when piped to tee (is this a BUG?)

Hi all! :wave:
I want to run a script inside a container that produces a colored output. But it is displayed without colors. Am I missing something? Some parameter might do the trick?
It works fine without the pipe to tee.

Thanks in advance!!

Here is an example:

Config

docker-compose.yml

version: '3.8'

 services:
   nginx-demo:
     image: nginx
     volumes:
       - ./run.sh:/run.sh

My script that produces colored output: run.sh

 #!/bin/bash

 ls --color=always

Output

image

Docker info

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc., v0.9.1)
  compose: Docker Compose (Docker Inc., v2.13.0)
  dev: Docker Dev Environments (Docker Inc., v0.0.5)
  extension: Manages Docker extensions (Docker Inc., v0.2.16)
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
  scan: Docker Scan (Docker Inc., v0.22.0)

Server:
 Containers: 26
  Running: 0
  Paused: 0
  Stopped: 26
 Images: 72
 Server Version: 20.10.21
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 770bd0108c32f3fb5c73ae1264f7e503fe7b2661
 runc version: v1.1.4-0-g5fd4c4d
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
  cgroupns
 Kernel Version: 5.15.49-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: aarch64
 CPUs: 6
 Total Memory: 8.736GiB
 Name: docker-desktop
 ID: WZUS:J2ZC:IJDT:V7LA:XC6C:3ZB5:F2WW:7VIV:QVRM:NZTB:L6VI:7HPU
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5000
  127.0.0.0/8
 Live Restore Enabled: false

Updated answer:

It looks like i was wrong and it is not a bug. --ansi is not for controlling colored outputs of containers but only for outputs generated by compose itself. For example the status lines of docker compose up -d

Detailed answer on GitHub: https://github.com/docker/compose/issues/10161#issuecomment-1377981816

Original answer:

I think you have indeed found a bug. I don’t exactly know how it should work since I have never needed it. I have never tried to use docker compose run when I needed colored output, but colors and pipe worked with docker-compose-v1 which is Docker Compose v1 on macOS. v1 has different parameters and different default behavior so I could not disable showing colors. At least not by using --ansi

I could use Docker Compose v2 with colors and pipe when I added -e TERM=xterm to the run command, but this seems odd since I don’t know what --ansi is for then.

I could find bug reports related to Docker Desktop for Windows, but I also tried in a virtual machine using only Docker CE and Docker Compose v2.14 and had the same result.

1 Like