Created
April 16, 2026 15:38
-
-
Save karthiks/7cc06fe699a959bd78a6ed4647848727 to your computer and use it in GitHub Desktop.
Minimal Dockerfile to test renaming existing user in OS base image in Docker.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Objective: Dockerfile to test renaming existing user in OS base image whose USER_UID=1000, in Docker. | |
| # Build: | |
| # docker build -f Dockerfile -t node-custom-user . | |
| # Run n Verify: | |
| # docker run -it --rm node-custom-user whoami | |
| FROM node:20-bookworm-slim | |
| # Build arguments for user configuration | |
| # On Windows with Docker Desktop (especially using WSL2 backend), file permissions are mapped based on UID/GIDs. | |
| # The default first user in WSL2 is typically UID 1000. If you use UID 2000 inside the container, files created by the container will have owner UID 2000, which on the WSL2 host will appear as an unknown user (or a different user), leading to permission issues when you try to edit those files from the host. | |
| # Better approach: Reuse UID 1000 cleanly by renaming the existing node user (which already has UID 1000) to your desired username, instead of deleting it or creating a new user with a different UID. | |
| # ARG USER_UID=1000 | |
| # ARG USER_GID=1000 | |
| ARG USERNAME=ccagent | |
| # ============================================ | |
| # Step 1: Install System Dependencies | |
| # ============================================ | |
| RUN apt-get update && apt-get install -y --no-install-recommends \ | |
| bash \ | |
| wget curl git vim jq ca-certificates \ | |
| sudo \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # ============================================ | |
| # Step 2: Create User with Sudo Access | |
| # Reuse existing 'node' user (UID 1000) by renaming it to $USERNAME | |
| # ============================================ | |
| # 2.1. Rename the group and the user | |
| RUN groupmod -n $USERNAME node \ | |
| && usermod -l $USERNAME -m -d /home/ccagent node \ | |
| # Add user to sudoers with NOPASSWD | |
| && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \ | |
| && chmod 0440 /etc/sudoers.d/$USERNAME | |
| # 2.2. Update environment variables | |
| ENV NODE_USER=$USERNAME | |
| # ============================================ | |
| # Step 3: Setup Workspace | |
| # ============================================ | |
| USER $USERNAME | |
| WORKDIR /workspace | |
| RUN chown $USERNAME:$USERNAME /workspace | |
| # ENTRYPOINT [ "bash"] | |
| # ENTRYPOINT [ "/bin/bash", "-i"] | |
| ENTRYPOINT [ "/bin/bash" ] | |
| CMD ["-l"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment