fix: fix container slow caused by uid/gid

This commit is contained in:
watermeko
2026-07-24 07:46:45 +00:00
committed by Guoguo
parent c1986acf65
commit 7eee77a33d
3 changed files with 24 additions and 9 deletions

View File

@@ -1,14 +1,18 @@
# Makefile for NanoKVM Project
# Configuration
IMAGE_NAME := nanokvm-builder
UID := $(shell id -u)
GID := $(shell id -g)
IMAGE_NAME := nanokvm-builder-local-$(UID)-$(GID)
PWD := $(shell pwd)
# Docker run common parameters
DOCKER_RUN_BASE := docker run -e UID=$(UID) -e GID=$(GID) -v $(PWD):/home/build/NanoKVM --rm
# Build the image with the host user's identity so entrypoint does not need
# to recursively rewrite the MaixCDK home directory at container startup.
DOCKER_BUILD_ARGS := --build-arg DOCKER_UID=$(UID) --build-arg DOCKER_GID=$(GID)
# Build commands
GO_BUILD_CMD := cd /home/build/NanoKVM/server && go mod tidy && CGO_ENABLED=1 GOOS=linux GOARCH=riscv64 CC=riscv64-unknown-linux-musl-gcc CGO_CFLAGS="-mcpu=c906fdv -march=rv64imafdcv0p7xthead -mcmodel=medany -mabi=lp64d" go build
SUPPORT_BUILD_CMD := . ./home/build/MaixCDK/bin/activate && cd /home/build/NanoKVM/support/sg2002 && ./build kvm_system && ./build kvm_system add_to_kvmapp
@@ -58,7 +62,7 @@ check-image: check-root
builder-image: check-root
@if ! docker image inspect $(IMAGE_NAME) >/dev/null 2>&1; then \
echo "Building Docker image..."; \
docker build -t $(IMAGE_NAME) -f docker/Dockerfile ./; \
docker build $(DOCKER_BUILD_ARGS) -t $(IMAGE_NAME) -f docker/Dockerfile ./; \
else \
echo "Docker image $(IMAGE_NAME) already exists."; \
fi
@@ -66,7 +70,7 @@ builder-image: check-root
# Force rebuild Docker image
rebuild-image: check-root
@echo "Force rebuilding Docker image..."
@docker build --no-cache -t $(IMAGE_NAME) -f docker/Dockerfile ./
@docker build --no-cache $(DOCKER_BUILD_ARGS) -t $(IMAGE_NAME) -f docker/Dockerfile ./
# Enter interactive shell (equivalent to build.sh with no arguments)
shell: check-root builder-image
@@ -94,4 +98,4 @@ clean:
rm -rf support/sg2002/build; \
echo "Removed support/sg2002/build"; \
fi
@echo "Clean completed."
@echo "Clean completed."

View File

@@ -6,7 +6,10 @@ RUN apt update \
&& apt install wget git cmake build-essential python3 python3-venv python3-pip autoconf automake libtool gosu -y
ARG DOCKER_USER=build
RUN useradd -m "$DOCKER_USER"
ARG DOCKER_UID=1000
ARG DOCKER_GID=1000
RUN groupadd --gid "$DOCKER_GID" "$DOCKER_USER" \
&& useradd --uid "$DOCKER_UID" --gid "$DOCKER_GID" --create-home --shell /bin/bash "$DOCKER_USER"
USER $DOCKER_USER
FROM base_apt AS host_tools
@@ -76,4 +79,4 @@ USER root
RUN chmod 0755 /entrypoint \
&& sed "s/\$DOCKER_USER/$DOCKER_USER/g" -i /entrypoint
ENTRYPOINT ["/entrypoint"]
ENTRYPOINT ["/entrypoint"]

14
docker/entrypoint Normal file → Executable file
View File

@@ -12,11 +12,19 @@ fi
if [ "$UID" != 0 ]
then
usermod -u "$UID" "$DOCKER_USER" 2>/dev/null && {
current_uid="$(id -u "$DOCKER_USER")"
current_gid="$(id -g "$DOCKER_USER")"
if [ "$current_uid" != "$UID" ]; then
usermod -u "$UID" "$DOCKER_USER"
fi
if [ "$current_gid" != "$GID" ]; then
groupmod -g "$GID" "$DOCKER_USER" 2>/dev/null ||
usermod -a -G "$GID" "$DOCKER_USER"
}
fi
set -- gosu "${UID}:${GID}" "${@}"
fi
exec "$@"
exec "$@"