awscli version 2 on alpine linux
Actually with a bit a effort it is possible to run AWS CLI v2 on Alpine:
ARG ALPINE_VERSION=3.15.4
FROM alpine:${ALPINE_VERSION}
ARG GLIBC_VERSION=2.35-r0
ARG AWSCLI_VERSION=2.6.1
# install glibc compatibility for alpine
RUN apk --no-cache add \
binutils \
curl \
&& curl -sL https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub -o /etc/apk/keys/sgerrand.rsa.pub \
&& curl -sLO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VERSION}/glibc-${GLIBC_VERSION}.apk \
&& curl -sLO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VERSION}/glibc-bin-${GLIBC_VERSION}.apk \
&& curl -sLO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VERSION}/glibc-i18n-${GLIBC_VERSION}.apk \
&& apk add --no-cache \
glibc-${GLIBC_VERSION}.apk \
glibc-bin-${GLIBC_VERSION}.apk \
glibc-i18n-${GLIBC_VERSION}.apk \
&& /usr/glibc-compat/bin/localedef -i en_US -f UTF-8 en_US.UTF-8 \
&& ln -sf /usr/glibc-compat/lib/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2 \
&& curl -sL https://awscli.amazonaws.com/awscli-exe-linux-x86_64-${AWSCLI_VERSION}.zip -o awscliv2.zip \
&& unzip awscliv2.zip \
&& aws/install \
&& rm -rf \
awscliv2.zip \
aws \
/usr/local/aws-cli/v2/current/dist/aws_completer \
/usr/local/aws-cli/v2/current/dist/awscli/data/ac.index \
/usr/local/aws-cli/v2/current/dist/awscli/examples \
glibc-*.apk \
&& find /usr/local/aws-cli/v2/current/dist/awscli/botocore/data -name examples-1.json -delete \
&& apk --no-cache del \
binutils \
curl \
&& rm -rf /var/cache/apk/*
The above Dockerfile will install the 'glibc' package for Alpine, so that the AWS CLI will be able to find the required shared libraries. The Dockerfile also removes some stuff we probably don't need, such as auto-complete and examples. If you need some other specific packages you can of course add them to the Dockerfile.
Update 2022-08-01
AWS has improved the build process, so now it's also possible to build from git with MUSL glibc (kudos to @Julian Bueno):
ARG ALPINE_VERSION=3.16
FROM python:3.10.5-alpine${ALPINE_VERSION} as builder
ARG AWS_CLI_VERSION=2.7.20
RUN apk add --no-cache git unzip groff build-base libffi-dev cmake
RUN git clone --single-branch --depth 1 -b ${AWS_CLI_VERSION} https://github.com/aws/aws-cli.git
WORKDIR aws-cli
RUN sed -i'' 's/PyInstaller.*/PyInstaller==5.2/g' requirements-build.txt
RUN python -m venv venv
RUN . venv/bin/activate
RUN scripts/installers/make-exe
RUN unzip -q dist/awscli-exe.zip
RUN aws/install --bin-dir /aws-cli-bin
RUN /aws-cli-bin/aws --version
# reduce image size: remove autocomplete and examples
RUN rm -rf /usr/local/aws-cli/v2/current/dist/aws_completer /usr/local/aws-cli/v2/current/dist/awscli/data/ac.index /usr/local/aws-cli/v2/current/dist/awscli/examples
RUN find /usr/local/aws-cli/v2/current/dist/awscli/botocore/data -name examples-1.json -delete
# build the final image
FROM alpine:${ALPINE_VERSION}
COPY --from=builder /usr/local/aws-cli/ /usr/local/aws-cli/
COPY --from=builder /aws-cli-bin/ /usr/local/bin/
The above reduces the image size even more (from 175MB to 95MB), since we no longer need the glibc-compat hack (however we have to use a specific version of PyInstaller). Hopefully AWS will also distribute the Alpine binaries, so we do not have to build it ourselves.
AWS CLI version 2 is compiled against glibc, the GNU Project's implementation of the C standard library. Most common Linux distributions use glibc, but Alpine Linux is instead based on musl libc.
Because binaries compiled against glibc aren't compatible with musl, AWS CLI version 2 doesn't run on Alpine Linux.
The best way to run AWS CLI version 2 on Alpine Linux would be for Amazon to provide one of the following:
- Binaries compiled against musl
- A source distribution, which can be compiled on Alpine Linux
- An official AWS CLI Docker image based on Alpine Linux