Skip to content

Commit dc63ee1

Browse files
committed
compose: increase zram at runtime
To alleviate memory pressure, increase zram to 50% of total memory at runtime. This allows the host to compress inactive pages to reduce contention. This service optionally allows for the maximum zram percentage and compression algorithm to be modified at runtime using variables. Change-type: patch Signed-off-by: Joseph Kogut <joseph@balena.io>
1 parent 4502783 commit dc63ee1

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docker-compose.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,48 @@ services:
278278
CLOUDFLARE_DNS_ZONE: product-os.io
279279
ROBOT_API: https://robot-ws.your-server.de
280280

281+
zram:
282+
image: docker:27.3.1-cli-alpine3.20
283+
restart: no
284+
labels:
285+
io.balena.features.balena-socket: '1'
286+
entrypoint:
287+
- /bin/sh
288+
- -c
289+
environment:
290+
ENABLED: true
291+
zram_pct: 50
292+
zram_algo: lz4
293+
command:
294+
- |
295+
set -e
296+
[[ $ENABLED == 'true' ]] || exit
297+
298+
HOST_CONTAINER_NAME=host-chroot
299+
stop_container() { docker stop "$(docker ps -qf "NAME=$HOST_CONTAINER_NAME")"; }
300+
trap stop_container EXIT
301+
302+
host_cmd() {
303+
if [ -z "$(docker ps -qf "NAME=$HOST_CONTAINER_NAME")" ]; then
304+
docker run \
305+
--interactive \
306+
--tty=true \
307+
--detach \
308+
--rm \
309+
--name="$HOST_CONTAINER_NAME" \
310+
--volume=/:/host \
311+
--privileged \
312+
alpine:3.20.3
313+
fi
314+
315+
HOST_CONTAINER_ID="$(docker ps -qf "NAME=$HOST_CONTAINER_NAME")"
316+
docker exec "$HOST_CONTAINER_ID" chroot /host bash -c "$*"
317+
}
318+
319+
host_cmd "swaps=\$(awk 'NR>1 { print \$1 }' /proc/swaps); if [ -n \"\$swaps\" ]; then swapoff \$swaps; fi"
320+
host_cmd "zrams=\$(find /dev -name \"zram*\"); if [ -n \"\$zrams\" ]; then for d in \$zrams; do zramctl -r \$d; done; fi"
321+
zram_dev=$(host_cmd "memtotal=\$(grep MemTotal /proc/meminfo | awk '{ print \$2 }'); zramctl --find --size \$((memtotal * $zram_pct / 100))K --algorithm $zram_algo")
322+
host_cmd "mkswap $zram_dev && swapon $zram_dev"
281323
# https://github.com/balenablocks/cert-manager
282324
# https://certbot.eff.org/docs/using.html
283325
# https://certbot-dns-cloudflare.readthedocs.io/

0 commit comments

Comments
 (0)