GAミント至上主義

Web Monomaniacal Developer.

Dockerのコンテナ数が増えてきたのでdocker composeを導入した

自社の低予算ビッグデータ基盤oceanusのローカル環境での起動をdocker-composeを使ってできるようにした。

GitHub - uyamazak/oceanus: Save all data to Google BigQuery. Fast and row cost using Docker and Kubernetes on GCP

これまでは、それぞれのコンテナのディレクトリにrun_local.shみたいなシェルスクリプト置いて実行していた。

また会社用Mastodonを動かす際に、docker-conpose.ymlがあって、使ったのもいいきっかけだった。


でもコンテナの種類が8まで増えて、サーバーの再起動時や開発中にどれが止まっているのかなどを確認するのが面倒になってきた。

ログを表示させるためデーモン化せずに起動させようとすると、それぞれにターミナルが必要になるので、それだけで大量のtmuxウィンドウを消費していたけど、docker-composeであれば1ウィンドウですべての標準出力を確認できるので、見通しも良くなった。

また、docker-compose.ymlを見るだけで、サービスの依存関係が分かるので、知らない人が起動するのにも便利。



compose化にあたって、環境変数もファイル化した。それぞれのディレクトリに.env.localというファイル名で配置。

version: '3'
services:
  arms:
    image: "asia.gcr.io/oceanus-dev/arms:latest"
    env_file: ./arms/.env.local
    ports:
     - "8080:80"
    command: [gunicorn, -c, gunicorn_conf_debug.py, "arms:app"]
    depends_on:
      - redis-pd
      - gopub

  redis-pd:
    image: "asia.gcr.io/oceanus-dev/redis-pd:latest"
    ports:
      - "6379:6379"
    volumes:
      - .redis-pd/data:/data

  r2bq:
    image: "asia.gcr.io/oceanus-dev/r2bq:latest"
    env_file: ./r2bq/.env.local
    depends_on:
      - redis-pd

  table-manager:
    env_file: ./table-manager/.env.local
    image: "asia.gcr.io/oceanus-dev/table-manager:latest"

  revelation:
    image: "asia.gcr.io/oceanus-dev/revelation:latest"
    env_file: ./revelation/.env.local
    depends_on:
      - redis-pd
      - rabbitmq

  revelation-worker:
    image: "asia.gcr.io/oceanus-dev/revelation:latest"
    env_file: ./revelation/.env.local
    depends_on:
      - redis-pd
      - rabbitmq
    command: [celery, -A, "task.celery_app", worker, "--loglevel=DEBUG"]

  rabbitmq:
    image: "asia.gcr.io/oceanus-dev/rabbitmq:latest"
    env_file: ./rabbitmq/.env.local
    ports:
      - "5672:5672"

  gopub:
    image: "asia.gcr.io/oceanus-dev/gopub:latest"
    env_file: ./gopub/.env.local
    command: [go, run, "app/main.go"]
    ports:
      - "8765:8765"

一つハマった箇所は、
.env.localの中でdocker run時の-eと同じように

HOST="http:www.example.com"

という風にしてしまうと、""込みでプログラム側で読み込んでしまい、動かなくなる箇所がいくつかあった。
囲まずにかけばおk。

HOST=http:www.example.com

本番環境は、Google Container Engineなので、env_file的なものは使えないけど、KubernetesにはConfigMapというのがあるらしいので、今度はそちらへの移行を進めようと思う。
Using ConfigMap Data in Pods | Kubernetes