fix: configure Docker containers to run as current user
- Added user mapping (UID:GID) to Docker Compose services to prevent root-owned files - Export current user's UID/GID in build.sh for Docker Compose - Enhanced clean build to handle permission issues gracefully: * Try normal cleanup first * Fallback to container-based cleanup for root-owned files * Ultimate fallback to sudo if needed - Added cleanup for all Docker volumes (build, source, kernel, target caches) Fixes 'Permission denied' errors when cleaning output files created by Docker containers.
This commit is contained in:
20
build.sh
20
build.sh
@@ -114,17 +114,29 @@ cd "$(dirname "$0")/build"
|
|||||||
if [ "$CLEAN_BUILD" = true ]; then
|
if [ "$CLEAN_BUILD" = true ]; then
|
||||||
print_info "Cleaning build artifacts and cache..."
|
print_info "Cleaning build artifacts and cache..."
|
||||||
|
|
||||||
# Remove output artifacts
|
# Remove output artifacts (use sudo if needed for root-owned files)
|
||||||
rm -rf ../output/*
|
if ! rm -rf ../output/* 2>/dev/null; then
|
||||||
|
print_info " Some files owned by root, using container to clean..."
|
||||||
|
export UID=$(id -u)
|
||||||
|
export GID=$(id -g)
|
||||||
|
docker compose run --rm builder sh -c "rm -rf /build/output/*" || {
|
||||||
|
print_warning "Failed to clean output directory, trying with sudo"
|
||||||
|
sudo rm -rf ../output/*
|
||||||
|
}
|
||||||
|
fi
|
||||||
print_info " Removed output artifacts"
|
print_info " Removed output artifacts"
|
||||||
|
|
||||||
# Remove cache directories
|
# Remove cache directories
|
||||||
rm -rf ../cache/*
|
rm -rf ../cache/* 2>/dev/null || {
|
||||||
|
print_info " Using sudo to remove cache directories..."
|
||||||
|
sudo rm -rf ../cache/*
|
||||||
|
}
|
||||||
print_info " Removed cache directories"
|
print_info " Removed cache directories"
|
||||||
|
|
||||||
# Remove Docker volumes
|
# Remove Docker volumes
|
||||||
print_info " Removing Docker cache volumes..."
|
print_info " Removing Docker cache volumes..."
|
||||||
docker volume rm alpine-initramfs_github-cache alpine-initramfs_kernel-cache 2>/dev/null || true
|
docker volume rm alpine-initramfs_github-cache alpine-initramfs_kernel-cache 2>/dev/null || true
|
||||||
|
docker volume rm build_build-cache build_source-cache build_kernel-cache build_target-cache 2>/dev/null || true
|
||||||
print_info " Docker cache volumes removed"
|
print_info " Docker cache volumes removed"
|
||||||
|
|
||||||
print_success "Clean completed successfully"
|
print_success "Clean completed successfully"
|
||||||
@@ -138,6 +150,8 @@ mkdir -p ../output ../cache/github ../cache/packages
|
|||||||
export BUILDMODE
|
export BUILDMODE
|
||||||
export MINIMAL_MODE
|
export MINIMAL_MODE
|
||||||
export TARGETARCH="${TARGETARCH:-amd64}"
|
export TARGETARCH="${TARGETARCH:-amd64}"
|
||||||
|
export UID=$(id -u)
|
||||||
|
export GID=$(id -g)
|
||||||
|
|
||||||
if [ "$DEV_MODE" = true ]; then
|
if [ "$DEV_MODE" = true ]; then
|
||||||
print_info "Starting development container..."
|
print_info "Starting development container..."
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ services:
|
|||||||
image: zero-os-alpine-builder:cached-${BUILDMODE:-debug}
|
image: zero-os-alpine-builder:cached-${BUILDMODE:-debug}
|
||||||
container_name: zero-os-alpine-builder-cached
|
container_name: zero-os-alpine-builder-cached
|
||||||
privileged: true
|
privileged: true
|
||||||
|
user: "${UID:-1000}:${GID:-1000}"
|
||||||
volumes:
|
volumes:
|
||||||
# Mount source configs and scripts (read-only for cache efficiency)
|
# Mount source configs and scripts (read-only for cache efficiency)
|
||||||
- ../configs:/build/configs:ro
|
- ../configs:/build/configs:ro
|
||||||
@@ -45,6 +46,7 @@ services:
|
|||||||
image: zero-os-alpine-builder:legacy
|
image: zero-os-alpine-builder:legacy
|
||||||
container_name: zero-os-alpine-builder-legacy
|
container_name: zero-os-alpine-builder-legacy
|
||||||
privileged: true
|
privileged: true
|
||||||
|
user: "${UID:-1000}:${GID:-1000}"
|
||||||
volumes:
|
volumes:
|
||||||
- ../configs:/build/configs:ro
|
- ../configs:/build/configs:ro
|
||||||
- ../scripts:/build/scripts:ro
|
- ../scripts:/build/scripts:ro
|
||||||
|
|||||||
Reference in New Issue
Block a user