Skip to content

Commit 18ae79c

Browse files
upd(container): reduce image size and build time
Cache packages, remove unused packages, and prevent installation of apt recommended packages.
1 parent 59d3394 commit 18ae79c

File tree

6 files changed

+108
-54
lines changed

6 files changed

+108
-54
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,6 @@ infra/tests/*
185185

186186
# Unikraft
187187
.unikraft
188+
189+
# mise-en-place
190+
.mise.toml

images/chromium-headful/Dockerfile

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,35 @@ ENV CGO_ENABLED=0
77

88
COPY server/go.mod ./
99
COPY server/go.sum ./
10-
RUN go mod download
10+
RUN --mount=type=cache,target=/root/.cache/go-build \
11+
--mount=type=cache,target=/go/pkg/mod \
12+
go mod download
1113

1214
COPY server/ .
13-
RUN GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} \
15+
RUN --mount=type=cache,target=/root/.cache/go-build \
16+
--mount=type=cache,target=/go/pkg/mod \
17+
GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} \
1418
go build -ldflags="-s -w" -o /out/kernel-images-api ./cmd/api
1519

1620
# webrtc client
1721
FROM node:22-bullseye-slim AS client
1822
WORKDIR /src
1923
COPY images/chromium-headful/client/package*.json ./
20-
RUN npm install
24+
RUN --mount=type=cache,target=/root/.npm npm install
2125
COPY images/chromium-headful/client/ .
22-
RUN npm run build
26+
RUN --mount=type=cache,target=/root/.npm npm run build
2327

2428
# xorg dependencies
2529
FROM docker.io/ubuntu:22.04 AS xorg-deps
2630
WORKDIR /xorg
2731
ENV DEBIAN_FRONTEND=noninteractive
28-
RUN set -eux; \
32+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=ubuntu2204-aptcache \
33+
--mount=type=cache,target=/var/lib/apt,sharing=locked,id=ubuntu2204-aptlib \
34+
rm -f /etc/apt/apt.conf.d/docker-clean; \
35+
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache; \
36+
set -eux; \
2937
apt-get update; \
30-
apt-get install -y \
38+
apt-get --no-install-recommends -y install \
3139
git gcc pkgconf autoconf automake libtool make xorg-dev xutils-dev \
3240
&& rm -rf /var/lib/apt/lists/*;
3341
COPY images/chromium-headful/xorg-deps/ /xorg/
@@ -54,9 +62,14 @@ FROM docker.io/ubuntu:22.04
5462
ENV DEBIAN_FRONTEND=noninteractive
5563
ENV DEBIAN_PRIORITY=high
5664

57-
RUN apt-get update && \
65+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=ubuntu2204-aptcache \
66+
--mount=type=cache,target=/var/lib/apt,sharing=locked,id=ubuntu2204-aptlib \
67+
rm -f /etc/apt/apt.conf.d/docker-clean; \
68+
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache; \
69+
apt-get update && \
5870
apt-get -y upgrade && \
59-
apt-get -y install \
71+
apt-get --no-install-recommends -y install \
72+
gpg-agent \
6073
# UI Requirements
6174
xvfb \
6275
xterm \
@@ -88,39 +101,44 @@ RUN apt-get update && \
88101
software-properties-common && \
89102
# Userland apps
90103
sudo add-apt-repository ppa:mozillateam/ppa && \
91-
sudo apt-get install -y --no-install-recommends \
92-
chromium-browser \
93-
libreoffice \
104+
sudo apt-get --no-install-recommends -y install \
94105
x11-apps \
95-
xpdf \
96-
gedit \
97-
xpaint \
98106
tint2 \
99-
galculator \
100-
pcmanfm \
101107
wget \
102108
xdg-utils \
103109
libvulkan1 \
104110
fonts-liberation \
105-
unzip && \
106-
apt-get clean
111+
unzip;
107112

108113
# install ffmpeg manually since the version available in apt is from the 4.x branch due to #drama.
109114
# as of writing these static builds will be the latest 7.0.x release.
110-
RUN set -eux; \
115+
RUN --mount=type=cache,target=/tmp/cache/ffmpeg,id=ffmpeg \
116+
set -eux; \
111117
URL="https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz"; \
112-
echo "Downloading FFmpeg static build from $URL"; \
113-
curl -fsSL "$URL" -o /tmp/ffmpeg.tar.xz; \
114-
tar -xJf /tmp/ffmpeg.tar.xz -C /tmp; \
118+
echo "Downloading FFmpeg MD5 checksum"; \
119+
curl -fsSL "${URL}.md5" -o /tmp/cache/ffmpeg/ffmpeg.tar.xz.md5; \
120+
sed -i -e 's/ .*$/ \/tmp\/cache\/ffmpeg\/ffmpeg.tar.xz/' /tmp/cache/ffmpeg/ffmpeg.tar.xz.md5; \
121+
echo "Checking cache for FFmpeg archive and validating MD5 checksum"; \
122+
if md5sum --check /tmp/cache/ffmpeg/ffmpeg.tar.xz.md5; then \
123+
echo "Checksum validated, using cached FFmpeg archive"; \
124+
else \
125+
echo "Downloading FFmpeg static build from $URL"; \
126+
curl -fsSL "$URL" -o /tmp/cache/ffmpeg/ffmpeg.tar.xz; \
127+
echo "Validating MD5 checksum of FFmpeg static build download"; \
128+
md5sum --check /tmp/cache/ffmpeg/ffmpeg.tar.xz.md5; \
129+
fi; \
130+
tar -xJf /tmp/cache/ffmpeg/ffmpeg.tar.xz -C /tmp; \
115131
install -m755 /tmp/ffmpeg-*/ffmpeg /usr/local/bin/ffmpeg; \
116132
install -m755 /tmp/ffmpeg-*/ffprobe /usr/local/bin/ffprobe; \
117133
rm -rf /tmp/ffmpeg*
118134

119135
# runtime
120136
ENV USERNAME=root
121-
RUN set -eux; \
137+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=ubuntu2204-aptcache \
138+
--mount=type=cache,target=/var/lib/apt,sharing=locked,id=ubuntu2204-aptlib \
139+
set -eux; \
122140
apt-get update; \
123-
apt-get install -y --no-install-recommends \
141+
apt-get --no-install-recommends -y install \
124142
wget ca-certificates python2 supervisor xclip xdotool \
125143
pulseaudio dbus-x11 xserver-xorg-video-dummy \
126144
libcairo2 libxcb1 libxrandr2 libxv1 libopus0 libvpx7 \
@@ -131,7 +149,7 @@ RUN set -eux; \
131149
# install libxcvt0 (not available in debian:bullseye)
132150
ARCH=$(dpkg --print-architecture); \
133151
wget http://ftp.de.debian.org/debian/pool/main/libx/libxcvt/libxcvt0_0.1.2-1_${ARCH}.deb; \
134-
apt-get install --no-install-recommends ./libxcvt0_0.1.2-1_${ARCH}.deb; \
152+
apt-get --no-install-recommends install ./libxcvt0_0.1.2-1_${ARCH}.deb; \
135153
rm ./libxcvt0_0.1.2-1_${ARCH}.deb; \
136154
#
137155
# workaround for an X11 problem: http://blog.tigerteufel.de/?p=476
@@ -146,14 +164,15 @@ RUN set -eux; \
146164
/home/$USERNAME/.local/share/xorg; \
147165
chmod 1777 /var/log/neko; \
148166
chown $USERNAME /var/log/neko/ /tmp/runtime-$USERNAME; \
149-
chown -R $USERNAME:$USERNAME /home/$USERNAME; \
150-
# clean up
151-
apt-get clean -y; \
152-
rm -rf /var/lib/apt/lists/* /var/cache/apt/
167+
chown -R $USERNAME:$USERNAME /home/$USERNAME;
153168

154169
# install chromium and sqlite3 for debugging the cookies file
155-
RUN add-apt-repository -y ppa:xtradeb/apps
156-
RUN apt update -y && apt install -y chromium sqlite3
170+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=ubuntu2204-aptcache \
171+
--mount=type=cache,target=/var/lib/apt,sharing=locked,id=ubuntu2204-aptlib \
172+
add-apt-repository -y ppa:xtradeb/apps;
173+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=ubuntu2204-aptcache \
174+
--mount=type=cache,target=/var/lib/apt,sharing=locked,id=ubuntu2204-aptlib \
175+
apt update -y && apt --no-install-recommends -y install chromium sqlite3;
157176

158177
# setup desktop env & app
159178
ENV DISPLAY_NUM=1

images/chromium-headful/client/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ WORKDIR /src
66
#
77
# install dependencies
88
COPY package*.json ./
9-
RUN npm install
9+
RUN --mount=type=cache,target=/root/.npm npm install
1010

1111
#
1212
# build client
1313
COPY . .
14-
RUN npm run build
14+
RUN --mount=type=cache,target=/root/.npm npm run build
1515

1616
#
1717
# artifacts from this stage

images/chromium-headful/xorg-deps/Dockerfile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ FROM $BASE_IMAGE AS xorg-deps
44
WORKDIR /xorg
55

66
ENV DEBIAN_FRONTEND=noninteractive
7-
RUN set -eux; \
7+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=ubuntu2204-aptcache \
8+
--mount=type=cache,target=/var/lib/apt,sharing=locked,id=ubuntu2204-aptlib \
9+
rm -f /etc/apt/apt.conf.d/docker-clean; \
10+
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache; \
11+
set -eux; \
812
apt-get update; \
9-
apt-get install -y \
10-
git gcc pkgconf autoconf automake libtool make xorg-dev xutils-dev \
11-
&& rm -rf /var/lib/apt/lists/*;
13+
apt-get install --no-install-recommends -y \
14+
git gcc pkgconf autoconf automake libtool make xorg-dev xutils-dev;
15+
1216

1317
COPY . /xorg/
1418

images/chromium-headful/xorg-deps/xf86-input-neko/Dockerfile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ FROM debian:bullseye-slim
22

33
ENV DEBIAN_FRONTEND=noninteractive
44

5-
RUN set -eux; \
5+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=debian-bullseye-aptcache \
6+
--mount=type=cache,target=/var/lib/apt,sharing=locked,id=debian-bullseye-aptlib \
7+
rm -f /etc/apt/apt.conf.d/docker-clean; \
8+
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache; \
9+
set -eux; \
610
apt-get update; \
7-
apt-get install -y \
8-
gcc pkgconf autoconf automake libtool make xorg-dev xutils-dev \
9-
&& rm -rf /var/lib/apt/lists/*;
11+
apt-get install --no-install-recommends -y \
12+
gcc pkgconf autoconf automake libtool make xorg-dev xutils-dev;
1013

1114
WORKDIR /app
1215

images/chromium-headless/image/Dockerfile

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@ ENV CGO_ENABLED=0
99
# Go module dependencies first for better layer caching
1010
COPY server/go.mod ./
1111
COPY server/go.sum ./
12-
RUN go mod download
12+
RUN --mount=type=cache,target=/root/.cache/go-build \
13+
--mount=type=cache,target=/go/pkg/mod \
14+
go mod download
1315

1416
# Copy the rest of the server source and build the binary
1517
COPY server/ .
16-
RUN GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} \
18+
RUN --mount=type=cache,target=/root/.cache/go-build \
19+
--mount=type=cache,target=/go/pkg/mod \
20+
GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} \
1721
go build -ldflags="-s -w" -o /out/kernel-images-api ./cmd/api
1822

1923
FROM docker.io/ubuntu:22.04
20-
21-
RUN set -xe; \
24+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=ubuntu2204-aptcache \
25+
--mount=type=cache,target=/var/lib/apt,sharing=locked,id=ubuntu2204-aptlib \
26+
rm -f /etc/apt/apt.conf.d/docker-clean; \
27+
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache; \
28+
set -xe; \
2229
apt-get -yqq update; \
23-
apt-get -yqq install \
30+
apt-get -yqq --no-install-recommends install \
2431
libcups2 \
2532
libnss3 \
2633
libatk1.0-0 \
@@ -37,12 +44,13 @@ RUN set -xe; \
3744
libxrandr2 \
3845
libgbm1 \
3946
libnss3; \
40-
apt-get -yqq install \
47+
apt-get -yqq --no-install-recommends install \
4148
ca-certificates \
4249
curl \
4350
build-essential \
4451
libssl-dev \
4552
git \
53+
gpg-agent \
4654
dbus \
4755
dbus-x11 \
4856
xvfb \
@@ -51,21 +59,38 @@ RUN set -xe; \
5159
supervisor;
5260

5361
# install chromium and sqlite3 for debugging the cookies file
54-
RUN add-apt-repository -y ppa:xtradeb/apps
55-
RUN apt update -y && apt install -y chromium sqlite3
62+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=ubuntu2204-aptcache \
63+
--mount=type=cache,target=/var/lib/apt,sharing=locked,id=ubuntu2204-aptlib \
64+
add-apt-repository -y ppa:xtradeb/apps
65+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=ubuntu2204-aptcache \
66+
--mount=type=cache,target=/var/lib/apt,sharing=locked,id=ubuntu2204-aptlib \
67+
apt-get update -y && apt-get --no-install-recommends install -y chromium sqlite3
5668

5769
# Install FFmpeg (latest static build) for the recording server
58-
RUN set -eux; \
70+
RUN --mount=type=cache,target=/tmp/cache/ffmpeg,id=ffmpeg \
71+
set -eux; \
5972
URL="https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz"; \
60-
echo "Downloading FFmpeg static build from $URL"; \
61-
curl -fsSL "$URL" -o /tmp/ffmpeg.tar.xz; \
62-
tar -xJf /tmp/ffmpeg.tar.xz -C /tmp; \
73+
echo "Downloading FFmpeg MD5 checksum"; \
74+
curl -fsSL "${URL}.md5" -o /tmp/cache/ffmpeg/ffmpeg.tar.xz.md5; \
75+
sed -i -e 's/ .*$/ \/tmp\/cache\/ffmpeg\/ffmpeg.tar.xz/' /tmp/cache/ffmpeg/ffmpeg.tar.xz.md5; \
76+
echo "Checking cache for FFmpeg archive and validating MD5 checksum"; \
77+
if md5sum --check /tmp/cache/ffmpeg/ffmpeg.tar.xz.md5; then \
78+
echo "Checksum validated, using cached FFmpeg archive"; \
79+
else \
80+
echo "Downloading FFmpeg static build from $URL"; \
81+
curl -fsSL "$URL" -o /tmp/cache/ffmpeg/ffmpeg.tar.xz; \
82+
echo "Validating MD5 checksum of FFmpeg static build download"; \
83+
md5sum --check /tmp/cache/ffmpeg/ffmpeg.tar.xz.md5; \
84+
fi; \
85+
tar -xJf /tmp/cache/ffmpeg/ffmpeg.tar.xz -C /tmp; \
6386
install -m755 /tmp/ffmpeg-*/ffmpeg /usr/local/bin/ffmpeg; \
6487
install -m755 /tmp/ffmpeg-*/ffprobe /usr/local/bin/ffprobe; \
6588
rm -rf /tmp/ffmpeg*
6689

6790
# Remove upower to prevent spurious D-Bus activations and logs
68-
RUN apt-get -yqq purge upower || true && rm -rf /var/lib/apt/lists/*
91+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=ubuntu2204-aptcache \
92+
--mount=type=cache,target=/var/lib/apt,sharing=locked,id=ubuntu2204-aptlib \
93+
apt-get -yqq purge upower || true
6994

7095
ENV WITHDOCKER=true
7196

0 commit comments

Comments
 (0)