Optimized Dockerfile to reduce uncompressed image size (#276)

* Optimized Dockerfile to reduce uncompressed image size

Combining apk del commands in the same layer as their add commands shaves off ~270MB when uncompressed.

* Skip libffi-dev rust cargo installation on x86_64
This commit is contained in:
Jaryl Chng 2023-08-30 03:12:32 +08:00 committed by GitHub
parent f1a0de2090
commit 4cc2dd4b54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,37 +7,36 @@ WORKDIR /usr/src/shynet
ARG GF_UID="500" ARG GF_UID="500"
ARG GF_GID="500" ARG GF_GID="500"
RUN apk update && \ RUN apk update && \
apk add gettext curl bash npm libffi-dev rust cargo apk add --no-cache gettext bash npm postgresql-libs && \
test "$(arch)" != "x86_64" && apk add libffi-dev rust cargo || echo "amd64 build, skipping Rust installation"
# libffi-dev and rust are used for the cryptography package, # libffi-dev and rust are used for the cryptography package,
# which we indirectly rely on. Necessary for aarch64 support. # which we indirectly rely on. Necessary for aarch64 support.
# Collect GeoIP Database # Collect GeoIP Database
RUN curl -m 180 "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key=kKG1ebhL3iWVd0iv&suffix=tar.gz" | tar -xvz -C /tmp && \ RUN apk add --no-cache curl && \
curl -m 180 "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key=kKG1ebhL3iWVd0iv&suffix=tar.gz" | tar -xvz -C /tmp && \
curl -m 180 "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=kKG1ebhL3iWVd0iv&suffix=tar.gz" | tar -xvz -C /tmp && \ curl -m 180 "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=kKG1ebhL3iWVd0iv&suffix=tar.gz" | tar -xvz -C /tmp && \
mv /tmp/GeoLite2*/*.mmdb /etc && \ mv /tmp/GeoLite2*/*.mmdb /etc && \
apk del curl apk --purge del curl
# Move dependency files # Move dependency files
COPY poetry.lock pyproject.toml ./ COPY poetry.lock pyproject.toml ./
COPY package.json package-lock.json ../ COPY package.json package-lock.json ../
# Django expects node_modules to be in its parent directory. # Django expects node_modules to be in its parent directory.
# Install more dependencies # Install more dependencies and cleanup build dependencies afterwards
RUN apk add --no-cache postgresql-libs && \ RUN apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev libressl-dev libffi-dev && \
apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev libressl-dev libffi-dev && \
npm i -P --prefix .. && \ npm i -P --prefix .. && \
pip install poetry==1.2.2 pip install poetry==1.2.2 && \
poetry config virtualenvs.create false && \
# Install Python dependencies
RUN poetry config virtualenvs.create false && \
poetry run pip install "Cython<3.0" "pyyaml==5.4.1" --no-build-isolation && \ poetry run pip install "Cython<3.0" "pyyaml==5.4.1" --no-build-isolation && \
poetry install --no-dev --no-interaction --no-ansi poetry install --no-dev --no-interaction --no-ansi && \
apk --purge del .build-deps && \
# Cleanup dependencies & setup user group
RUN apk --purge del .build-deps && \
rm -rf /var/lib/apt/lists/* && \ rm -rf /var/lib/apt/lists/* && \
rm /var/cache/apk/* && \ rm /var/cache/apk/*
addgroup --system -g $GF_GID appgroup && \
# Setup user group
RUN addgroup --system -g $GF_GID appgroup && \
adduser appuser --system --uid $GF_UID -G appgroup && \ adduser appuser --system --uid $GF_UID -G appgroup && \
mkdir -p /var/local/shynet/db/ && \ mkdir -p /var/local/shynet/db/ && \
chown -R appuser:appgroup /var/local/shynet chown -R appuser:appgroup /var/local/shynet