在GKE的Ubuntu Docker镜像中安装gcsfuse失败的问题
问题:在Ubuntu 22.10 Docker镜像中安装gcsfuse失败
背景
我正尝试在Google Kubernetes Engine(GKE)环境内的Ubuntu 22.10 Docker镜像中安装gcsfuse,以实现容器内访问Google Cloud Storage。
原Dockerfile
FROM --platform=amd64 ubuntu:22.10 # Use baseimage-docker's init system. CMD ["/sbin/my_init"] # Install. EXPOSE 80 RUN ls -la RUN \ sed -i 's/# \(.*multiverse$\)/\1/g' /etc/apt/sources.list && \ apt-get update && \ apt-get -y upgrade && \ apt-get install -y build-essential && \ apt-get install -y gcc && \ apt-get install -y software-properties-common && \ apt-get install -y cmake && \ apt-get install -y make && \ apt-get install -y clang && \ apt-get install -y mesa-common-dev && \ apt-get install -y git && \ apt-get install -y xorg-dev && \ apt-get install -y nasm && \ apt-get install -y byobu curl git htop man unzip vim wget && \ rm -rf /var/lib/apt/lists/* # RUN apt-get install -y gobjc++ #RUN apt-get install -y gnupg lsb-release wget #RUN export DOCKER_DEFAULT_PLATFORM=linux/amd64 #RUN lsb_release -c -s > /tmp/lsb_release #RUN GCSFUSE_REPO=$(cat /tmp/lsb_release); echo "deb http://packages.cloud.google.com/apt gcsfuse-$GCSFUSE_REPO main" | tee /etc/apt/sources.list.d/gcsfuse.list #RUN wget -O - https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - RUN apt-get install --yes --no-install-recommends ca-certificates curl gpg gpg-agent RUN echo "deb http://packages.cloud.google.com/apt gcsfuse-buster main" | tee /etc/apt/sources.list.d/gcsfuse.list RUN curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - RUN apt-get install -y gcsfuse
首次构建错误
Step 7/23 : RUN echo "deb http://packages.cloud.google.com/apt gcsfuse-buster main" | tee /etc/apt/sources.list.d/gcsfuse.list ---> Running in c027599dc506 deb http://packages.cloud.google.com/apt gcsfuse-buster main Removing intermediate container c027599dc506 ---> 732f7fb73280 Step 8/23 : RUN curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - ---> Running in 47ad78e4351e % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)). 100 2537 100 2537 0 0 48417 0 --:--:-- --:--:-- --:--:-- 48788 OK Removing intermediate container 47ad78e4351e ---> 90819264fc33 Step 9/23 : RUN apt-get install -y gcsfuse ---> Running in 8805b3fcaae8 Reading package lists... Building dependency tree... Reading state information... E: Unable to locate package gcsfuse The command '/bin/sh -c apt-get install -y gcsfuse' returned a non-zero code: 100
尝试过的无效方案
- Github相关Issue
- Stackoverflow相关提问
- 两种第三方安装方法
修改后的Dockerfile及新错误
修改后的安装片段:
RUN export GCSFUSE_REPO=gcsfuse-`lsb_release -c -s` RUN echo "deb http://packages.cloud.google.com/apt $GCSFUSE_REPO main" \ | tee /etc/apt/sources.list.d/gcsfuse.list RUN curl https://packages.cloud.google.com/apt/doc/apt-key.gpg \ | apt-key add - RUN apt-get install -y gcsfuse
新错误:
Step 5/30 : RUN export GCSFUSE_REPO=gcsfuse-`lsb_release -c -s` ---> Running in 94ee52e0b35f Removing intermediate container 94ee52e0b35f ---> fa5a33fd2305 Step 6/30 : RUN echo "deb http://packages.cloud.google.com/apt $GCSFUSE_REPO main" | tee /etc/apt/sources.list.d/gcsfuse.list ---> Running in 785cfe4c4d4c deb http://packages.cloud.google.com/apt main Removing intermediate container 785cfe4c4d4c ---> f4aaed9a03ae Step 7/30 : RUN curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - ---> Running in 8ccfdfab4681 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 100 2537 100 2537 0 0 24924 0 --:--:-- --:--:-- --:--:-- 25118 Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)). OK Removing intermediate container 8ccfdfab4681 ---> 9a856aa4bd1a Step 8/30 : RUN apt-get install -y gcsfuse ---> Running in e4e0445ae72f E: Malformed entry 1 in list file /etc/apt/sources.list.d/gcsfuse.list (Component) E: The list of sources could not be read. The command '/bin/sh -c apt-get install -y gcsfuse' returned a non-zero code: 100
解决方案
问题根源
- Ubuntu 22.10代号为
kinetic,Google的gcsfuse仓库没有对应分支,但可以兼容使用Ubuntu 22.04的jammy分支; - Docker的每个
RUN指令是独立shell环境,单独export的变量无法在后续RUN中生效; apt-key已废弃,需改用密钥文件存入trusted.gpg.d的方式。
有效安装片段
替换原Dockerfile中gcsfuse相关的RUN指令为以下内容:
# 安装依赖并配置gcsfuse源 RUN apt-get update && \ apt-get install --yes --no-install-recommends ca-certificates curl gpg && \ # 将Google密钥转换为标准格式并存入trusted.gpg.d curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | gpg --dearmor -o /usr/share/keyrings/cloud.google.gpg && \ # 添加兼容的jammy分支源 echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt gcsfuse-jammy main" | tee /etc/apt/sources.list.d/gcsfuse.list && \ # 更新源并安装gcsfuse apt-get update && \ apt-get install --yes gcsfuse && \ # 清理缓存减小镜像体积 rm -rf /var/lib/apt/lists/*
说明
- 所有操作合并到一个
RUN中,避免环境变量失效同时减少镜像层数; - 使用
gpg --dearmor替代废弃的apt-key,符合新版apt的密钥管理规范; - 选用Ubuntu 22.04的
jammy分支,完全兼容22.10版本; - 安装后清理apt缓存,优化镜像大小。
内容的提问来源于stack exchange,提问作者Turgut
相关产品推荐
相关产品推荐

