#Airflow学習用の環境構築(Airflow×Jupyter)
Airflow学習用にローカル側でDockerコンテナを利用して環境構築をする。Airflowで実行するPythonスクリプトの開発環境用にJupyterを連携して立ち上げる。
#ディレクトリ構造#
myairflow
- AirFlow # Airflow側のマウント先ディレクトリ(任意の命名)
- Mount_Directory # Jupyter側のマウント先ディレクトリ(任意の命名)
- .env # Airflow側の環境変数
- docker-compose.yml # 立ち上げ用($Docker-compose up -d)
- Dockerfile # Jupyter側の環境構築
- requirements.txt # Jupyter側で利用するライブラリ
#Docker-compose.yml
# Apache Software Foundation (ASF) のライセンス(Apache License 2.0)
# 開発やテスト目的のみに使ってください(本番環境には非推奨)
# ========================= #
# Airflow の初期化 ==> 起動
# ========================= #
## "docker-compose up airflow-init" ==> "docker-compose up -d"
# ============================== #
# Airflow のデバック(Flower|CLI)
# ============================== #
## "docker-compose --profile flower up" | "docker-compose --profile debug run airflow-cli XXXX "
# `PythonOperator`等を利用して作成したDAGファイルは、次のディレクトリに配置して再起動すること
# --> 「AirFlow/dags/***」+ $docker-compose up -d --> UI画面にファイルが反映される
# ================================ #
# {環境変数名}/{意味}/{デフォルト値}
# ================================ #
## AIRFLOW_IMAGE_NAME |使用する Docker イメージ |apache/airflow:2.10.5
## AIRFLOW_UID |Airflow の実行ユーザーID |50000
## AIRFLOW_PROJ_DIR |ホスト側のベースディレクトリ|.
## _AIRFLOW_WWW_USER_USERNAME |Airflow の管理者ユーザー名 |airflow
## _AIRFLOW_WWW_USER_PASSWORD |Airflow の管理者パスワード |airflow
## _PIP_ADDITIONAL_REQUIREMENTS|起動時に追加インストールするPythonパッケージ|空文字
#
# =================================================================== #
# Airflow クラスタ構成(CeleryExecutor + Redis + PostgreSQL):共通設定
# =================================================================== #
---
x-airflow-common:
&airflow-common
image: ${AIRFLOW_IMAGE_NAME:-apache/airflow:2.10.5}
depends_on:
&airflow-common-depends-on
redis:
condition: service_healthy
postgres:
condition: service_healthy
user: "${AIRFLOW_UID:-50000}:0"
volumes:
- ${AIRFLOW_PROJ_DIR:-.}/dags:/opt/airflow/dags
- ${AIRFLOW_PROJ_DIR:-.}/logs:/opt/airflow/logs
- ${AIRFLOW_PROJ_DIR:-.}/config:/opt/airflow/config
- ${AIRFLOW_PROJ_DIR:-.}/plugins:/opt/airflow/plugins
environment:
&airflow-common-env
AIRFLOW__CORE__EXECUTOR: CeleryExecutor
AIRFLOW__DATABASE__SQL_ALCHEMY_CONN: postgresql+psycopg2://airflow:airflow@postgres/airflow
AIRFLOW__CELERY__RESULT_BACKEND: db+postgresql://airflow:airflow@postgres/airflow
AIRFLOW__CELERY__BROKER_URL: redis://:@redis:6379/0
AIRFLOW__CORE__FERNET_KEY: ''
AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION: 'true'
AIRFLOW__CORE__LOAD_EXAMPLES: 'false' # 'true'
AIRFLOW__API__AUTH_BACKENDS: 'airflow.api.auth.backend.basic_auth,airflow.api.auth.backend.session'
AIRFLOW__SCHEDULER__ENABLE_HEALTH_CHECK: 'true'
_PIP_ADDITIONAL_REQUIREMENTS: ${_PIP_ADDITIONAL_REQUIREMENTS:-}
# AIRFLOW_CONFIG: '/opt/airflow/config/airflow.cfg'
# ============================ #
# Airflow マウント設定{volumes}
# ============================ #
## /opt/airflow/dags | DAGファイルを置く場所
## /opt/airflow/logs | ログ出力先
## /opt/airflow/config | 設定ファイル用(任意)
## /opt/airflow/plugins | プラグインなどの拡張コード
#
# ============================= #
# Airflow 動作設定{environment}
# ============================= #
## AIRFLOW__CORE__EXECUTOR | `CeleryExecutor`を使用する
## AIRFLOW__DATABASE__SQL_ALCHEMY_CONN | `PostgreSQL`をDBに設定する
## AIRFLOW__CELERY__RESULT_BACKEND | `Celery`の結果保存先をDBに設定する
## AIRFLOW__CELERY__BROKER_URL | `Celery`のブローカーとして`Redis`を使用する
## AIRFLOW__CORE__FERNET_KEY | 暗号化キー(未設定)
## AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION | `DAG`は初期状態で一時停止する
## AIRFLOW__CORE__LOAD_EXAMPLES | `サンプルDAG`を読み込むか否か
## AIRFLOW__API__AUTH_BACKENDS | `Basic認証`と`セッション認証`を有効化する
## _PIP_ADDITIONAL_REQUIREMENTS | コンテナ起動時に追加パッケージ
services:
# ===================== #
# Airflow のメタデータDB
# ===================== #
postgres:
image: postgres:13
environment:
POSTGRES_USER: airflow
POSTGRES_PASSWORD: airflow
POSTGRES_DB: airflow
volumes:
- postgres-db-volume:/var/lib/postgresql/data
healthcheck:
test: ["CMD", "pg_isready", "-U", "airflow"]
interval: 10s
retries: 5
start_period: 5s
restart: always
# ======================================================= #
# Celery Executor(分散処理)のメッセージキューとして利用する
# ======================================================= #
redis:
image: redis:7.2-bookworm # # https://redis.io/blog/redis-adopts-dual-source-available-licensing/
expose:
- 6379
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 30s
retries: 50
start_period: 30s
restart: always
# =================== #
# AirflowのWeb UI画面
# =================== #
airflow-webserver:
<<: *airflow-common
command: webserver
ports:
- "8080:8080"
healthcheck:
test: ["CMD", "curl", "--fail", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 5
start_period: 30s
restart: always
depends_on:
<<: *airflow-common-depends-on
airflow-init:
condition: service_completed_successfully
# ================================ #
# Airflow:DAGのスケジューリング処理
# ================================ #
airflow-scheduler:
<<: *airflow-common
command: scheduler
healthcheck:
test: ["CMD", "curl", "--fail", "http://localhost:8974/health"]
interval: 30s
timeout: 10s
retries: 5
start_period: 30s
restart: always
depends_on:
<<: *airflow-common-depends-on
airflow-init:
condition: service_completed_successfully
# ========================================== #
# Airflow:DAG内のタスクをCelery経由で実行する
# ========================================== #
airflow-worker:
<<: *airflow-common
command: celery worker
healthcheck:
test:
- "CMD-SHELL"
- 'celery --app airflow.providers.celery.executors.celery_executor.app inspect ping -d "celery@$${HOSTNAME}" || celery --app airflow.executors.celery_executor.app inspect ping -d "celery@$${HOSTNAME}"'
interval: 30s
timeout: 10s
retries: 5
start_period: 30s
environment:
<<: *airflow-common-env
DUMB_INIT_SETSID: "0"
restart: always
depends_on:
<<: *airflow-common-depends-on
airflow-init:
condition: service_completed_successfully
# ========================================== #
# Event-driven DAG:Triggerer 処理を実行する
# ========================================== #
airflow-triggerer:
<<: *airflow-common
command: triggerer
healthcheck:
test: ["CMD-SHELL", 'airflow jobs check --job-type TriggererJob --hostname "$${HOSTNAME}"']
interval: 30s
timeout: 10s
retries: 5
start_period: 30s
restart: always
depends_on:
<<: *airflow-common-depends-on
airflow-init:
condition: service_completed_successfully
# ============================================ #
# 初期化専用:DBマイグレーションやユーザー作成など
# ============================================ #
airflow-init:
<<: *airflow-common
entrypoint: /bin/bash
command:
- -c
- |
if [[ -z "${AIRFLOW_UID}" ]]; then
echo
echo -e "\033[1;33mWARNING!!!: AIRFLOW_UID not set!\e[0m"
echo "If you are on Linux, you SHOULD follow the instructions below to set "
echo "AIRFLOW_UID environment variable, otherwise files will be owned by root."
echo "For other operating systems you can get rid of the warning with manually created .env file:"
echo " See: https://airflow.apache.org/docs/apache-airflow/stable/howto/docker-compose/index.html#setting-the-right-airflow-user"
echo
fi
one_meg=1048576
mem_available=$$(($$(getconf _PHYS_PAGES) * $$(getconf PAGE_SIZE) / one_meg))
cpus_available=$$(grep -cE 'cpu[0-9]+' /proc/stat)
disk_available=$$(df / | tail -1 | awk '{print $$4}')
warning_resources="false"
if (( mem_available < 4000 )) ; then
echo
echo -e "\033[1;33mWARNING!!!: Not enough memory available for Docker.\e[0m"
echo "At least 4GB of memory required. You have $$(numfmt --to iec $$((mem_available * one_meg)))"
echo
warning_resources="true"
fi
if (( cpus_available < 2 )); then
echo
echo -e "\033[1;33mWARNING!!!: Not enough CPUS available for Docker.\e[0m"
echo "At least 2 CPUs recommended. You have $${cpus_available}"
echo
warning_resources="true"
fi
if (( disk_available < one_meg * 10 )); then
echo
echo -e "\033[1;33mWARNING!!!: Not enough Disk space available for Docker.\e[0m"
echo "At least 10 GBs recommended. You have $$(numfmt --to iec $$((disk_available * 1024 )))"
echo
warning_resources="true"
fi
if [[ $${warning_resources} == "true" ]]; then
echo
echo -e "\033[1;33mWARNING!!!: You have not enough resources to run Airflow (see above)!\e[0m"
echo "Please follow the instructions to increase amount of resources available:"
echo " https://airflow.apache.org/docs/apache-airflow/stable/howto/docker-compose/index.html#before-you-begin"
echo
fi
mkdir -p /sources/logs /sources/dags /sources/plugins
chown -R "${AIRFLOW_UID}:0" /sources/{logs,dags,plugins}
exec /entrypoint airflow version
environment:
<<: *airflow-common-env
_AIRFLOW_DB_MIGRATE: 'true'
_AIRFLOW_WWW_USER_CREATE: 'true'
_AIRFLOW_WWW_USER_USERNAME: ${_AIRFLOW_WWW_USER_USERNAME:-airflow}
_AIRFLOW_WWW_USER_PASSWORD: ${_AIRFLOW_WWW_USER_PASSWORD:-airflow}
_PIP_ADDITIONAL_REQUIREMENTS: ''
user: "0:0"
volumes:
- ${AIRFLOW_PROJ_DIR:-.}:/sources
# ================================ #
# airflow コマンド:デバッグやCLI用
# ================================ #
airflow-cli:
<<: *airflow-common
profiles:
- debug
environment:
<<: *airflow-common-env
CONNECTION_CHECK_MAX_COUNT: "0"
command:
- bash
- -c
- airflow
# ================================ #
# Celeryタスク:モニタリングツール
# ================================ #
flower:
<<: *airflow-common
command: celery flower
profiles:
- flower
ports:
- "5555:5555"
healthcheck:
test: ["CMD", "curl", "--fail", "http://localhost:5555/"]
interval: 30s
timeout: 10s
retries: 5
start_period: 30s
restart: always
depends_on:
<<: *airflow-common-depends-on
airflow-init:
condition: service_completed_successfully
# ================== #
# JupyterLab(連携用)
# ================== #
datasciense:
container_name: jupyterlab
build:
context: .
dockerfile: Dockerfile
ports:
- "9678:8888"
volumes:
- ./Mount_Directory:/Mount_Directory
environment:
- LANG=ja_JP.UTF-8
- TZ=Asia/Tokyo
command: jupyter lab --ip=0.0.0.0 --allow-root --NotebookApp.token=''
# Airflow本体やDAGなどの永続化
volumes:
postgres-db-volume:
#.env
# AirflowユーザーのUID (Linuxユーザーなら id -u で確認)
AIRFLOW_UID=50000
# プロジェクトのルートパス(通常はカレントディレクトリ)| .
AIRFLOW_PROJ_DIR=./AirFlow
# Airflow イメージの指定(任意。カスタムビルドしてる場合に使う)
AIRFLOW_IMAGE_NAME=apache/airflow:2.10.5
# Airflow Web UI の初期ユーザー名とパスワード
_AIRFLOW_WWW_USER_USERNAME=admin
_AIRFLOW_WWW_USER_PASSWORD=password
# Airflow サンプルデータの有無{'true' or 'false'}
# AIRFLOW__CORE__LOAD_EXAMPLES: 'false'
# 追加でインストールする Python パッケージ (例: google-cloud-storage, pandas 等)
# _PIP_ADDITIONAL_REQUIREMENTS=
# (必要に応じて)Airflowの設定ファイルを直接指定する場合
# AIRFLOW_CONFIG=/opt/airflow/config/airflow.cfg
#Dockerfile
FROM ubuntu:22.04
ENV TZ=Asia/Tokyo
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
## 必要なパッケージ(apt-get) ##
RUN apt-get update && apt-get install -yq --no-install-recommends \
python3-pip \
python3-dev \
sudo \
vim \
curl \
wget \
unzip \
tzdata \
git \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
## 必要なパッケージ(pip)
COPY requirements.txt .
RUN pip install --upgrade pip && \
pip install --break-system-packages -r requirements.txt
## Jupyter NotebookのPDF出力を日本語に対応する
RUN sed -i 's/\\documentclass\[11pt\]{article}/\\documentclass\[xelatex,ja=standard\]{bxjsarticle}/' \
/usr/local/share/jupyter/nbconvert/templates/latex/index.tex.j2
## JupyterLab 作業ディレクトリ
WORKDIR /Mount_Directory
CMD ["jupyter", "lab", "--ip=0.0.0.0", "--allow-root", "--LabApp.token=''"]
#requirements.txt
# 共通 (JupyterLab)
jupyterlab==4.2.5
jupyterlab-language-pack-ja-JP
# データサイエンス
numpy==2.1.1
pandas==2.2.2
matplotlib==3.9.2
コメント(一覧)