※当サイトの記事には、広告・プロモーションが含まれます。

docker compose logs -fの内容をシェルスクリプトの処理で判定に使いたかったのだが...

www.itmedia.co.jp

 帝国データバンクによると、「はじめての」シリーズなどのIT・ビジネス関連書籍を手がける出版社、秀和システム(東京都江東区)が7月1日付で法的整理および出版事業を他社に譲渡する手続きを選択すると通知。上田智一代表は「現時点では何も回答できない」と話したという。

IT関連書籍の秀和システムが法的整理へ 一時は船井電機の実質親会社に──帝国データバンク - ITmedia NEWS

 2021年に子会社を通じて船井電機を買収したが、その後船井電機を通じて買収した脱毛サロンを手がけるグループ会社で取引先との未払いトラブルが発生。連帯保証を行っていたことから船井電機の株式が仮差押えを受けるなどの事態に発展していた。

IT関連書籍の秀和システムが法的整理へ 一時は船井電機の実質親会社に──帝国データバンク - ITmedia NEWS

⇧ 仮にトラブルが無かったとしても、「技術書」は安くても1冊3000円ぐらいするので、市場の拡大が困難そうではありますかね...

「活字離れは本当か?出版業界の今 | 経済産業省 METI Journal ONLINE」のサイトの統計情報によると、

⇧ そもそも、「出版市場」自体が横ばいで、且つ、「コミック」などの市場の割合が伸びているからして、「技術書」の市場動向が分からないのだが、芳しいとは思えない...

まぁ、「技術書」が1冊500円ほどであれば、気軽に購入できるのだが、如何せん、貧乏人には手が出にくい金額なのよね...

ちなみに、私も貧乏人に該当します、所謂、「ワーキングプア(working poor)」というやつですかね...

docker compose logs -fの内容をシェルスクリプトの処理で判定に使いたいのだが...

結構前に、

ts0818.hatenablog.com

⇧ 上記の記事で「Ansible」において、「docker-compose logs」相当の機能を実現する「モジュール」が用意されていないという話をしたのだが、そもそも、「シェルスクリプト」でも実現している情報が皆無だったという...

 

ネットの情報を漁っていたところ、

qiita.com

⇧ 上記サイト様を参考に実現できたので、備忘録として。

と言うか、有益な情報は「Google」検索した際に「上位表示」して欲しいところですわ...

Google Chrome」で実現されている「検索アルゴリズム」がイケてないということなのだとは思うけども...

■/home/ts0818/work-soft/docker-compose/docker-compose.yml

services:
  db:
    image: mysql:8.0.42-debian
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080    

とりあえず、「Docker Compose」を利用して、手動で「Docker コンテナ」を起動し、「Docker コンテナ」の出力するログの内容も確認しておく。

root@DESKTOP-JG9SBOV:/home/ts0818/work-soft/docker-compose# docker compose up -d
[+] Running 1/3
[+] Running 1/3ker-compose_default      Created                                                                                                                                                                                          0.1s
[+] Running 1/3ker-compose_default      Created                                                                                                                                                                                          0.1s
[+] Running 1/3ker-compose_default      Created                                                                                                                                                                                          0.1s
[+] Running 1/3ker-compose_default      Created                                                                                                                                                                                          0.1s
[+] Running 3/3ker-compose_default      Created                                                                                                                                                                                          0.1s
 ✔ Network docker-compose_default      Created                                                                                                                                                                                          0.1s ⠼ Container docker-compose-db-1       Starting                                                                                                                                                                                         0.4s
 ✔ Container docker-compose-db-1       Started                                                                                                                                                                                          0.5s
 ✔ Container docker-compose-adminer-1  Started                                                                                                                                                                                          0.5s
root@DESKTOP-JG9SBOV:/home/ts0818/work-soft/docker-compose# docker compose logs -f
adminer-1  | [Tue Jul  1 11:01:58 2025] PHP 8.4.8 Development Server (http://[::]:8080) started
db-1       | 2025-07-01 11:01:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.42-1debian12 started.
db-1       | 2025-07-01 11:01:58+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db-1       | 2025-07-01 11:01:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.42-1debian12 started.
db-1       | 2025-07-01 11:01:58+00:00 [Note] [Entrypoint]: Initializing database files
db-1       | 2025-07-01T11:01:58.871299Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
db-1       | 2025-07-01T11:01:58.871509Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.42) initializing of server in progress as process 80
db-1       | 2025-07-01T11:01:58.883279Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
db-1       | 2025-07-01T11:02:00.363782Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db-1       | 2025-07-01T11:02:03.035259Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
db-1       | 2025-07-01T11:02:03.354578Z 6 [Warning] [MY-013360] [Server] Plugin mysql_native_password reported: ''mysql_native_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instead'
db-1       | 2025-07-01 11:02:08+00:00 [Note] [Entrypoint]: Database files initialized
db-1       | 2025-07-01 11:02:08+00:00 [Note] [Entrypoint]: Starting temporary server
db-1       | 2025-07-01T11:02:08.991828Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
db-1       | 2025-07-01T11:02:08.991850Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.42) starting as process 124
db-1       | 2025-07-01T11:02:09.024826Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
db-1       | 2025-07-01T11:02:09.448715Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db-1       | 2025-07-01T11:02:10.021538Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db-1       | 2025-07-01T11:02:10.021699Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
db-1       | 2025-07-01T11:02:10.039443Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
db-1       | 2025-07-01T11:02:10.071033Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
db-1       | 2025-07-01T11:02:10.071342Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.42'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
db-1       | 2025-07-01 11:02:10+00:00 [Note] [Entrypoint]: Temporary server started.
db-1       | 2025-07-01T11:02:10.164788Z 8 [Warning] [MY-013360] [Server] Plugin mysql_native_password reported: ''mysql_native_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instead'
db-1       | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
db-1       | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
db-1       | Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
db-1       | Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
db-1       | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
db-1       | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
db-1       |
db-1       | 2025-07-01 11:02:12+00:00 [Note] [Entrypoint]: Stopping temporary server
db-1       | 2025-07-01T11:02:13.033399Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.42).
db-1       | 2025-07-01T11:02:15.155861Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.42)  MySQL Community Server - GPL.
db-1       | 2025-07-01 11:02:16+00:00 [Note] [Entrypoint]: Temporary server stopped
db-1       |
db-1       | 2025-07-01 11:02:16+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
db-1       |
db-1       | 2025-07-01T11:02:16.329252Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
db-1       | 2025-07-01T11:02:16.329272Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.42) starting as process 1
db-1       | 2025-07-01T11:02:16.343000Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
db-1       | 2025-07-01T11:02:16.855092Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db-1       | 2025-07-01T11:02:17.240959Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db-1       | 2025-07-01T11:02:17.241020Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
db-1       | 2025-07-01T11:02:17.248424Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
db-1       | 2025-07-01T11:02:17.278088Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
db-1       | 2025-07-01T11:02:17.278241Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.42'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.    

⇧ 検索したい文字列を確認。

今回は、参考サイト様と同じく「ready for connections」を検索ワードとする。

■docker compose logs -f の内容をシェルスクリプトで利用する

function check_compose_logs() {
  local -r TARGET_STRING="$1"

  local -r START_DATETIME_SECONDS=$(date +%s)
  local -r TIMEOUT_SECONDS=10
  
  #docker compose up -d

  # temp.log を作成して出力を保存
  temp_pipe="temp_${START_DATETIME_SECONDS}.pipe"

  # すでにファイルがあれば削除
  if [ -f "$temp_pipe" ]; then
    rm "$temp_pipe"
  fi
  mkfifo "$temp_pipe"
  # docker compose logs -f をバックグラウンドで実行して、出力を log_file にリダイレクト
  docker compose logs -f > "$temp_pipe" 2> /dev/null &

  # シグナル処理用の trap
  trap "kill -9 $!" 1 2 3 15

  # log_file から "ready for connections" を検索
  #grep --line-buffered -m 1 "ready for connections" > /dev/null < "$temp_pipe"

  # ログの出力を停止するために複数回シグナルを送る
  count=0
  
  while read line || kill -0 $! 2> /dev/null; do

    echo '['"${count}"']'"$line"
    if [[ -n $(echo "$line" | grep "${TARGET_STRING}") ]]; then
      echo "success docker compose up and compose service process."
      kill -2 $!
      sleep 0.1
      break
    fi
    
    current_datetime_seconds=$(date +%s)
    progress_datetime_seconds=$((current_datetime_seconds - START_DATETIME_SECONDS))
    echo 'current_datetime_seconds: '"${current_datetime_seconds}"
    echo 'START_DATETIME_SECONDS: '"${START_DATETIME_SECONDS}"
    echo 'progress_datetime_seconds: '"${progress_datetime_seconds}"
    if [[ "${progress_datetime_seconds}" -gt "${TIMEOUT_SECONDS}" ]]; then
      echo "timeout."
      kill -2 $!
      sleep 0.1
      break
    fi
    
    # ログの行数
    count=$((count +1))
#    sleep 1

  done < "$temp_pipe"
  rm "$temp_pipe"
}

check_compose_logs "ready for connections"
#check_compose_logs "error"

で、「シェル」にコピペして実行。

 

■結果

[3] 8695
[0]adminer-1  | [Tue Jul  1 11:01:58 2025] PHP 8.4.8 Development Server (http://[::]:8080) started
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[1]db-1       | 2025-07-01 11:01:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.42-1debian12 started.
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[2]db-1       | 2025-07-01 11:01:58+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[3]db-1       | 2025-07-01 11:01:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.42-1debian12 started.
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[4]db-1       | 2025-07-01 11:01:58+00:00 [Note] [Entrypoint]: Initializing database files
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[5]db-1       | 2025-07-01T11:01:58.871299Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[6]db-1       | 2025-07-01T11:01:58.871509Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.42) initializing of server in progress as process 80
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[7]db-1       | 2025-07-01T11:01:58.883279Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[8]db-1       | 2025-07-01T11:02:00.363782Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[9]db-1       | 2025-07-01T11:02:03.035259Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[10]db-1       | 2025-07-01T11:02:03.354578Z 6 [Warning] [MY-013360] [Server] Plugin mysql_native_password reported: ''mysql_native_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instead'
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[11]db-1       | 2025-07-01 11:02:08+00:00 [Note] [Entrypoint]: Database files initialized
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[12]db-1       | 2025-07-01 11:02:08+00:00 [Note] [Entrypoint]: Starting temporary server
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[13]db-1       | 2025-07-01T11:02:08.991828Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[14]db-1       | 2025-07-01T11:02:08.991850Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.42) starting as process 124
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[15]db-1       | 2025-07-01T11:02:09.024826Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[16]db-1       | 2025-07-01T11:02:09.448715Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[17]db-1       | 2025-07-01T11:02:10.021538Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[18]db-1       | 2025-07-01T11:02:10.021699Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[19]db-1       | 2025-07-01T11:02:10.039443Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
current_datetime_seconds: 1751375315
START_DATETIME_SECONDS: 1751375315
progress_datetime_seconds: 0
[20]db-1       | 2025-07-01T11:02:10.071033Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
success docker compose up and compose service process.
[3]+  Exit 130                docker compose logs -f > "$temp_pipe" 2> /dev/null
root@DESKTOP-JG9SBOV:/home/ts0818/work-soft/docker-compose# ls -la
total 24
drwxr-xr-x 2 root root 4096 Jul  1 22:08 .
drwxr-xr-x 7 root root 4096 Jul  1 18:31 ..
-rw-r--r-- 1 root root  267 Jul  1 18:42 docker-compose.yml
-rw-r--r-- 1 root root 6081 Jul  1 19:01 temp.log
-rwxr-xr-x 1 root root  840 Jul  1 19:48 test.sh
-rw-r--r-- 1 root root    0 Jul  1 18:56 test.txt

⇧ といった感じで、「シェルスクリプト」内の判定で「docker compos logs -f」を利用することができたものの、

  1. ログの行数が少ない
  2. 検索ワードが存在しない

の場合に、whileがハングするという残念仕様になっている...

なので、

  1. docker compose logs --since
  2. docker compose logs --until

などの期間指定でログを取得して判定するしか無さそう...

とりあえず、

qiita.com

⇧ 上記サイト様にありますように、「docker compose logs」の内容を一時ファイルに書き込んで、while文などで、一時ファイルの内容を1行ずつ読み込んで、grepしていく感じになるんかな?

この処理を実装したスクリプトをcronなどで定期的に実行する感じになるってことですかね?

ちなみに、変数に格納すると、「OOM(OutOfMemory)」が引き起こされる恐れがあるので、一時ファイルに書き込むのが無難な気がしている。

まぁ、いつものことながら、「シェルスクリプト」で「docker compos logs -f」を利用する情報がほとんど見当たらないんだが、一般的に正常に動作しているかどうかの確認どうしているんですかね?

毎度モヤモヤ感が半端ない…

今回はこのへんで。