mirror of
https://github.com/sipeed/NanoKVM.git
synced 2026-09-11 00:22:56 -05:00
fix: reuse existing container users and groups
This commit is contained in:
@@ -8,9 +8,19 @@ RUN apt update \
|
|||||||
ARG DOCKER_USER=build
|
ARG DOCKER_USER=build
|
||||||
ARG DOCKER_UID=1000
|
ARG DOCKER_UID=1000
|
||||||
ARG DOCKER_GID=1000
|
ARG DOCKER_GID=1000
|
||||||
RUN groupadd --gid "$DOCKER_GID" "$DOCKER_USER" \
|
# Reuse existing accounts for numeric IDs that are already present in the
|
||||||
&& useradd --uid "$DOCKER_UID" --gid "$DOCKER_GID" --create-home --shell /bin/bash "$DOCKER_USER"
|
# base image. All later ownership and user switches use numeric IDs, so a
|
||||||
USER $DOCKER_USER
|
# dedicated build user/group is only needed when the ID does not exist.
|
||||||
|
RUN set -eux; \
|
||||||
|
if ! getent group "$DOCKER_GID" >/dev/null; then \
|
||||||
|
groupadd --gid "$DOCKER_GID" "$DOCKER_USER"; \
|
||||||
|
fi; \
|
||||||
|
if ! getent passwd "$DOCKER_UID" >/dev/null; then \
|
||||||
|
useradd --uid "$DOCKER_UID" --gid "$DOCKER_GID" --no-create-home \
|
||||||
|
--home-dir "/home/$DOCKER_USER" --shell /bin/bash "$DOCKER_USER"; \
|
||||||
|
fi; \
|
||||||
|
install -d -o "$DOCKER_UID" -g "$DOCKER_GID" "/home/$DOCKER_USER"
|
||||||
|
USER $DOCKER_UID:$DOCKER_GID
|
||||||
|
|
||||||
FROM base_apt AS host_tools
|
FROM base_apt AS host_tools
|
||||||
|
|
||||||
@@ -42,6 +52,8 @@ RUN cd /tmp/go_cache \
|
|||||||
|
|
||||||
FROM base_apt AS sdk
|
FROM base_apt AS sdk
|
||||||
|
|
||||||
|
ENV HOME="/home/$DOCKER_USER"
|
||||||
|
|
||||||
RUN cd ~ \
|
RUN cd ~ \
|
||||||
&& git clone https://github.com/Sipeed/MaixCDK \
|
&& git clone https://github.com/Sipeed/MaixCDK \
|
||||||
&& cd MaixCDK \
|
&& cd MaixCDK \
|
||||||
@@ -49,7 +61,7 @@ RUN cd ~ \
|
|||||||
&& . ./bin/activate \
|
&& . ./bin/activate \
|
||||||
&& pip install -U -r requirements.txt
|
&& pip install -U -r requirements.txt
|
||||||
|
|
||||||
COPY --chown=$DOCKER_USER . /home/$DOCKER_USER/NanoKVM
|
COPY --chown=$DOCKER_UID:$DOCKER_GID . /home/$DOCKER_USER/NanoKVM
|
||||||
|
|
||||||
RUN cd ~/MaixCDK \
|
RUN cd ~/MaixCDK \
|
||||||
&& . ./bin/activate \
|
&& . ./bin/activate \
|
||||||
@@ -60,7 +72,7 @@ FROM base_apt
|
|||||||
|
|
||||||
# Install golang
|
# Install golang
|
||||||
COPY --from=golang /usr/local/go /usr/local/go
|
COPY --from=golang /usr/local/go /usr/local/go
|
||||||
COPY --chown=$DOCKER_USER --from=golang /root/go /home/$DOCKER_USER/go
|
COPY --chown=$DOCKER_UID:$DOCKER_GID --from=golang /root/go /home/$DOCKER_USER/go
|
||||||
ENV PATH="$PATH:/usr/local/go/bin"
|
ENV PATH="$PATH:/usr/local/go/bin"
|
||||||
|
|
||||||
# Install host tools
|
# Install host tools
|
||||||
@@ -68,7 +80,7 @@ COPY --from=host_tools /usr/local/host-tools /usr/local/host-tools
|
|||||||
ENV PATH="$PATH:/usr/local/host-tools/gcc/riscv64-linux-musl-x86_64/bin"
|
ENV PATH="$PATH:/usr/local/host-tools/gcc/riscv64-linux-musl-x86_64/bin"
|
||||||
|
|
||||||
# Install SDK
|
# Install SDK
|
||||||
COPY --from=sdk /home/$DOCKER_USER/MaixCDK /home/$DOCKER_USER/MaixCDK
|
COPY --chown=$DOCKER_UID:$DOCKER_GID --from=sdk /home/$DOCKER_USER/MaixCDK /home/$DOCKER_USER/MaixCDK
|
||||||
COPY --chmod=+x docker/entrypoint /entrypoint
|
COPY --chmod=+x docker/entrypoint /entrypoint
|
||||||
|
|
||||||
RUN echo "Verify build tools" \
|
RUN echo "Verify build tools" \
|
||||||
|
|||||||
@@ -12,19 +12,9 @@ fi
|
|||||||
|
|
||||||
if [ "$UID" != 0 ]
|
if [ "$UID" != 0 ]
|
||||||
then
|
then
|
||||||
current_uid="$(id -u "$DOCKER_USER")"
|
set -- gosu "${UID}:${GID}" env HOME="/home/$DOCKER_USER" "${@}"
|
||||||
current_gid="$(id -g "$DOCKER_USER")"
|
else
|
||||||
|
export HOME=/root
|
||||||
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
|
fi
|
||||||
|
|
||||||
exec "$@"
|
exec "$@"
|
||||||
|
|||||||
Reference in New Issue
Block a user