Docker部署Django时遇403 MediaTypeBlocked错误,求解决
Docker构建Django项目时apt-get安装gettext报错403 MediaTypeBlocked
执行命令:
docker compose -f local.yml up --build -d --remove-orphans
构建过程中出现以下错误日志:
初始错误片段
=> ERROR [some-api_api python-run-stage 2/8] RUN apt-get update && apt-get install --no-install-recommends -y libpq-dev gettext && apt-get purge -y --auto-remove -o 6.9s => [some-api_postgres internal] load build context 0.1s => => transferring context: 692B 0.0s => [some-api_postgres 1/4] FROM docker.io/library/postgres:14.4@sha256:9ceb24f8c5f15c053d973a3610866f473690875dc13eb3282b45302189321040 0.0s => CACHED [some-api_postgres 2/4] COPY ./docker/local/postgres/maintenance /usr/local/bin/maintenance 0.0s => CACHED [some-api_postgres 3/4] RUN chmod +x /usr/local/bin/maintenance/* 0.0s => CACHED [some-api_postgres 4/4] RUN mv /usr/local/bin/maintenance/* /usr/local/bin && rmdir /usr/local/bin/maintenance 0.0s => [some-api_postgres] exporting to image 0.2s => => exporting layers 0.0s => => writing image sha256:ee3ebe58d5d8bac55432307b3994a7dadcf27b8b885e64ffef2ce4307ca02c08 0.0s => => naming to docker.io/library/some-api_postgres 0.0s => CACHED [some-api_api python-build-stage 1/3] RUN apt-get update && apt-get install --no-install-recommends -y build-essential libpq-dev 0.0s => CACHED [some-api_api python-build-stage 2/3] COPY ./requirements . 0.0s => CANCELED [some-api_api python-build-stage 3/3] RUN pip wheel --wheel-dir /usr/src/app/wheels -r requirements-dev.txt
详细错误日志
> [some-api_api python-run-stage 2/8] RUN apt-get update && apt-get install --no-install-recommends -y libpq-dev gettext && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false && rm -rf /var/lib/apt/lists/*: #0 1.157 Get:1 http://deb.debian.org/debian bullseye InRelease [116 kB] #0 1.289 Get:2 http://deb.debian.org/debian-security bullseye-security InRelease [48.4 kB] #0 1.391 Get:3 http://deb.debian.org/debian bullseye-updates InRelease [44.1 kB] #0 2.698 Get:4 http://deb.debian.org/debian bullseye/main amd64 Packages [8184 kB] #0 3.941 Get:5 http://deb.debian.org/debian-security bullseye-security/main amd64 Packages [189 kB] #0 4.029 Get:6 http://deb.debian.org/debian bullseye-updates/main amd64 Packages [6340 B] #0 4.764 Fetched 8588 kB in 4s (2203 kB/s) #0 4.764 Reading package lists... #0 5.296 Reading package lists... #0 5.836 Building dependency tree... #0 5.966 Reading state information... #0 6.109 libpq-dev is already the newest version (13.8-0+deb11u1). #0 6.109 Suggested packages: #0 6.109 gettext-doc autopoint libasprintf-dev libgettextpo-dev #0 6.139 The following NEW packages will be installed: #0 6.141 gettext gettext-base #0 6.369 0 upgraded, 2 newly installed, 0 to remove and 1 not upgraded. #0 6.369 Need to get 1486 kB of archives. #0 6.369 After this operation, 6658 kB of additional disk space will be used. #0 6.369 Err:1 http://deb.debian.org/debian bullseye/main amd64 gettext-base amd64 0.21-4 #0 6.369 403 MediaTypeBlocked [IP: 199.232.18.132 80] #0 6.729 Err:2 http://deb.debian.org/debian bullseye/main amd64 gettext amd64 0.21-4 #0 6.729 403 MediaTypeBlocked [IP: 199.232.18.132 80] #0 6.742 E: Failed to fetch http://deb.debian.org/debian/pool/main/g/gettext/gettext-base_0.21-4_amd64.deb 403 MediaTypeBlocked [IP: 199.232.18.132 80] #0 6.742 E: Failed to fetch http://deb.debian.org/debian/pool/main/g/gettext/gettext_0.21-4_amd64.deb 403 MediaTypeBlocked [IP: 199.232.18.132 80] #0 6.742 E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
最终错误提示
------ failed to solve: executor failed running [/bin/sh -c apt-get update && apt-get install --no-install-recommends -y libpq-dev gettext && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false && rm -rf /var/lib/apt/lists/*]: exit code: 100
错误原因
核心问题是从官方Debian源deb.debian.org下载gettext相关包时返回403 MediaTypeBlocked,属于网络访问层面的限制,可能是:
- 本地网络的代理、防火墙拦截了对该源的请求
- 官方源的访问策略限制了当前IP的访问
- 网络波动导致的临时访问异常
解决办法
更换国内Debian镜像源
在Dockerfile中执行apt-get update之前,添加替换源的命令,以阿里云镜像为例:RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list也可以使用清华镜像:
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list添加--fix-missing参数
修改Dockerfile中的安装命令,增加--fix-missing参数,尝试修复包获取问题:RUN apt-get update && apt-get install --no-install-recommends -y --fix-missing libpq-dev gettext && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false && rm -rf /var/lib/apt/lists/*清理Docker缓存后重新构建
先清理旧的构建缓存,避免缓存的错误源信息影响:docker compose -f local.yml build --no-cache docker compose -f local.yml up -d --remove-orphans检查网络限制
暂时关闭本地的代理工具、防火墙,确认是否是网络规则导致的访问拦截;如果是企业网络,联系管理员确认是否允许访问Debian官方源。
内容的提问来源于stack exchange,提问作者Bedirhan Sahin
相关产品推荐
相关产品推荐

