last day (14 days later) » 

2:46 PM
0
Q: How to enable colors in a Docker container?

darckcrystaleContext I have a Docker Compose with which I run several containers. My docker-compose.yml: version: '2' services: web: build: ../application-web/ ports: - "80:80" tty: true # Add a volume to link php code on the host and inside the container volumes: - ${applic...

 
That depends on the details of the software running your unit tests. Maybe it's checking isatty(0) (or isatty(1)). Maybe it needs different terminfo content. Makes more sense to investigate that specific piece of software where the decision about whether to colorize output is being made than to hunt around at the higher layers.
...to be clear: bash itself has no control over the content of output written by programs it starts, including color codes contained in that output. Those programs write their output directly to the same file descriptor bash itself uses (except when redirections &c. explicitly modify this); bash has no say in what that content is, and thus in whether it contains color codes.
 
@CharlesDuffy I also tried to just connect myself to a container and run some commands in it like ls -la which on my computer returns colorized output. In the container, it returns non-colorized output.
 
...which doesn't have anything to do with Docker itself, and everything to do with your copy of ls and how it's configured (ie. the value of the LS_COLORS environment variable). If your container runs a minimal distribution with Busybox, the copy of ls it includes may not have color support at all.
 
About isatty, you will notice that I activate tty mode in the docker-compose.yml for all my containers.
 
(and ls is not part of bash -- see again, "not bash's decision whether tools it runs colorize their output").
btw, is xterm really the same TERM you're running with on the outside, as opposed to something more modern like screen-256color?
...and just for the heck of it, let's try something: blue=$(tput setaf 4); reset=$(tput setaf 0); printf 'blue=%q\n' "$blue"; printf '%s\n' "${blue}this should be blue${reset}". With that done, two questions: (1) What's on the line after blue=? (2) Is the this should be blue text rendered in blue? (3) Do the results differ from what you get in your local terminal? (4) Do they still differ even after you set TERM to an exact match?
 
2:46 PM
Ok, for the host I have TERM=xterm-256color.
 
...oh, and does [ -t 1 ]; echo "$?" return 0 or 1? (But answer this after the other four questions above).
 
On my host, your command displays
`blue=$'\E[34m'
this should be blue`
and `this should be blue` is written in blue.
 
...and in the container?
 
(it's restarting)
On my host, LS_COLORS is set, but not on my container.
 
...as some background -- $'\E[34m' means that what needs to be written to the TTY with your terminal type to cause the following text to be in blue is an escape sequence (aka \x1b), followed by the string [34m. That'll work no matter which program writes it -- ie. whether that program is containerized or not.
those escape sequences are in the terminfo database (the successor to the older UNIX termcap database).
 
2:54 PM
After running your command on my container:
- it displayed
`blue=$'\E[34m'
this should be blue`
- `this should be blue` is written in blue
- the `username@computer:current_directory` switched to black.
 
okay, then -- that demonstrates that the container does have a terminfo database that properly tells it how to generate color codes for your TERM type.
so, container's fine, shell's fine, and we know the problem with ls (the lack of LS_COLORS).
 
Ok, font color for all turned into black (I was using a light grey as font color)
(in my container)
 
right, that was the reset (tput setaf 0).
...so, what's still outstanding is figuring out why your unit-testing software isn't generating colorized output, but without knowing that software and its source, that's not something I'm well-positioned to help with.
btw, see upload.wikimedia.org/wikipedia/commons/1/15/… if you want to try to find the setaf code for that grey.
 
I just exited the container then reconnected, it turned back to default :)
Some other .sh file of mine are using colors with codes:
WHITE="\\033[1;97m"
GREEN="\\033[1;32m"
NORMAL="\\033[0;39m"
YELLOW="\\033[1;33m"
RED="\\033[1;31m"

with them, indeed, it works :/
 
...oh, one last thing -- for fd in 0 1 2; do [ -t "$fd" ]; printf '%s %s\n' "$fd" "$?"; done
 
3:01 PM
0 0
1 0
2 0
 
okay, so all three FDs see themselves as connected to a TTY
(btw, as an aside, all-caps variable names are used for variables with meaning to the shell or operating system; consider using lowercase names in your scripts, which are guaranteed not to interfere with the operation of the shell or of POSIX-specified OS utilities -- see pubs.opengroup.org/onlinepubs/9699919799/basedefs/…, fourth paragraph).
...anyhow, I think we've pretty well isolated the issue to the specific unit-testing software that's supposed to be colorizing output and not
 
Oh, I didn't know, we use it to define constants in our scripts, thank you!
 
hopefully that's helpful -- I've probably gone as far as I can here. Take care!
 
Thank you for your help, I'll keep you update if I find something!
 

last day (14 days later) »