Azure Pipeline Cache@2 Docker缓存未复用问题求助
Azure Pipeline Docker缓存优化问题排查求助
我正在Azure Pipeline中尝试通过Cache@2任务实现Docker构建的缓存优化,先后使用了两种Pipeline配置:
初始配置
stages: - stage: Build displayName: Build and push stage jobs: - job: Build displayName: Build pool: vmImage: "ubuntu-latest" steps: - script: | echo "##vso[task.setvariable variable=DOCKER_BUILDKIT]1" displayName: Enable Docker BuildKit - task: Docker@2 displayName: Login to container registry inputs: command: login containerRegistry: $(dockerRegistryServiceConnection) - script: | mkdir -p $(Pipeline.Workspace)/.docker/cache echo "Contents of cache directory before build:" ls -la $(Pipeline.Workspace)/.docker/cache displayName: Ensure Docker cache directory exists - task: Bash@3 displayName: Build and Push Docker Image to ACR inputs: targetType: inline script: | echo "Create new builder with docker-container driver..." docker buildx create --name builder --driver docker-container --bootstrap --use echo "Build image using the new builder..." docker buildx build --builder builder \ --cache-from=type=local,src=$(Pipeline.Workspace)/.docker/cache \ --cache-to=type=local,dest=$(Pipeline.Workspace)/.docker/cache,mode=max \ --tag $(containerRegistry)/$(imageRepository):$(releaseTag) \ --tag $(containerRegistry)/$(imageRepository):$(latestTag) \ --file $(dockerfilePath) \ $(buildContext) \ --push - task: Cache@2 displayName: Save Docker layers cache inputs: key: $(cacheKey) path: $(Pipeline.Workspace)/.docker/cache - script: | echo "Contents of cache directory after build:" ls -la $(Pipeline.Workspace)/.docker/cache docker images displayName: List Docker Images and Cache Contents - script: | echo "Cache hit/miss info:" echo "Cache key: $(cacheKey)" displayName: Log cache information
该配置未生效,随后调整为以下版本:
EDIT 3配置
stages: - stage: Build displayName: Build and push stage jobs: - job: Build displayName: Build pool: vmImage: "ubuntu-latest" steps: - task: Docker@2 displayName: Login to container registry inputs: command: login containerRegistry: $(dockerRegistryServiceConnection) - task: Cache@2 displayName: Cache Docker image inputs: key: $(cacheKey) path: $(DOCKER_CACHE_DIR) cacheHitVar: CACHE_RESTORED - script: | echo "Checking for Docker cache directory..." if [ -d "$(DOCKER_CACHE_DIR)" ]; then echo "Cache directory exists. Listing contents..." ls -la $(DOCKER_CACHE_DIR) if [ -f "$(DOCKER_CACHE_DIR)/cache.tar" ]; then echo "Cache file exists. Loading Docker cache..." docker load -i $(DOCKER_CACHE_DIR)/cache.tar || echo "Failed to load cache" else echo "Cache file does not exist." fi else echo "Cache directory does not exist. Skipping cache load." fi displayName: Load Docker cache if directory exists condition: eq(variables.CACHE_RESTORED, 'true') - task: Docker@2 displayName: Build and Push Docker Image to ACR inputs: containerRegistry: $(dockerRegistryServiceConnection) repository: $(imageRepository) command: buildAndPush Dockerfile: $(dockerfilePath) buildContext: $(Build.SourcesDirectory)/services/api tags: | $(releaseTag) $(latestTag) - script: | echo "Saving Docker image to cache..." mkdir -p $(DOCKER_CACHE_DIR) IMAGE_TAG=$(containerRegistry)/$(imageRepository):$(releaseTag) if docker image inspect $IMAGE_TAG > /dev/null 2>&1; then docker save -o $(DOCKER_CACHE_DIR)/cache.tar $IMAGE_TAG echo "Docker image saved to cache." else echo "Image $IMAGE_TAG not found. Skipping cache save." fi displayName: Cache docker layers - script: | echo "Contents of cache directory after build:" ls -la $(DOCKER_CACHE_DIR) docker images echo "Cache hit/miss info:" echo "Cache key: $(cacheKey)" displayName: List Docker Images and Cache Contents
当前问题
EDIT 3配置已实现缓存命中、加载及保存,但Docker构建时仍未复用缓存层,每次都会重新安装所有依赖,构建时间显著增加。
已尝试的调试手段
- 初始使用Cache@2与Docker@2任务组合,未生效;
- 手动创建缓存目录;
- 手动从ACR拉取历史镜像供Docker缓存识别。
求助内容
- 排查当前EDIT 3配置中缺失的部分,解决Docker缓存层复用问题;
- 询问生产环境使用自托管VM代理进行Docker构建是否可行。
后续会补充相关排查结果,供他人参考。
内容的提问来源于stack exchange,提问作者Navemics
相关产品推荐
相关产品推荐

