【dbt】dbt ~ 環境設定 / Docker 編 ~

■ はじめに

dbt の ドキュメント化(データリネージ機能)を使うために
ローカル環境を整備してたら、docker 環境があれば
pip よりも簡単に使えることが分かったのでメモしておく

公式ドキュメントより
https://docs.getdbt.com/docs/core/installation-overview#install-dbt-core

[1] Use pip to install dbt (recommended)
[2] Use a Docker image to install dbt << 今回は、こちらを選択
[3] Install dbt from source

[1] については、以下の関連記事を参照のこと

dbt ~ 環境設定編 ~
https://dk521123.hatenablog.com/entry/2023/12/16/152147

目次

【0】前提条件
 1)Docker環境
 2)DB環境
【1】ローカル環境
 1)使用するDockerイメージ
 2)ローカル環境の確認
【2】Hello world
 1)ディレクトリ構成
 2)コマンド例

【0】前提条件

1)Docker環境

* 以下の関連記事を参照のこと

Docker ~ Linux / 環境構築編 ~
https://dk521123.hatenablog.com/entry/2018/04/10/234030
Docker ~ docker を sudo なしで実行する ~
https://dk521123.hatenablog.com/entry/2023/11/22/000000

2)DB環境

* 今回は、PostgreSQL を使う

Docker compose ~ PostgreSQL
https://dk521123.hatenablog.com/entry/2023/07/20/025544
Docker compose ~ Pgweb/pgAdmin ~
https://dk521123.hatenablog.com/entry/2023/08/10/111919

【1】ローカル環境

1)使用するDockerイメージ

https://github.com/dbt-labs/dbt-core/pkgs/container/dbt-postgres

docker pull ghcr.io/dbt-labs/dbt-postgres:1.8.2

2)ローカル環境の確認

* DBT Version表示を通して、
 自分のローカル環境が最低限設定されているかを確認してみる

https://github.com/dbt-labs/dbt-core/pkgs/container/dbt-postgres

mkdir demo-dbt
cd demo-dbt

mkdir dbt_hello_world

$ docker run --rm -v $(pwd)/dbt_hello_world:/dbt_hello_world \
--net=host ghcr.io/dbt-labs/dbt-postgres:1.8.2 \
--version
~~~
Core:
  - installed: 1.8.3
  - latest:    1.8.7 - Update available!

  Your version of dbt-core is out of date!
  You can find instructions for upgrading here:
  https://docs.getdbt.com/docs/installation

Plugins:
  - postgres: 1.8.2 - Up to date!
~~~

解説

# --rmオプション: コンテナ終了時にコンテナ自動的に削除
# -v: ホストの dbt_hello_world をコンテナにマウント
# --net=host: コンテナにおいて、ネットワーク名前空間をホストと共有
$ docker run --rm -v $(pwd)/dbt_hello_world:/dbt_hello_world \
--net=host ghcr.io/dbt-labs/dbt-postgres:1.8.2 \
 <dbtコマンド>

【2】Hello world

1)ディレクトリ構成

~/demo-dbt
  + compose.yml
  + dbt_hello_world
      + profiles.yml
      + dbt_project.yml

compose.yml

version: '3'

services:
  # PostgreSQL
  postgres:
    image: postgres:latest
    container_name: postgres
    hostname: postgresql
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      PGPASSWORD: password
      POSTGRES_DB: sample
      TZ: "Asia/Tokyo"
    ports:
      - 5431:5432
    volumes:
      - ./postgres/init:/docker-entrypoint-initdb.d
      - ./postgres/data:/var/lib/postgresql/data
  # pgAdmin
  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin
    hostname: pgadmin
    restart: always
    ports:
      - 18081:80
    volumes:
      - volume_pgadmin:/var/lib/pgadmin
    environment:
      PGADMIN_DEFAULT_EMAIL: demo@sample.com
      PGADMIN_DEFAULT_PASSWORD: password
    depends_on:
      - postgres
volumes:
  volume_pgadmin:
    name: v_pgadmin

profiles.yml

dbt_hello_world:
  target: dev
  outputs:
    dev:
      type: postgres
      threads: 1
      host: localhost
      port: 5431
      user: postgres
      password: password
      dbname: sample
      schema: dbt_demo

dbt_project.yml

name: 'dbt_hello_world'
config-version: 2
version: '1.0.0'

profile: 'dbt_hello_world'

model-paths: ["models"]
analysis-paths: ["analysis"]
test-paths: ["tests"]
seed-paths: ["seeds"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

target-path: "target"
clean-targets: [target, dbt_packages]

models:
  dbt_hello_world:
    example:

2)コマンド例

$ cd ~/demo-dbt

# PostgreSQLを立ち上げる
$ docker compose up -d

# Run 'dbt debug'
$ docker run --rm --network=host \
-v $(pwd)/dbt_hello_world:/usr/app/dbt_hello_world \
ghcr.io/dbt-labs/dbt-postgres:1.8.2 \
debug --profiles-dir="/usr/app/dbt_hello_world" \
--project-dir="/usr/app/dbt_hello_world"
===
14:03:44  Running with dbt=1.8.3
14:03:44  dbt version: 1.8.3
14:03:44  python version: 3.11.2
14:03:44  python path: /usr/local/bin/python
14:03:44  os info: Linux-6.8.0-40-generic-x86_64-with-glibc2.31
14:03:44  Using profiles dir at /usr/app/
14:03:44  Using profiles.yml file at /usr/app/profiles.yml
14:03:44  Using dbt_project.yml file at /usr/app/dbt_project.yml
14:03:44  adapter type: postgres
14:03:44  adapter version: 1.8.2
14:03:44  Configuration:
14:03:44    profiles.yml file [OK found and valid]
14:03:44    dbt_project.yml file [OK found and valid]
14:03:44  Required dependencies:
14:03:44   - git [OK found]

14:03:44  Connection:
14:03:44    host: localhost
14:03:44    port: 5431
14:03:44    user: postgres
14:03:44    database: sample
14:03:44    schema: dbt_demo
14:03:44    connect_timeout: 10
14:03:44    role: None
14:03:44    search_path: None
14:03:44    keepalives_idle: 0
14:03:44    sslmode: None
14:03:44    sslcert: None
14:03:44    sslkey: None
14:03:44    sslrootcert: None
14:03:44    application_name: dbt
14:03:44    retries: 1
14:03:44  Registered adapter: postgres=1.8.2
14:03:44    Connection test: [OK connection ok]

14:03:44  All checks passed!

参考文献

* Dockerの設定は、公式ドキュメントにも載っている

https://docs.getdbt.com/docs/core/docker-install

関連記事

dbt ~ 環境設定編 ~
https://dk521123.hatenablog.com/entry/2023/12/16/152147
dbt ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2023/06/30/000000
dbt ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/05/30/151003
dbt ~ ドキュメント化 / dbt docs ~
https://dk521123.hatenablog.com/entry/2023/12/10/125512
Docker ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/02/25/000000
Docker ~ Linux / 環境構築編 ~
https://dk521123.hatenablog.com/entry/2018/04/10/234030
Docker ~ 基本編 / docker network ~
https://dk521123.hatenablog.com/entry/2022/04/30/000000
Docker ~ 基本編 / Data Volume ~
https://dk521123.hatenablog.com/entry/2018/09/08/222100
Docker ~ docker を sudo なしで実行する ~
https://dk521123.hatenablog.com/entry/2023/11/22/000000
Docker compose ~ PostgreSQL
https://dk521123.hatenablog.com/entry/2023/07/20/025544
Docker compose ~ Pgweb/pgAdmin ~
https://dk521123.hatenablog.com/entry/2023/08/10/111919