-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·97 lines (85 loc) · 3.04 KB
/
build.sh
File metadata and controls
executable file
·97 lines (85 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
set -euo pipefail
# ============================================================================
# Configuration - modify as needed
# ============================================================================
SANDBOX_HOSTNAME="kubernetes-1-33-4"
NETWORK_NAME="docker-archive-bridge"
NETWORK_SUBNET="10.0.2.0/24"
NETWORK_IP_RANGE="10.0.2.16/28"
NETWORK_GATEWAY="10.0.2.1"
BUILDER_NAME="docker-archive-builder"
# ============================================================================
# Functions
# ============================================================================
# Create custom network (for buildx)
# https://docs.docker.com/build/builders/drivers/docker-container/#custom-network
create_network() {
docker network create \
--subnet "${NETWORK_SUBNET}" \
--ip-range "${NETWORK_IP_RANGE}" \
--gateway "${NETWORK_GATEWAY}" \
"${NETWORK_NAME}" 2>/dev/null || true
}
# Create buildx builder
create_builder() {
docker buildx create \
--driver-opt "network=${NETWORK_NAME}" \
--name "${BUILDER_NAME}" \
--buildkitd-flags "--allow-insecure-entitlement security.insecure" \
2>/dev/null || true
}
# Prune buildx cache
prune_cache() {
docker buildx --builder "${BUILDER_NAME}" prune \
--filter type=exec.cachemount -f || true
}
# Prepare kernel modules
prepare_modules() {
local kernel_version
kernel_version=$(uname -r)
mkdir -p modules
cp "/lib/modules/${kernel_version}" "modules/${kernel_version}" -r
}
# Cleanup kernel modules
cleanup_modules() {
rm -rf modules
}
# make sure fs.inotify.max_user_instances >= 512
# https://kind.sigs.k8s.io/docs/user/known-issues/#pod-errors-due-to-too-many-open-files
fix_too_many_open_files() {
local max_user_instances
max_user_instances=$(cat /proc/sys/fs/inotify/max_user_instances 2>/dev/null || echo 0)
if [[ "${max_user_instances}" -lt 512 ]]; then
echo "Warning: fs.inotify.max_user_instances is ${max_user_instances}, should be at least 512"
echo "Consider running: sudo sysctl -w fs.inotify.max_user_instances=512"
fi
}
# Execute Docker buildx build
execute_build() {
local image_tag="${1:?Error: Please provide image tag, e.g.: ./build.sh myimage:tag}"
local progress_opt=""
# Determine progress option based on DEBUG environment variable
if [[ "${DEBUG:-}" == "true" ]] || [[ "${DEBUG:-}" == "1" ]] || [[ "${DEBUG:-}" == "yes" ]]; then
progress_opt="--progress=plain"
fi
docker buildx build \
--builder "${BUILDER_NAME}" \
--build-arg CACHE_BUST=$(date +%s) \
--build-arg BUILDKIT_SANDBOX_HOSTNAME="${SANDBOX_HOSTNAME}" \
${progress_opt} \
--allow security.insecure \
--load \
-t "${image_tag}" \
.
}
# ============================================================================
# Script entry point
# ============================================================================
create_network
create_builder
prune_cache
prepare_modules
fix_too_many_open_files
execute_build "${1}"
cleanup_modules