Optimize Dockerfile to reduce image size and improve build efficiency

- Merge apt-related RUN commands in builder and final stages
- Remove redundant apt-get clean (automatically handled by base image)
- Consolidate cleanup operations within the same layer for effective
  size reduction

Reference:
- https://docs.docker.com/build/building/best-practices/#apt-get
This commit is contained in:
Peter Dave Hello
2024-08-01 23:45:13 +08:00
parent d7b38d9513
commit 99c41c7e34

View File

@@ -4,14 +4,11 @@ FROM ubuntu:24.04 as builder
ENV DEBIAN_FRONTEND=noninteractive ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \ RUN apt-get update && \
apt-get install -y software-properties-common apt-get install -y software-properties-common && \
add-apt-repository ppa:deadsnakes/ppa && \
RUN add-apt-repository ppa:deadsnakes/ppa
# Install necessary packages and Python # Install necessary packages and Python
RUN apt-get update && \ apt-get update && \
apt-get install -y --no-install-recommends gcc wget unzip libc6-dev python3.11 python3.11-distutils python3.11-venv && \ apt-get install -y --no-install-recommends gcc wget unzip libc6-dev python3.11 python3.11-distutils python3.11-venv && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* rm -rf /var/lib/apt/lists/*
# Verify Python installation and setup symlink # Verify Python installation and setup symlink
@@ -50,12 +47,10 @@ RUN pip install --no-cache-dir --upgrade pip && \
FROM ubuntu:24.04 as final FROM ubuntu:24.04 as final
RUN apt-get update && \ RUN apt-get update && \
apt-get install -y software-properties-common apt-get install -y software-properties-common && \
add-apt-repository ppa:deadsnakes/ppa && \
RUN add-apt-repository ppa:deadsnakes/ppa
# Install Python # Install Python
RUN apt-get update && apt-get install -y --no-install-recommends python3.11 && \ apt-get update && apt-get install -y --no-install-recommends python3.11 && \
ln -s /usr/bin/python3.11 /usr/bin/python && \ ln -s /usr/bin/python3.11 /usr/bin/python && \
rm -rf /var/lib/apt/lists/* rm -rf /var/lib/apt/lists/*