Docker环境下Bitbake获取树莓派内核失败问题排查
问题:Docker容器内Yocto Nanbield构建树莓派镜像时无法获取Linux内核
在Windows宿主机的Docker容器(基于Debian Bullseye)中使用Yocto Nanbield构建树莓派4镜像,Bitbake可正常启动,但linux-raspberrypi配方的do_fetch任务失败,无法克隆树莓派内核仓库。已尝试修改Git配置、切换Yocto版本(从Kirkstone到Nanbield)均无效。原生Debian机器上构建正常,仅Docker容器内出现该问题,提升网络带宽可临时解决但并非最优方案。
构建脚本
RUN mkdir -p $YOCTO_DIR && cd $YOCTO_DIR && mkdir sources && cd sources && \ git clone https://git.yoctoproject.org/poky -b nanbield && \ git clone https://git.yoctoproject.org/meta-raspberrypi -b nanbield && \ git clone https://git.openembedded.org/meta-openembedded -b nanbield RUN cd $YOCTO_DIR && source sources/poky/oe-init-build-env && \ git config --global http.postBuffer 52428800 && git config --global core.compression 0 && \ git config --global url."https://github.com/".insteadOf git@github.com: && \ git config --global url."https://".insteadOf git:// && \ bitbake-layers add-layer ../sources/meta-raspberrypi \ ../sources/meta-openembedded/meta-oe \ ../sources/meta-openembedded/meta-multimedia \ ../sources/meta-openembedded/meta-networking \ ../sources/meta-openembedded/meta-python && \ echo 'MACHINE ?= "raspberrypi4"' >> conf/local.conf && \ echo 'LICENSE_FLAGS_ACCEPTED = "synaptics-killswitch"' >> conf/local.conf && \ bitbake core-image-base
错误日志
894.5 WARNING: linux-raspberrypi-1_6.1.61+git-r0 do_fetch: Failed to fetch URL git://github.com/raspberrypi/linux.git;name=machine;branch=rpi-6.1.y;protocol=https, attempting MIRRORS if available 904.7 ERROR: linux-raspberrypi-1_6.1.61+git-r0 do_fetch: Fetcher failure: Fetch command export PSEUDO_DISABLED=1; export PATH="/home/notroot/yocto/build/tmp/sysroots-uninative/x86_64-linux/usr/bin:/home/notroot/yocto/sources/poky/scripts:/home/notroot/yocto/build/tmp/work/raspberrypi4-poky-linux-gnueabi/linux-raspberrypi/6.1.61+git/recipe-sysroot-native/usr/bin/arm-poky-linux-gnueabi:/home/notroot/yocto/build/tmp/work/raspberrypi4-poky-linux-gnueabi/linux-raspberrypi/6.1.61+git/recipe-sysroot/usr/bin/crossscripts:/home/notroot/yocto/build/tmp/work/raspberrypi4-poky-linux-gnueabi/linux-raspberrypi/6.1.61+git/recipe-sysroot-native/usr/sbin:/home/notroot/yocto/build/tmp/work/raspberrypi4-poky-linux-gnueabi/linux-raspberrypi/6.1.61+git/recipe-sysroot-native/usr/bin:/home/notroot/yocto/build/tmp/work/raspberrypi4-poky-linux-gnueabi/linux-raspberrypi/6.1.61+git/recipe-sysroot-native/sbin:/home/notroot/yocto/build/tmp/work/raspberrypi4-poky-linux-gnueabi/linux-raspberrypi/6.1.61+git/recipe-sysroot-native/bin:/home/notroot/yocto/sources/poky/bitbake/bin:/home/notroot/yocto/build/tmp/hosttools"; export HOME="/home/notroot"; LANG=C git -c gc.autoDetach=false -c core.pager=cat clone --bare --mirror https://github.com/raspberrypi/linux.git /home/notroot/yocto/build/downloads/git2/github.com.raspberrypi.linux.git --progress failed with exit code 128, no output 904.7 ERROR: linux-raspberrypi-1_6.1.61+git-r0 do_fetch: Bitbake Fetcher Error: FetchError('Unable to fetch URL from any source.', 'git://github.com/raspberrypi/linux.git;name=machine;branch=rpi-6.1.y;protocol=https') 904.8 NOTE: recipe linux-raspberrypi-1_6.1.61+git-r0: task do_fetch: Failed 904.9 ERROR: Task (/home/notroot/yocto/sources/meta-raspberrypi/recipes-kernel/linux/linux-raspberrypi_6.1.bb:do_fetch) failed with exit code '1'
解决方案
1. 调整Docker容器MTU配置
Windows宿主机Docker默认网络的MTU可能与宿主机不一致,导致大文件克隆时丢包。
- 在Dockerfile中添加MTU配置:
RUN echo '{"mtu": 1500}' > /etc/docker/daemon.json && systemctl restart docker - 或启动容器时指定MTU参数:
(注:若宿主机网络MTU不是1500,需替换为对应值)docker run --mtu=1500 [其他启动参数] your-image-name
2. 延长Git网络超时时间
在Git配置中增加对慢速网络的容忍度,避免因延迟导致克隆中断:
RUN git config --global http.lowSpeedLimit 0 && git config --global http.lowSpeedTime 900
将上述命令加入到构建脚本的Git配置RUN指令中。
3. 预克隆内核仓库到镜像
提前手动克隆内核仓库到Yocto的downloads目录,跳过Bitbake自动fetch步骤:
RUN mkdir -p $YOCTO_DIR/build/downloads/git2 && cd $YOCTO_DIR/build/downloads/git2 && \ git clone --bare --mirror https://github.com/raspberrypi/linux.git github.com.raspberrypi.linux.git
将此指令放在bitbake core-image-base之前。
4. 启用Yocto源码镜像源
在conf/local.conf中添加官方镜像源,从镜像站点获取内核源码:
echo 'SOURCE_MIRROR_URL = "http://downloads.yoctoproject.org/mirror/sources/"' >> conf/local.conf
镜像站点缓存了常用源码,能提升克隆成功率。
5. 增加Docker资源分配
Windows宿主机分配给Docker的资源不足可能导致克隆中断:
- 在Docker Desktop设置中,将内存分配提升至至少4GB
- 增加CPU核心数至至少2核
- 确保容器可用磁盘空间不低于50GB
内容的提问来源于stack exchange,提问作者Klickmann
相关产品推荐
相关产品推荐

