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

基于Airflow镜像的Dockerfile无法安装requirements.txt依赖求助

解决Dockerfile中pip安装依赖超时问题(公司代理环境)

针对公司代理环境下,Docker构建阶段pip安装依赖超时,但容器运行后手动安装正常的问题,以下是几个可行的解决方案:

1. 在Dockerfile中直接注入代理环境变量

Docker构建阶段不会自动继承主机的systemd代理配置,也不会读取.docker/config.json中的代理(该配置仅在容器运行时生效)。需要在Dockerfile中显式添加代理环境变量,确保构建阶段的pip能访问外部网络:

修改后的Dockerfile:

FROM apache/airflow:2.7.1
ADD requirements.txt .

# 配置构建阶段的代理环境变量
ENV HTTP_PROXY=http://user:pwd@ip:8080
ENV HTTPS_PROXY=http://user:pwd@ip:8080
ENV NO_PROXY=localhost,127.0.0.1,172.17.0.,.company.com,cc-artifactory.company.net,artifactory.ocp-sdp.p4avd.company.com

# 延长pip超时时间,适配公司网络速度
ENV PIP_DEFAULT_TIMEOUT 120

RUN pip install --upgrade pip
RUN pip install apache-airflow==${AIRFLOW_VERSION} -r requirements.txt

2. 通过构建参数传递代理(避免硬编码敏感信息)

如果不想把代理密码写死在Dockerfile里,可以用--build-arg参数在构建时动态传递代理配置:

步骤1:修改Dockerfile,添加构建参数定义

FROM apache/airflow:2.7.1
ADD requirements.txt .

# 定义代理相关的构建参数
ARG HTTP_PROXY
ARG HTTPS_PROXY
ARG NO_PROXY

# 将构建参数转为环境变量,供pip使用
ENV HTTP_PROXY=${HTTP_PROXY}
ENV HTTPS_PROXY=${HTTPS_PROXY}
ENV NO_PROXY=${NO_PROXY}

ENV PIP_DEFAULT_TIMEOUT 120

RUN pip install --upgrade pip
RUN pip install apache-airflow==${AIRFLOW_VERSION} -r requirements.txt

步骤2:在docker-compose.yml中配置构建参数

services:
  airflow-worker:
    build:
      context: .
      args:
        HTTP_PROXY: http://user:pwd@ip:8080
        HTTPS_PROXY: http://user:pwd@ip:8080
        NO_PROXY: localhost,127.0.0.1,172.17.0.,.company.com,cc-artifactory.company.net,artifactory.ocp-sdp.p4avd.company.com

之后执行docker compose up --build即可。

3. 检查并调整pip源配置

Apache Airflow基础镜像可能默认使用特定的pip源,导致代理无法正常工作。可以强制指定公司内部的PyPI镜像源(比如你的artifactory地址),确保依赖从内部源获取:

RUN pip install --upgrade pip
RUN pip install --index-url https://cc-artifactory.company.net/api/pypi/pypi/simple apache-airflow==${AIRFLOW_VERSION} -r requirements.txt

如果必须使用官方PyPI源,可添加--trusted-host参数避免SSL证书问题:

RUN pip install --upgrade pip
RUN pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org apache-airflow==${AIRFLOW_VERSION} -r requirements.txt

4. 验证构建阶段的网络连通性

如果以上方案仍无效,可以在Dockerfile中添加curl命令测试网络连通性,确认代理是否生效:

RUN curl -v https://pypi.org

如果curl超时,说明代理地址、密码或NO_PROXY配置有误;如果curl能正常访问,可尝试在pip命令中直接指定代理参数:

RUN pip install --upgrade pip --proxy=http://user:pwd@ip:8080
RUN pip install --proxy=http://user:pwd@ip:8080 apache-airflow==${AIRFLOW_VERSION} -r requirements.txt

内容的提问来源于stack exchange,提问作者Gerardo Zinno

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 02:17:16