Raphael
Raphael

Reputation: 10579

Capturing output of docker-compose in variable

When I run a docker-compose command on the CLI, it looks like this:

docker-compose up -d my-service
# Recreating my-cluster_my-service_1 ... done

However, capturing the output seems to break it:

foo=$(docker-compose up -d my-service 2>&1)
echo $foo # <-- this line actually vanishes!
# Recreating my-cluster_my-service_1 ... done
#  ecreating my-cluster_my-service_1 ... 

Note how we have to re-direct stderr because, for some reason, docker-compose outputs there instead of stdout.

This makes it hard to handle output of docker-compose programmatically. How can I get the correct output of docker-compose?

Upvotes: 2

Views: 931

Answers (1)

Raphael
Raphael

Reputation: 10579

The issue is apparently that docker-compose uses control characters to re-write the printed line. That's fine in an interactive terminal (even though the effect is nil in this case) but capturing (and echoing) a stream containing such characters has unexpected effects like what we see above.

Add option --no-ansi to suppress those control characters:

$ alf=$(docker-compose --no-ansi up -d my-service 2>&1)
$ echo $alf
# Recreating my-cluster_my-service_1 ... done

Upvotes: 1

Related Questions