【问题标题】:How to install aws-cli on alpine?如何在高山上安装 aws-cli?
【发布时间】:2020-09-07 03:59:25
【问题描述】:

我正在运行 alpine 的 docker swarm 管理器节点上安装 aws-cli(Linux 0317632a4ad9 4.9.59-moby #1 SMP Thu Mar 1 20:54:00 UTC 2018 x86_64 Linux)。 Alpine 的 aws-cli 包目前列在边缘分支 (1.18.55.r0) 的社区 repo 中。我修改了/etc/apk/repositories 来定位这个仓库。

安装爆炸寻找 py3-urllib3,但我解决了这个问题,最后得到了一个干净的安装,没有错误,如下所示:

  ~ $ sudo apk add aws-cli@edge-comm
    fetch http://dl-cdn.alpinelinux.org/alpine/edge/community/x86_64/APKINDEX.tar.gz
    ERROR: unsatisfiable constraints:
      py3-urllib3-1.25.9-r0:
        masked in: @edge
        satisfies: py3-botocore-1.16.12-r0[py3-urllib3<1.26]

    ~ $ sudo apk add py3-urllib3@edge aws-cli@edge-comm
    (1/23) Installing groff (1.22.3-r1)
    (2/23) Installing py3-six (1.10.0-r6)
    (3/23) Installing py3-dateutil (2.6.0-r1)
    (4/23) Installing libpng (1.6.37-r0)
    (5/23) Installing freetype (2.7.1-r2)
    (6/23) Installing libjpeg-turbo (1.5.3-r2)
    (7/23) Installing lcms2 (2.8-r1)
    (8/23) Installing openjpeg (2.3.0-r2)
    (9/23) Installing tiff (4.0.10-r0)
    (10/23) Installing libwebp (0.6.0-r0)
    (11/23) Installing py3-pillow (4.1.0-r0)
    (12/23) Installing py3-roman (2.0.0-r2)
    (13/23) Installing py3-docutils (0.13.1-r0)
    (14/23) Installing py3-jmespath@edge-comm (0.9.5-r0)
    (15/23) Installing py3-urllib3@edge (1.25.9-r0)
    (16/23) Installing py3-botocore@edge-comm (1.16.12-r0)
    (17/23) Installing py3-s3transfer@edge-comm (0.3.3-r0)
    (18/23) Installing py3-colorama@edge-comm (0.4.3-r0)
    (19/23) Installing yaml (0.1.7-r0)
    (20/23) Installing py3-yaml (3.12-r1)
    (21/23) Installing py3-asn1 (0.2.3-r0)
    (22/23) Installing py3-rsa (3.4.2-r1)
    (23/23) Installing aws-cli@edge-comm (1.18.55-r0)
    Executing busybox-1.26.2-r11.trigger
    OK: 576 MiB in 81 packages

二进制文件是在 /usr/bin/aws 中创建的,但在寻找 awscli 模块时会崩溃:

~ $ aws
Traceback (most recent call last):
  File "/usr/bin/aws", line 19, in <module>
    import awscli.clidriver
ModuleNotFoundError: No module named 'awscli'

谢谢!

【问题讨论】:

  • 你能分享你的基本图像吗?
  • 感谢 Adiii,这是 Docker-for-AWS 映像,但正如您所提到的,我认为我的可能已经过时了。同时,我找到了一个不使用 aws-cli 的解决方案,因此我不再需要这样做。 (stackoverflow.com/questions/61902349/…)
  • Docker-for-AWS 镜像一定有问题。它可以在 docker 容器中运行:docker run --rm -it alpine:3.13 sh -xc 'apk add aws-cli &amp;&amp; aws --version'.

标签: aws-cli alpine


【解决方案1】:

对于任何在谷歌上搜索的人, 我一直在使用图片node:12.16.1-alpine

RUN apk add --no-cache \
        python3 \
        py3-pip \
    && pip3 install --upgrade pip \
    && pip3 install --no-cache-dir \
        awscli \
    && rm -rf /var/cache/apk/*

RUN aws --version   # Just to make sure its installed alright

# Should output aws-cli/1.18.69 etc.

对我来说工作得很好。

注意apk --no-cachepip3 --no-cache-dir 用于通过不保留包缓存来保持 Docker 映像精简。

【讨论】:

  • 注意这里安装的是 CLI v1,而不是新的 v2 CLI
  • Python 3 已弃用,现在您将面临:ERROR: unsatisfiable constraints: python (missing): required by: world[python] 要解决它,请切换到 apk add --no-cache python3 py3-pip 以安装 Python 3 依赖项。
  • *Python 2 (最终)被弃用了。
【解决方案2】:

在我看来你可以使用 alpine 不支持 aws cli V2

RUN apk add --no-cache \
    python3 \
    py3-pip \
&& pip3 install --upgrade pip \
&& pip3 install --no-cache-dir \
    awscli \
&& rm -rf /var/cache/apk/*

【讨论】:

    【解决方案3】:

    根据@Rose (4317383) 的答案进行扩展和更新(并获取 awscli 版本 2):

    由于 musl / non-glibc alpine 实现,常规 pip3 数据包 (pip3 install awscliv2 &amp;&amp; awscliv2 -i) 会出现以下错误:

    Error relocating /root/.awscliv2/binaries/aws: __strcat_chk: symbol not found
    Error relocating /root/.awscliv2/binaries/aws: __snprintf_chk: symbol not found
    Error relocating /root/.awscliv2/binaries/aws: __vfprintf_chk: symbol not found
    Error relocating /root/.awscliv2/binaries/aws: __strdup: symbol not found
    Error relocating /root/.awscliv2/binaries/aws: __stpcpy_chk: symbol not found
    Error relocating /root/.awscliv2/binaries/aws: __vsnprintf_chk: symbol not found
    Error relocating /root/.awscliv2/binaries/aws: __strncpy_chk: symbol not found
    Error relocating /root/.awscliv2/binaries/aws: __strcpy_chk: symbol not found
    Error relocating /root/.awscliv2/binaries/aws: __fprintf_chk: symbol not found
    Error relocating /root/.awscliv2/binaries/aws: __strncat_chk: symbol not found
    

    怀疑 GLIBC 是必要的,但是 if you are willing to invest ~100MB of space or you really need v2 ,下面的 sn-p 会有所帮助

    RUN apk --no-cache add \
        binutils \
        curl \
        && GLIBC_VER=$(curl -s https://api.github.com/repos/sgerrand/alpine-pkg-glibc/releases/latest | grep tag_name | cut -d : -f 2,3 | tr -d \",' ') \
        && curl -sL https://raw.githubusercontent.com/sgerrand/alpine-pkg-glibc/master/sgerrand.rsa.pub -o /etc/apk/keys/sgerrand.rsa.pub \
        && curl -sLO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VER}/glibc-${GLIBC_VER}.apk \
        && curl -sLO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VER}/glibc-bin-${GLIBC_VER}.apk \
        && apk add --no-cache \
        glibc-${GLIBC_VER}.apk \
        glibc-bin-${GLIBC_VER}.apk \
        && curl -sL https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip -o awscliv2.zip \
        && unzip awscliv2.zip \
        && aws/install \
        && rm -rf \
        awscliv2.zip \
        aws \
        /usr/local/aws-cli/v2/*/dist/aws_completer \
        /usr/local/aws-cli/v2/*/dist/awscli/data/ac.index \
        /usr/local/aws-cli/v2/*/dist/awscli/examples \
        && apk --no-cache del \
        binutils \
        curl \
        && rm glibc-${GLIBC_VER}.apk \
        && rm glibc-bin-${GLIBC_VER}.apk \
        && rm -rf /var/cache/apk/*
    
    RUN awsv2 --version   # Just to make sure its installed alright
    

    if your scripts do not break with awscliv2 named as awscli, you might add :

    RUN ln -s $(which awscliv2) /usr/bin/aws
    

    【讨论】:

    • 成功了! aws-cli/2.2.4 Python/3.8.8 Linux/4.19.121-linuxkit exe/x86_64.alpine.3 prompt/off
    【解决方案4】:
    RUN apk update \
        && apk --no-cache add curl \
        && apk --no-cache add unzip \
        && curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" \
        && unzip awscliv2.zip \
        && ./aws/install   
    

    【讨论】:

    • 此解决方案在没有安装 Glibc 的情况下无法在 Alpine 上运行
    • 也可以考虑在安装后删除 awscliv2.zip 以节省一些空间。
    【解决方案5】:

    在 docker hub 上有一个由 Amazon 发布的官方 AWS-CLI 映像,尽管它使用的是 AmazonLinux2 而不是 Alpine。不过我会推荐你​​使用它。

    参考: https://hub.docker.com/r/amazon/aws-cli

    这是 DockerFile: https://github.com/aws/aws-cli/blob/v2/docker/Dockerfile

    FROM amazonlinux:2 as installer
    COPY awscli-exe-linux-x86_64.zip .
    RUN yum update -y \
      && yum install -y unzip \
      && unzip awscli-exe-linux-x86_64.zip \
      # The --bin-dir is specified so that we can copy the
      # entire bin directory from the installer stage into
      # into /usr/local/bin of the final stage without
      # accidentally copying over any other executables that
      # may be present in /usr/local/bin of the installer stage.
      && ./aws/install --bin-dir /aws-cli-bin/
    
    FROM amazonlinux:2
    RUN yum update -y \
      && yum install -y less groff \
      && yum clean all
    COPY --from=installer /usr/local/aws-cli/ /usr/local/aws-cli/
    COPY --from=installer /aws-cli-bin/ /usr/local/bin/
    WORKDIR /aws
    ENTRYPOINT ["/usr/local/bin/aws"]
    

    【讨论】:

    • 虽然这可行,但 AmazonLinux2 的映像比 Alpine 大得多。
    • Alpine 不使用 yum 作为包管理器
    猜你喜欢
    • 1970-01-01
    • 2020-08-03
    • 2019-10-02
    • 2020-03-26
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    相关资源
    最近更新 更多