Skip to content

Commit e4e036f

Browse files
committed
Cleanup after Ubuntu apt and pip installs
For each apt install, we should update, install and clean all in the same RUN. This avoids carrying around apt temporary files in intermediate layers. Also, during pip installs, do not cache temporary files. This change reduced the Ubuntu-22 image size by approximately 0.5GB.
1 parent 6d71a30 commit e4e036f

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

Ubuntu-22/Dockerfile

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,21 @@ ENV TZ=UTC
2626

2727
ENV GCC_MAJOR_VERSION=12
2828

29+
# Install utilities
30+
COPY "apt_cleanup.sh" "/usr/local/bin/"
31+
RUN cd /usr/local/bin && \
32+
mv apt_cleanup.sh apt_cleanup && chmod 755 apt_cleanup
33+
2934
# Preinstall python + dependencies as virtual environment
3035
RUN apt-get update && \
3136
apt-get install --yes --no-install-recommends \
3237
python3 \
33-
virtualenv
38+
virtualenv && \
39+
apt_cleanup
3440
RUN virtualenv /opt/venv
3541
ENV VIRTUAL_ENV /opt/venv
3642
ENV PATH /opt/venv/bin:$PATH
37-
RUN pip install --upgrade pip \
43+
RUN pip install --no-cache-dir --upgrade pip \
3844
-r "https://raw.githubusercontent.com/tianocore/edk2/master/pip-requirements.txt"
3945

4046

@@ -67,8 +73,7 @@ RUN apt-get update && \
6773
g++-${GCC_MAJOR_VERSION}-arm-linux-gnueabi gcc-${GCC_MAJOR_VERSION}-arm-linux-gnueabi \
6874
g++-${GCC_MAJOR_VERSION}-arm-linux-gnueabihf gcc-${GCC_MAJOR_VERSION}-arm-linux-gnueabihf && \
6975
apt-get upgrade -y && \
70-
apt-get clean &&\
71-
rm -rf /var/lib/apt/lists/*
76+
apt_cleanup
7277

7378
RUN \
7479
update-alternatives \
@@ -167,15 +172,17 @@ RUN apt-get update && apt-get install --yes --no-install-recommends \
167172
cd .. && \
168173
rm -rf qemu-build && \
169174
apt remove --yes \
170-
ninja-build
175+
ninja-build && \
176+
apt_cleanup
177+
171178

172179
#####################################################################
173180
# Dev Image
174181
#
175182
FROM test AS dev
176183

177184
# Install convenience tools. Things we like having around, but aren't
178-
# required.
185+
# required.pt
179186
RUN apt-get update && \
180187
apt-get install --yes --no-install-recommends \
181188
bear \
@@ -186,7 +193,7 @@ RUN apt-get update && \
186193
nano \
187194
vim \
188195
&& \
189-
apt-get clean
196+
apt_cleanup
190197

191198
# Setup the entry point
192199
COPY ubuntu22_dev_entrypoint.sh /usr/libexec/entrypoint

Ubuntu-22/apt_cleanup.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
#
3+
# Copyright (c) 2023-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
4+
# SPDX-License-Identifier: BSD-2-Clause-Patent
5+
#
6+
7+
# Utility to cleanup after an apt install or upgrade.
8+
9+
set -e
10+
set -x
11+
12+
apt-get autoremove -y
13+
apt-get clean
14+
rm -rf /var/lib/apt/lists/*

Ubuntu-24/Dockerfile

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ ENV TZ=UTC
2929

3030
ENV GCC_MAJOR_VERSION=13
3131

32+
# Install utilities
33+
COPY "apt_cleanup.sh" "/usr/local/bin/"
34+
RUN cd /usr/local/bin && \
35+
mv apt_cleanup.sh apt_cleanup && chmod 755 apt_cleanup
36+
3237
# Preinstall python + dependencies as virtual environment
3338
RUN apt-get update && \
3439
apt-get install --yes --no-install-recommends \
3540
python3 python3-venv\
36-
virtualenv
41+
virtualenv && \
42+
apt_cleanup
3743
RUN virtualenv /opt/venv
3844
ENV VIRTUAL_ENV /opt/venv
3945
ENV PATH /opt/venv/bin:$PATH
@@ -70,8 +76,7 @@ RUN apt-get update && \
7076
g++-${GCC_MAJOR_VERSION}-arm-linux-gnueabi gcc-${GCC_MAJOR_VERSION}-arm-linux-gnueabi \
7177
g++-${GCC_MAJOR_VERSION}-arm-linux-gnueabihf gcc-${GCC_MAJOR_VERSION}-arm-linux-gnueabihf && \
7278
apt-get upgrade -y && \
73-
apt-get clean &&\
74-
rm -rf /var/lib/apt/lists/*
79+
apt_cleanup
7580

7681
RUN \
7782
update-alternatives \
@@ -171,7 +176,8 @@ RUN apt-get update && apt-get install --yes --no-install-recommends \
171176
cd .. && \
172177
rm -rf qemu-build && \
173178
apt remove --yes \
174-
ninja-build
179+
ninja-build && \
180+
apt_cleanup
175181

176182
#####################################################################
177183
# Dev Image
@@ -191,7 +197,7 @@ RUN apt-get update && \
191197
vim \
192198
cmake \
193199
&& \
194-
apt-get clean
200+
apt_cleanup
195201

196202
# Setup the entry point
197203
COPY ubuntu24_dev_entrypoint.sh /usr/libexec/entrypoint

Ubuntu-24/apt_cleanup.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
#
3+
# Copyright (c) 2023-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
4+
# SPDX-License-Identifier: BSD-2-Clause-Patent
5+
#
6+
7+
# Utility to cleanup after an apt install or upgrade.
8+
9+
set -e
10+
set -x
11+
12+
apt-get autoremove -y
13+
apt-get clean
14+
rm -rf /var/lib/apt/lists/*

0 commit comments

Comments
 (0)