Skip to content

Instantly share code, notes, and snippets.

@aniongithub
Created June 17, 2025 17:45
Show Gist options
  • Save aniongithub/ed6eda2151340a030b4a60968e56520f to your computer and use it in GitHub Desktop.
Save aniongithub/ed6eda2151340a030b4a60968e56520f to your computer and use it in GitHub Desktop.
Proper Current user pass-through for docker container
FROM debian
RUN apt-get update && apt-get install -y sudo acl
ARG USER_NAME
ARG USER_UID
ARG USER_GID
# Reuse existing UID if present; otherwise create user and group
RUN existing_user=$(getent passwd "${USER_UID}" | cut -d: -f1) && \
if [ -n "$existing_user" ]; then \
if [ "$existing_user" != "${USER_NAME}" ]; then \
usermod -l "${USER_NAME}" "$existing_user" && \
groupmod -n "${USER_NAME}" "$existing_user"; \
fi; \
else \
if ! getent group "${USER_GID}" >/dev/null; then \
groupadd -g "${USER_GID}" "${USER_NAME}"; \
fi && \
useradd -m -u "${USER_UID}" -g "${USER_GID}" -s /bin/bash "${USER_NAME}"; \
fi
# Give them passwordless sudo
RUN echo ${USER_NAME} ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/${USER_NAME} \
&& chmod 0440 /etc/sudoers.d/${USER_NAME}
USER ${USER_NAME}
#!/bin/bash
CONTAINER_TAG="user_passthrough"
# Fail on any error, undefined variable, or failed pipe command
set -euo pipefail
# Build and pass our current user's uid, gid and username
echo "Building container..."
docker build \
--build-arg USER_UID=$(id -u) \
--build-arg USER_GID=$(id -g) \
--build-arg USER_NAME=$(whoami) \
-t ${CONTAINER_TAG} .
echo "Running container..."
docker run -it \
-v $PWD:/workspaces/nucleus-infra-bootstrap \
${CONTAINER_TAG} bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment