Skip to content

Commit 3acb05b

Browse files
committed
Optimize Dockerfile build
Use a multi-stage build to trim of ~250MB from the exported image.
1 parent d60ab57 commit 3acb05b

1 file changed

Lines changed: 52 additions & 34 deletions

File tree

Dockerfile

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,82 @@
55

66
ARG PYTHON_VERSION="3.10"
77

8-
FROM python:${PYTHON_VERSION}-alpine
9-
10-
ARG CLI_VERSION
11-
12-
# Metadata as defined at http://label-schema.org
13-
ARG BUILD_DATE
8+
#---------------------------------------------------------------------------------------------
149

15-
LABEL maintainer="Microsoft" \
16-
org.label-schema.schema-version="1.0" \
17-
org.label-schema.vendor="Microsoft" \
18-
org.label-schema.name="Azure CLI" \
19-
org.label-schema.version=$CLI_VERSION \
20-
org.label-schema.license="MIT" \
21-
org.label-schema.description="The Azure CLI is used for all Resource Manager deployments in Azure." \
22-
org.label-schema.url="https://docs.microsoft.com/cli/azure/overview" \
23-
org.label-schema.usage="https://docs.microsoft.com/cli/azure/install-az-cli2#docker" \
24-
org.label-schema.build-date=$BUILD_DATE \
25-
org.label-schema.vcs-url="https://github.com/Azure/azure-cli.git" \
26-
org.label-schema.docker.cmd="docker run -v \${HOME}/.azure:/root/.azure -it mcr.microsoft.com/azure-cli:$CLI_VERSION"
10+
FROM python:${PYTHON_VERSION}-alpine AS common
2711

2812
# bash gcc make openssl-dev libffi-dev musl-dev - dependencies required for CLI
2913
# openssh - included for ssh-keygen
3014
# ca-certificates
3115

32-
# curl - required for installing jp
16+
# curl - required for installing jp, and also a useful tool
3317
# jq - we include jq as a useful tool
34-
# pip wheel - required for CLI packaging
35-
# jmespath-terminal - we include jpterm as a useful tool
3618
# libintl and icu-libs - required by azure devops artifact (az extension add --name azure-devops)
3719

3820
# We don't use openssl (3.0) for now. We only install it so that users can use it.
3921
# Once cryptography is bumped to the latest version, openssl1.1-compat should be removed and openssl1.1-compat-dev
4022
# should be replaced by openssl-dev.
41-
RUN apk add --no-cache bash openssh ca-certificates jq curl openssl openssl1.1-compat perl git zip \
42-
&& apk add --no-cache --virtual .build-deps gcc make openssl1.1-compat-dev libffi-dev musl-dev linux-headers \
43-
&& apk add --no-cache libintl icu-libs libc6-compat \
44-
&& apk add --no-cache bash-completion \
23+
24+
RUN apk add --no-cache \
25+
bash openssh ca-certificates jq curl openssl openssl1.1-compat perl git zip \
26+
libintl icu-libs libc6-compat \
27+
bash-completion \
4528
&& update-ca-certificates
4629

30+
#---------------------------------------------------------------------------------------------
31+
32+
FROM common AS tools
33+
4734
ARG JP_VERSION="0.1.3"
4835

4936
RUN curl -L https://github.com/jmespath/jp/releases/download/${JP_VERSION}/jp-linux-amd64 -o /usr/local/bin/jp \
5037
&& chmod +x /usr/local/bin/jp
5138

39+
#---------------------------------------------------------------------------------------------
40+
41+
FROM common AS builder
42+
43+
# bash gcc make openssl-dev libffi-dev musl-dev - dependencies required for CLI
44+
45+
RUN apk add --no-cache --virtual .build-deps gcc make openssl1.1-compat-dev libffi-dev musl-dev linux-headers
46+
5247
WORKDIR azure-cli
5348
COPY . /azure-cli
5449

5550
# 1. Build packages and store in tmp dir
5651
# 2. Install the cli and the other command modules that weren't included
57-
RUN ./scripts/install_full.sh \
58-
&& cat /azure-cli/az.completion > ~/.bashrc \
59-
&& runDeps="$( \
52+
RUN ./scripts/install_full.sh
53+
54+
# Remove CLI source code from the final image and normalize line endings.
55+
RUN dos2unix az.completion /usr/local/bin/az
56+
57+
#---------------------------------------------------------------------------------------------
58+
59+
FROM common
60+
61+
ARG CLI_VERSION
62+
63+
# Metadata as defined at http://label-schema.org
64+
ARG BUILD_DATE
65+
66+
LABEL maintainer="Microsoft" \
67+
org.label-schema.schema-version="1.0" \
68+
org.label-schema.vendor="Microsoft" \
69+
org.label-schema.name="Azure CLI" \
70+
org.label-schema.version=$CLI_VERSION \
71+
org.label-schema.license="MIT" \
72+
org.label-schema.description="The Azure CLI is used for all Resource Manager deployments in Azure." \
73+
org.label-schema.url="https://docs.microsoft.com/cli/azure/overview" \
74+
org.label-schema.usage="https://docs.microsoft.com/cli/azure/install-az-cli2#docker" \
75+
org.label-schema.build-date=$BUILD_DATE \
76+
org.label-schema.vcs-url="https://github.com/Azure/azure-cli.git" \
77+
org.label-schema.docker.cmd="docker run -v \${HOME}/.azure:/root/.azure -it mcr.microsoft.com/azure-cli:$CLI_VERSION"
78+
79+
COPY --from=builder /azure-cli/az.completion /root/.bashrc
80+
COPY --from=builder /usr/local /usr/local
81+
COPY --from=tools /usr/local/bin/jp /usr/local/bin/
82+
83+
RUN runDeps="$( \
6084
scanelf --needed --nobanner --recursive /usr/local \
6185
| awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
6286
| sort -u \
@@ -65,11 +89,5 @@ RUN ./scripts/install_full.sh \
6589
)" \
6690
&& apk add --virtual .rundeps $runDeps
6791

68-
WORKDIR /
69-
70-
# Remove CLI source code from the final image and normalize line endings.
71-
RUN rm -rf ./azure-cli && \
72-
dos2unix /root/.bashrc /usr/local/bin/az
73-
7492
ENV AZ_INSTALLER=DOCKER
7593
CMD bash

0 commit comments

Comments
 (0)