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

CircleCI Ubuntu环境Pipeline中apt-get未找到及公网IP获取问题排查

问题原因

你混淆了CircleCI主机系统与Job运行的容器系统:

  • CircleCI的主机VM确实是Ubuntu,但你的findpublicip Job运行在指定的Docker容器(hashicorp/terraform:light)内,该容器基于Alpine Linux而非Debian/Ubuntu系。Alpine使用apk作为包管理器,不存在apt-get命令,因此触发报错。
解决方案

提供三种可行的解决方式:

方式1:适配Alpine容器的包管理命令

直接修改run步骤的命令,用Alpine的apk安装curl,且Alpine默认以root用户运行,无需sudo:

version: 2.1
orbs:
  jq: circleci/jq@2.2.0
executors:
  my-executor-terraform:
    docker:
      - image: docker.mirror.hashicorp.services/hashicorp/terraform:light
    working_directory: /home/circleci/workingfile
jobs:
  findpublicip:
    executor: my-executor-terraform
    steps:
      - checkout
      - run:
          name: get ip to add to azure storage account
          command: |
             apk add --no-cache curl
             ip=$(curl ifconfig.me)
             echo $ip
workflows:
  example-workflow:
    jobs:
      - findpublicip

方式2:改用Ubuntu系的Terraform镜像

如果更习惯apt-get命令,可以换用基于Ubuntu的Terraform镜像(比如hashicorp/terraform:latest,该镜像基于Ubuntu),修改Executor的镜像配置即可:

executors:
  my-executor-terraform:
    docker:
      - image: docker.mirror.hashicorp.services/hashicorp/terraform:latest
    working_directory: /home/circleci/workingfile

之后可保留原有的apt-get安装命令(Ubuntu容器默认可能需要sudo,或直接添加-y参数自动确认)。

方式3:直接使用CircleCI内置环境变量(无需安装工具)

CircleCI提供内置环境变量$CIRCLE_WORKFLOW_IP_ADDRESS,可直接获取当前Workflow的公网IP,无需额外安装curl:

- run:
    name: get ip to add to azure storage account
    command: |
       echo $CIRCLE_WORKFLOW_IP_ADDRESS

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 06:16:20