-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-lfs.sh
More file actions
executable file
·281 lines (239 loc) · 8.62 KB
/
docker-lfs.sh
File metadata and controls
executable file
·281 lines (239 loc) · 8.62 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/bin/bash
# Docker build and run script for LFS
set -e
# Configuration
IMAGE_NAME="lfs12"
CONTAINER_NAME="lfs12-container"
DOCKERFILE="Dockerfile"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log() {
echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}"
}
warn() {
echo -e "${YELLOW}[WARNING] $1${NC}"
}
error() {
echo -e "${RED}[ERROR] $1${NC}"
exit 1
}
info() {
echo -e "${BLUE}[INFO] $1${NC}"
}
# Check if Docker is available
check_docker() {
if ! command -v docker &> /dev/null; then
error "Docker is not installed or not in PATH"
fi
if ! docker info >/dev/null 2>&1; then
error "Docker daemon is not running or you don't have permission to access it"
fi
}
# Build Docker image
build_image() {
local dockerfile="$1"
local tag_suffix="$2"
log "Building LFS Docker image using $dockerfile..."
if [ -n "$tag_suffix" ]; then
local full_tag="${IMAGE_NAME}:${tag_suffix}"
else
local full_tag="${IMAGE_NAME}:latest"
fi
docker build -f "$dockerfile" -t "$full_tag" .
info "Image built successfully: $full_tag"
# Show image size
local size=$(docker images "$full_tag" --format "table {{.Size}}" | tail -n 1)
info "Image size: $size"
}
# Run container
run_container() {
local image_tag="$1"
local run_mode="$2"
log "Running LFS container..."
# Stop and remove existing container if it exists
docker stop "$CONTAINER_NAME" 2>/dev/null || true
docker rm "$CONTAINER_NAME" 2>/dev/null || true
case "$run_mode" in
"interactive")
docker run -it --name "$CONTAINER_NAME" \
--hostname lfs-container \
--cap-add SYS_ADMIN \
--tmpfs /tmp:noexec,nosuid,size=1g \
--tmpfs /run:noexec,nosuid,size=100m \
"$image_tag"
;;
"daemon")
docker run -d --name "$CONTAINER_NAME" \
--hostname lfs-container \
--cap-add SYS_ADMIN \
--tmpfs /tmp:noexec,nosuid,size=1g \
--tmpfs /run:noexec,nosuid,size=100m \
"$image_tag" \
/bin/bash -c "while true; do sleep 30; done"
info "Container started in daemon mode. Connect with: docker exec -it $CONTAINER_NAME /bin/bash"
;;
"test")
docker run --rm \
--hostname lfs-container \
"$image_tag" \
/bin/bash -c "echo 'LFS Container Test:' && uname -a && ls -la / && echo 'Test completed successfully'"
;;
*)
error "Unknown run mode: $run_mode"
;;
esac
}
# Clean up images and containers
cleanup() {
log "Cleaning up Docker resources..."
# Stop and remove container
docker stop "$CONTAINER_NAME" 2>/dev/null || true
docker rm "$CONTAINER_NAME" 2>/dev/null || true
# Remove images
docker rmi "${IMAGE_NAME}:latest" 2>/dev/null || true
docker rmi "${IMAGE_NAME}:optimized" 2>/dev/null || true
docker rmi "${IMAGE_NAME}:minimal" 2>/dev/null || true
# Clean up dangling images
# docker image prune -f
info "Cleanup completed"
}
# Show Docker usage stats
show_stats() {
log "Docker usage statistics:"
echo "=== Images ==="
docker images | grep -E "(REPOSITORY|$IMAGE_NAME)" || echo "No LFS images found"
echo -e "\n=== Containers ==="
docker ps -a | grep -E "(CONTAINER|$CONTAINER_NAME)" || echo "No LFS containers found"
echo -e "\n=== System Usage ==="
docker system df
}
# Usage information
usage() {
cat << EOF
LFS Docker Management Script
Usage: $0 [COMMAND] [OPTIONS]
Commands:
build [basic|optimized|minimal] Build Docker image (default: basic)
run [interactive|daemon|test] Run container (default: interactive)
stop Stop running container
clean Clean up all LFS Docker resources
stats Show Docker usage statistics
shell Open shell in running container
Build Options:
basic - Complete LFS system (~970MB)
optimized - Cleaned up system (~950MB)
minimal - Development tools kept, docs removed (~475MB)
Examples:
$0 build minimal # Build minimal image
$0 run # Run smallest available image
$0 run daemon # Run container in background
$0 run test # Run test to verify image
$0 shell # Connect to running container
$0 stats # Show usage statistics
$0 clean # Clean up everything
EOF
}
# Main function
main() {
local command="${1:-build}"
local option="${2:-basic}"
check_docker
case "$command" in
"build")
case "$option" in
"basic")
build_image "Dockerfile" "latest"
;;
"optimized")
build_image "Dockerfile.optimized" "optimized"
;;
"minimal")
build_image "Dockerfile.minimal" "minimal"
;;
*)
error "Unknown build option: $option. Use: basic, optimized, or minimal"
;;
esac
;;
"run")
# Check what images are available and select the best one
local image_tag=""
local available_images=()
# Check for available images in order of preference (smallest first)
if docker images "${IMAGE_NAME}:minimal" --format "{{.Repository}}" | grep -q "${IMAGE_NAME}"; then
available_images+=("${IMAGE_NAME}:minimal")
fi
if docker images "${IMAGE_NAME}:optimized" --format "{{.Repository}}" | grep -q "${IMAGE_NAME}"; then
available_images+=("${IMAGE_NAME}:optimized")
fi
if docker images "${IMAGE_NAME}:latest" --format "{{.Repository}}" | grep -q "${IMAGE_NAME}"; then
available_images+=("${IMAGE_NAME}:latest")
fi
# If no images are available, offer to build one
if [ ${#available_images[@]} -eq 0 ]; then
warn "No LFS Docker images found!"
echo
echo "Available options:"
echo " 1. Build minimal image (~475MB) - Recommended"
echo " 2. Build optimized image (~950MB)"
echo " 3. Build basic image (~970MB)"
echo
read -p "Which image would you like to build? [1/2/3]: " -r choice
case "$choice" in
1|""|"minimal")
build_image "Dockerfile.minimal" "minimal"
image_tag="${IMAGE_NAME}:minimal"
;;
2|"optimized")
build_image "Dockerfile.optimized" "optimized"
image_tag="${IMAGE_NAME}:optimized"
;;
3|"basic")
build_image "Dockerfile" "latest"
image_tag="${IMAGE_NAME}:latest"
;;
*)
error "Invalid choice. Please run the script again and choose 1, 2, or 3."
;;
esac
else
# Use the first (smallest) available image
image_tag="${available_images[0]}"
info "Using image: $image_tag"
fi
case "$option" in
"interactive"|"daemon"|"test")
run_container "$image_tag" "$option"
;;
*)
run_container "$image_tag" "interactive"
;;
esac
;;
"stop")
log "Stopping container..."
docker stop "$CONTAINER_NAME" 2>/dev/null || warn "Container not running"
;;
"shell")
log "Opening shell in container..."
docker exec -it "$CONTAINER_NAME" /bin/bash || error "Container not running or not found"
;;
"clean")
cleanup
;;
"stats")
show_stats
;;
"help"|"-h"|"--help")
usage
;;
*)
error "Unknown command: $command. Use '$0 help' for usage information."
;;
esac
}
main "$@"