You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CircleCI执行Docker命令报错‘docker: command not found’该如何修复?

CircleCI构建Maven项目时Docker命令找不到的修复方案

问题背景

我有一个此前使用Bitbucket Pipeline构建的Maven项目,现计划迁移至CircleCI。使用的CircleCI配置文件如下:

version: 2.1
orbs:
  docker: circleci/docker@2.2.0 # Replace x.y.z with the latest version of the Docker orb

jobs:
  build:
    docker:
      - image: maven:3.9.0-eclipse-temurin-17-focal

    steps:
      - checkout

      # You can customize this section according to your Maven build steps
      - run:
          name: Build
          command: mvn clean install -s settings.xml

      # No need to manually build the Docker image, use the existing Dockerfile
      - setup_remote_docker:
          version: 20.10.7
        # Use the following command to build and tag the Docker image
      - run:
          name: Build Docker Image
          command: |
            docker info
            docker build -t nexus:latest .
            docker tag nexus:latest kuldeepiitg/nexus:latest
            docker login -u $DOCKER_USERNAME -p $DOCKER_TOKEN
            docker push kuldeepiitg/nexus:latest

workflows:
  version: 2
  build-and-deploy:
    jobs:
      - build

执行时出现错误:

#!/bin/bash -eo pipefail
docker info
docker build -t nexus:latest .
docker tag nexus:latest kuldeepiitg/nexus:latest
docker login -u $DOCKER_USERNAME -p $DOCKER_TOKEN
docker push kuldeepiitg/nexus:latest

/bin/bash: docker: command not found

Exited with code exit status 127

错误原因

当前使用的maven:3.9.0-eclipse-temurin-17-focal镜像未预装Docker CLI,且setup_remote_docker仅启动远程Docker守护进程,不会自动在当前容器中配置Docker客户端环境,导致无法识别docker命令。

修复方案

方案一:使用Docker Orb封装命令(推荐)

既然已引入circleci/docker orb,直接使用其提供的标准化命令完成镜像构建与推送,无需手动配置Docker环境:

version: 2.1
orbs:
  docker: circleci/docker@2.2.0

jobs:
  build:
    docker:
      - image: maven:3.9.0-eclipse-temurin-17-focal

    steps:
      - checkout
      - run:
          name: 构建Maven项目
          command: mvn clean install -s settings.xml
      # 检查Docker环境
      - docker/check
      # 构建镜像
      - docker/build:
          image: kuldeepiitg/nexus
          tag: latest
      # 推送镜像到Docker Hub
      - docker/push:
          image: kuldeepiitg/nexus
          tag: latest
          registry: docker.io
          username: $DOCKER_USERNAME
          password: $DOCKER_TOKEN

workflows:
  version: 2
  build-and-deploy:
    jobs:
      - build

方案二:手动安装Docker CLI并配置远程连接

若需保留手动执行Docker命令的逻辑,需先在Maven镜像中安装Docker CLI,再配置环境变量连接到远程Docker守护进程:

version: 2.1
orbs:
  docker: circleci/docker@2.2.0

jobs:
  build:
    docker:
      - image: maven:3.9.0-eclipse-temurin-17-focal

    steps:
      - checkout
      - run:
          name: 构建Maven项目
          command: mvn clean install -s settings.xml
      - setup_remote_docker:
          version: 20.10.7
      - run:
          name: 安装Docker CLI
          command: |
            apt-get update && apt-get install -y docker.io
      - run:
          name: 构建并推送Docker镜像
          command: |
            # 配置Docker连接到远程守护进程
            export DOCKER_HOST=tcp://localhost:2376
            export DOCKER_TLS_VERIFY=1
            export DOCKER_CERT_PATH=/home/circleci/.docker
            docker info
            docker build -t nexus:latest .
            docker tag nexus:latest kuldeepiitg/nexus:latest
            docker login -u $DOCKER_USERNAME -p $DOCKER_TOKEN
            docker push kuldeepiitg/nexus:latest

workflows:
  version: 2
  build-and-deploy:
    jobs:
      - build

注意事项

  • 方案一更符合CircleCI的最佳实践,减少手动配置环节,降低出错概率。
  • 需确保CircleCI项目已在环境变量中配置DOCKER_USERNAME和DOCKER_TOKEN,否则镜像推送会失败。

内容的提问来源于stack exchange,提问作者Kuldeep Yadav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 17:55:20