How to add users to Docker container?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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.
Related reading
- How to allow a Kubernetes Job access to a file on host
- How to allow a pods in Kubernetes access external docker container ip like mysql
- How to analyze disk usage of a Docker container
- How to append an argument to a container command?
- How to analyze logs in a distributed system?
- How to assign a static IP to a pod using Kubernetes on deployment
- How to allow a User only access their own data in Spring Boot / Spring Security?
- How to allow all Network connection types HTTP and HTTPS in Android 9 Pie?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.