How to add users to Docker container?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Docker is a powerful platform for building, running, and sharing containerized applications. When working with Docker containers, managing user access can be essential, especially when dealing with secure environments or specific application requirements. This guide will walk you through the steps of adding users to a Docker container, along with detailed examples and explanations.
Understanding Docker Users
By default, Docker containers run using the root user. Running applications as the root user can pose security risks, especially if the container provides a service accessible from the outside world. Therefore, it's often a best practice to create and use non-root users in your Docker containers.
Adding a User to a Docker Container
To add a user to a Docker container, you have two main options:
- At Build Time: Modify the Dockerfile to include user creation commands.
- At Runtime: Use commands to create and switch users in a running container.
Adding Users at Build Time
Creating users directly in the Dockerfile is a robust method because it ensures the user is available every time a container is created from the image. Below is an example:
Explanation:
FROM ubuntu:latest: Starts from the latest Ubuntu image.RUN apt-get update && apt-get install -y sudo: Installs necessary packages, in this case,sudo.RUN groupadd: Creates a groupmyusergroup.useradd: Adds a usermyuserwith a user ID (uid) of 1001, associated withmyusergroup.USER myuser: Sets the default user for subsequentRUN,CMD, andENTRYPOINTdirectives in the Dockerfile.
Adding Users at Runtime
Sometimes, you might need to add users to a container that has already been created and is running:
Switching Users:
You might not always directly change the user in a running container. Instead, you can enter the namespace of a user using the su or sudo command:
Running Commands as a Non-root User
Whether at build time or runtime, it's vital to occasionally execute commands as a non-root user, especially for development or testing. You can do this by prefacing your command with sudo -u <username>:
Table of Key Points
| Task | Command/Instruction |
| Create a group and user in Dockerfile | groupadd and useradd in Dockerfile |
| Changing default user in Dockerfile | USER <username> |
| Start a container and switch users | docker run... and su - <username> |
| Execute command as a specific user | sudo -u <username> <command> |
Best Practices
- Least Privilege: Always aim to run your applications with the least privilege necessary. It would reduce the risk of accidental or malicious operations.
- Dockerfile Optimization: Ensure your Dockerfile is clean and optimized by minimizing layers and applying necessary commands per layer.
- Persistency: User data or application states should be stored in Docker volumes or external databases for persistence when possible.
- Avoid SUDO: Avoid using
sudoas much as possible in your container environment, as it's often a sign that permissions are improperly configured. - Security Updates: Regularly update your base images and dependencies to patch security vulnerabilities.
Conclusion
Adding users to Docker containers is a vital skill for ensuring secure and efficient container operations. By managing users correctly, you can tailor permissions, enhance security, and maintain the integrity of your Docker deployments. With the approaches discussed above, you can implement user management effectively in Docker environments.

