Docker Compose从Git仓库构建时无法找到Dockerfile的问题排查
问题描述
树莓派4(Debian Bullseye系统,Docker版本24.0.7)环境下,尝试通过Docker Compose从包含Dockerfile的私有Git仓库构建容器,docker-compose.yml配置如下:
version: '3' services: creideiki: build: context: ssh://hab@haoke:22/data/Git/py_wunsch.git#main ssh: - default environment: - SSH_AUTH_SOCK=${SSH_AUTH_SOCK} volumes: - ${SSH_AUTH_SOCK}:${SSH_AUTH_SOCK} restart: always ports: - "3031:3031"
执行构建命令:
hab@tsht:/data/creideiki_test $ docker --log-level debug compose --progress=plain build
得到错误输出:
DEBU[0000] using default config store "/home/hab/.docker/buildx" #0 building with "default" instance using docker driver #1 [creideiki internal] load git source ssh://hab@haoke:22/data/Git/py_wunsch.git#main #1 0.252 Warning: Permanently added the ECDSA host key for IP address '192.168.2.8' to the list of known hosts. #1 0.513 aa8cb0051360c52b27cf3781d6f6a07409c0d6ba refs/heads/main #1 DONE 0.7s #1 [creideiki internal] load git source ssh://hab@haoke:22/data/Git/py_wunsch.git#main #1 CACHED DEBU[0000] using default config store "/home/hab/.docker/buildx" DEBU[0000] serving grpc connection DEBU[0000] stopping session span="load buildkit capabilities" DEBU[0000] serving grpc connection DEBU[0002] stopping session failed to solve: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount2916653639/ssh:/hab@haoke:22/data/Git/py_wunsch.git#main/Dockerfile: no such file or directory
已知Git仓库根目录存在Dockerfile,且执行git clone -b main ssh://hab@haoke:22/data/Git/py_wunsch.git可成功克隆,本地克隆后能正常构建镜像,但Docker构建时提示找不到Dockerfile。
问题原因与解决办法
原因分析
从错误路径/var/lib/docker/tmp/buildkit-mount2916653639/ssh:/hab@haoke:22/data/Git/py_wunsch.git#main/Dockerfile可以看出,Docker没有正确将SSH URL识别为Git仓库地址,而是将其当作本地目录路径处理,导致无法定位仓库内的Dockerfile。
解决步骤
- 显式指定Dockerfile路径:在
build配置中添加dockerfile: Dockerfile,强制Docker在克隆后的仓库根目录查找目标文件:build: context: ssh://hab@haoke:22/data/Git/py_wunsch.git#main dockerfile: Dockerfile ssh: - default - 简化Git SSH URL格式:使用Git标准SSH URL格式(省略默认22端口),避免Docker解析错误:
context: hab@haoke:/data/Git/py_wunsch.git#main - 升级Compose配置版本:将
version: '3'改为version: '3.8'或更高版本,确保配置支持Git context的SSH认证特性。 - 强制启用BuildKit:构建时添加环境变量
DOCKER_BUILDKIT=1,确保使用支持Git SSH context的构建引擎:DOCKER_BUILDKIT=1 docker compose --progress=plain build
内容的提问来源于stack exchange,提问作者Helmut
相关产品推荐
相关产品推荐

