You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Airflow容器中requirements.txt依赖未安装问题求助

Airflow Docker容器依赖安装失败问题解决

问题场景

在Windows 11环境下用Docker部署Airflow 2.7.3,通过requirements.txt指定依赖(pandas、scikit-learn、matplotlib等),但重建镜像启动容器后,DAG报错找不到对应模块:

Broken DAG: [/opt/airflow/dags/my_dag.py] Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/opt/airflow/dags/my_dag.py", line 6, in <module>
    import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'

执行以下命令重建镜像后问题依旧:

docker-compose down
docker-compose build
docker-compose up -d

当前配置文件:

  • requirements.txt:
pandas
scikit-learn
sqlalchemy
airflow
dbt
matplotlib
  • Dockerfile:
# Use an official Airflow image as the base image
FROM apache/airflow:2.7.3

# Set the working directory to /usr/src/app
WORKDIR /usr/src/app

# Copy the requirements.txt file into the container at /usr/src/app
COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

# Install any needed packages specified in requirements.txt
#RUN pip install --no-cache-dir -r ./requirements.txt

# Make port 8080 available to the world outside this container
EXPOSE 8080

问题根源

  1. requirements.txt存在冲突依赖:官方Airflow镜像已预装对应版本的Airflow,手动添加airflow会导致版本覆盖,破坏原有环境;dbt应指定具体包名(如dbt-core),否则无法正确安装。
  2. Dockerfile路径配置错误:将工作目录改为/usr/src/app,但Airflow的Python运行环境依赖/opt/airflow下的路径,导致安装的包不在Airflow的模块搜索路径中。
  3. Docker Compose未统一使用自定义镜像:若docker-compose.yml中worker、scheduler等服务仍使用官方镜像,而非你构建的自定义镜像,依赖自然不会生效。

解决方案

1. 修正requirements.txt

移除冲突的airflow,并修正dbt为具体包名:

pandas
scikit-learn
sqlalchemy
dbt-core
matplotlib

2. 调整Dockerfile

对齐官方镜像的默认路径,确保依赖安装到Airflow的运行环境中:

# 使用官方Airflow镜像作为基础
FROM apache/airflow:2.7.3

# 明确切换到airflow用户(官方镜像默认已设置,此处可省略,但明确指定更清晰)
USER airflow

# 将requirements.txt复制到Airflow默认工作目录
COPY requirements.txt /opt/airflow/

# 安装依赖,确保使用airflow用户的pip环境
RUN pip install --no-cache-dir -r requirements.txt

说明:无需手动暴露8080端口,官方镜像已配置相关端口映射。

3. 确保Docker Compose使用自定义镜像

修改docker-compose.yml,让所有Airflow服务(webserver、scheduler、worker)复用自定义构建的镜像:

version: '3.8'
# 定义通用配置
x-airflow-common: &airflow-common
  build: .  # 使用当前目录的Dockerfile构建镜像
  image: custom-airflow:2.7.3  # 自定义镜像名称
  environment:
    &airflow-common-env
    AIRFLOW__CORE__EXECUTOR: CeleryExecutor
    # 其他环境变量配置...
  volumes:
    - ./dags:/opt/airflow/dags
    - ./logs:/opt/airflow/logs
    - ./plugins:/opt/airflow/plugins
  user: "${AIRFLOW_UID:-50000}:0"

services:
  airflow-webserver:
    <<: *airflow-common
    command: webserver
    ports:
      - "8080:8080"
    healthcheck:
      test: ["CMD", "curl", "--fail", "http://localhost:8080/health"]
      interval: 30s
      timeout: 30s
      retries: 3

  airflow-scheduler:
    <<: *airflow-common
    command: scheduler
    healthcheck:
      test: ["CMD-SHELL", 'airflow jobs check --job-type SchedulerJob --hostname "$${HOSTNAME}"']
      interval: 30s
      timeout: 30s
      retries: 3

  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}"'
      interval: 30s
      timeout: 30s
      retries: 3

4. 重新构建并启动容器

执行以下命令彻底清理旧环境并启动:

# 停止容器并删除关联卷、网络
docker-compose down -v
# 无缓存构建镜像,确保依赖完全重新安装
docker-compose build --no-cache
# 后台启动服务
docker-compose up -d

5. 验证依赖安装情况

进入worker容器检查依赖是否成功安装:

# 检查matplotlib
docker-compose exec airflow-worker pip list | grep matplotlib
# 检查scikit-learn
docker-compose exec airflow-worker pip list | grep scikit-learn

内容的提问来源于stack exchange,提问作者omer pyt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 23:42:55