Welcome to new things

[Technical] [Electronic work] [Gadget] [Game] memo writing

How to split logs into tabs for each container in Docker Compose

When I want to link multiple containers together, I use Docker Compose.

When you run Docker Compose, you will see the logs for each container, but you will see the logs for all containers on one screen.

How to split logs into tabs for each container in Docker Compose

I want to follow the log for each container, but it is hard to see the log of other containers in the middle of the log.

So, I tried to split the Docker Compose logs into tabs for each container.

Square Needle

We used an application called "Byobu", which is installed by default in Ubuntu and allows tab switching in the shell.

The usage of "Byobu" is summarized in a separate article.

www.ekwbtblog.com

Byobu Setting

To start Byobu with a set of tabs, first define a set of tabs and save them in the following file.

  • ~/.byoku/windows.tmux.<tab set name>.

Then, set the environment variable "BYOBU_WINDOWS" to the "tab set name" and start Byobu with the set tab.

$ BYOBU_WINDOWS=<タブのセット名> byobu

The file is written as follows

First create a session, then add windows.

new-session -n <タブ名> <コマンド>
new-window -n <タブ名> <コマンド>
new-window -n <タブ名> <コマンド>
new-window -n <タブ名> <コマンド>
…

Dynamic generation of settings

Since the Byotu tab configuration file is a static file, a script is used to rewrite its contents each time the Docker Compose log is displayed.

view-compose-logs.sh

#!/bin/bash

svcs=($(docker-compose config --services))
config=""
first_svc=1

for svc in "${svcs[@]}"
do
    if [ "$first_svc" = "1" ]; then
        config="new-session -n $svc docker-compose logs -f $svc;"
        first_svc=0
    else
        config="${config}\nnew-window -n $svc docker-compose logs -f $svc;"
    fi
done

if [ "$config" != "" ]; then
    echo -e "$config" > ~/.byobu/windows.tmux.docker_compose_log
    BYOBU_WINDOWS=docker_compose_log  byobu
fi

execution

After starting Docker Compose with docker-compose up -d to separate it from the log, execute ./view-compose-logs.sh.

docker-compose up -d
./view-compose-logs.sh

For example, if you launch "Jenkins", "phpMyAdmin" and "MySQL" in Docker Compose

Jenkins

How to split logs into tabs for each container in Docker Compose

phpMyAdmin

How to split logs into tabs for each container in Docker Compose

MySQL

How to split logs into tabs for each container in Docker Compose

and the tabs will appear at the bottom and the logs will be divided into tabs.

Use "Alt+Left/Right keys" to switch tabs and "Ctrl+c" to close tabs.

Impressions, etc.

Ubuntu uses MATE for its desktop environment.

Until now, I have been using the following script to display Docker Compose logs in separate tabs in the MATE terminal.

#!/bin/bash

svcs=($(docker-compose config --services))

for svc in "${svcs[@]}"
do
    mate-terminal --profile=hold -e "docker-compose logs -f $svc" -t $svc --tab
done

How to split logs into tabs for each container in Docker Compose

However, since it only works in an environment with a MATE terminal, we have changed it to a Byobu-based form so that it can be used in a CUI environment, such as SSH connection or Windows WSL.

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com