如何在AKS部署的Azure DevOps自托管代理中安装Docker?
如何在AKS上的Azure DevOps自托管代理中配置Docker环境
问题描述
我在AKS集群上部署了Azure DevOps自托管代理,用于运行Pipeline完成Java代码构建、Docker镜像推送及AKS部署。当前Pipeline执行时持续报错:
##[error]Unhandled: Unable to locate executable file: 'docker'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.
我的Pipeline配置如下:
trigger: batch: false branches: include: - main pool: name: test-observability-pool stages: - stage: Build jobs: - job: Build displayName: Build and Publish to Docker Hub steps: - task: JavaToolInstaller@0 inputs: versionSpec: "17" jdkArchitectureOption: x64 jdkSourceOption: LocalDirectory jdkFile: jdk-17_linux-x64_bin.tar.gz jdkDestinationDirectory: /builds/binaries/externals cleanDestinationDirectory: true - task: Gradle@3 displayName: Gradle Build inputs: gradleWrapperFile: gradlew tasks: shadowJar publishJUnitResults: false javaHomeOption: JDKVersion sonarQubeRunAnalysis: false spotBugsAnalysis: false - task: Docker@2 displayName: Docker Login inputs: containerRegistry: Docker Hub Connection command: login - task: Docker@2 displayName: Docker BuildAndPush inputs: containerRegistry: Docker Hub Connection repository: elettrogewi/kubepublisher-otel command: buildAndPush Dockerfile: "**/Dockerfile" tags: latest - stage: Deploy jobs: - job: Deploy displayName: Deploy on AKSObservability01 steps: - task: Kubernetes@1 inputs: connectionType: Kubernetes Service Connection kubernetesServiceEndpoint: aks-observability02 namespace: default command: apply arguments: -f publisher-otel.yaml --force secretType: dockerRegistry containerRegistryType: Azure Container Registry outputFormat: yaml
解决方案
方案1:自定义包含Docker的自托管代理镜像
Azure DevOps官方代理镜像默认不含Docker,需构建自定义镜像集成Docker环境:
- 创建如下
Dockerfile:
FROM mcr.microsoft.com/azure-pipelines/vsts-agent:ubuntu-22.04 # 安装Docker套件 RUN apt-get update && \ apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common && \ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg && \ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null && \ apt-get update && \ apt-get install -y docker-ce docker-ce-cli containerd.io # 将代理用户加入docker组,避免sudo执行命令 RUN usermod -aG docker vstsagent
- 构建镜像并推送到你的容器仓库(如Docker Hub或ACR):
docker build -t <你的仓库名>/azure-pipelines-agent-docker:ubuntu-22.04 . docker push <你的仓库名>/azure-pipelines-agent-docker:ubuntu-22.04
- 修改AKS上的自托管代理部署YAML,将镜像替换为你刚构建的自定义镜像,并添加特权模式(Docker-in-Docker需要):
spec: containers: - name: vstsagent image: <你的仓库名>/azure-pipelines-agent-docker:ubuntu-22.04 securityContext: privileged: true
方案2:在AKS节点VM上直接安装Docker(适用于代理直接运行在节点而非Pod的场景)
如果自托管代理是直接部署在AKS节点的VM中,可通过以下步骤安装Docker:
- 登录到目标AKS节点:
az aks ssh --resource-group <资源组名称> --name <AKS集群名称> --node-name <节点名称>
- 执行Docker安装命令:
sudo apt-get update && \ sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common && \ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg && \ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null && \ sudo apt-get update && \ sudo apt-get install -y docker-ce docker-ce-cli containerd.io
- 将代理用户加入docker组:
sudo usermod -aG docker <代理用户名>
- 重启代理服务使权限生效:
sudo systemctl restart vstsagent.<组织名>.<代理池名>.<代理名>
方案3:使用Kaniko替代Docker(无需安装Docker环境)
如果不想在代理中维护Docker,可使用Kaniko任务直接构建推送镜像:
- 将Pipeline中的
Docker@2任务替换为Kaniko@1任务:
# 移除原有的Docker Login和BuildAndPush任务,替换为以下内容 - task: Kaniko@1 displayName: Build and Push Docker Image inputs: dockerfile: '**/Dockerfile' imageName: 'elettrogewi/kubepublisher-otel:latest' containerRegistry: 'Docker Hub Connection' command: 'buildAndPush'
- 确保Dockerfile路径配置正确,Kaniko会自动使用指定的容器连接完成登录和推送操作。
内容的提问来源于stack exchange,提问作者Mo0oon9898
相关产品推荐
相关产品推荐

