From 7eee77a33d2c3d31a0eeac7c305c8427a0282056 Mon Sep 17 00:00:00 2001 From: watermeko Date: Fri, 24 Jul 2026 07:46:45 +0000 Subject: [PATCH] fix: fix container slow caused by uid/gid --- Makefile | 12 ++++++++---- docker/Dockerfile | 7 +++++-- docker/entrypoint | 14 +++++++++++--- 3 files changed, 24 insertions(+), 9 deletions(-) mode change 100644 => 100755 docker/entrypoint diff --git a/Makefile b/Makefile index 591d64b..f1620ba 100644 --- a/Makefile +++ b/Makefile @@ -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." \ No newline at end of file + @echo "Clean completed." diff --git a/docker/Dockerfile b/docker/Dockerfile index 2519e56..507d88e 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -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"] \ No newline at end of file +ENTRYPOINT ["/entrypoint"] diff --git a/docker/entrypoint b/docker/entrypoint old mode 100644 new mode 100755 index a0e48aa..c55c046 --- a/docker/entrypoint +++ b/docker/entrypoint @@ -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 "$@" \ No newline at end of file +exec "$@"