■ はじめに
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】docker pull で構築する 1)使用するDockerイメージ 2)ローカル環境の確認 【2】PostgreSQLをdocker composeで構築 1)ディレクトリ構成 2)コマンド例 【3】dbt も docker compose で構築する 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】docker pull で構築する
1)使用するDockerイメージ
https://github.com/dbt-labs/dbt-core/pkgs/container/dbt-postgres
docker pull ghcr.io/dbt-labs/dbt-postgres:1.8.2 # 現在の最新は # ghcr.io/dbt-labs/dbt-postgres:1.9.latest
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】PostgreSQLをdocker composeで構築
* docker compose でPostgreSQLを構築して一通り構築してみる
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!
【3】dbt も docker compose で構築する
* ついでに、pgadmin も入れとく
1)フォルダ構成
+ compose.yml
compose.yml
services: db: platform: linux/x86_64 image: postgres:16 container_name: dbt_postgres environment: POSTGRES_USER: dbt_user POSTGRES_PASSWORD: dbt_pass POSTGRES_DB: dbt_db ports: - "5432:5432" volumes: - pgdata:/var/lib/postgresql/data dbt: platform: linux/x86_64 image: ghcr.io/dbt-labs/dbt-postgres:1.9.latest container_name: dbt_cli working_dir: /usr/app volumes: - ./dbt:/usr/app - ./.dbt:/root/.dbt depends_on: - db entrypoint: [ "tail", "-f", "/dev/null" ] # 起動後にシェルで入れるように待機状態 pgadmin: platform: linux/x86_64 image: dpage/pgadmin4 container_name: pgadmin restart: always environment: PGADMIN_DEFAULT_EMAIL: admin@admin.com PGADMIN_DEFAULT_PASSWORD: admin ports: - "8080:80" volumes: - pgadmin_data:/var/lib/pgadmin depends_on: - db volumes: pgdata: pgadmin_data:
2)セットアップ手順
[1] 上記の compose.yml を作成 [2] ターミナルで docker-compose up -d を実行 [3] dbt コンテナに入って以下を実行
コマンド例
$ docker exec -it dbt_cli bash # DBT プロジェクトを作成していない場合 $ dbt init my_project # profiles.yml を以下のように設定: $ cd my_project # ここでエラーになった場合、/root/.dbt/profiles.ymlに誤りがある可能性あり $ dbt debug # => コンテナ内に入らずに行う場合 # docker exec dbt_cli /bin/sh -c "cd my_project && dbt debug" $ dbt run # 成功したら以下で pgadmin に接続し、接続情報を設定し「select * from my_second_dbt_model;」を実行する
/root/.dbt/profiles.yml(コンテナ内)
my_project: target: dev outputs: dev: type: postgres host: db user: dbt_user password: dbt_pass port: 5432 dbname: dbt_db schema: public threads: 1
参考文献
* 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
dbt ~ Version更新あれこれ ~
https://dk521123.hatenablog.com/entry/2024/12/23/010839
dbt-athena ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2025/11/14/152128
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