Docker
User Management
Container Security
DevOps
Software Development

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.

Practice system design

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:

  1. At Build Time: Modify the Dockerfile to include user creation commands.
  2. 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:

dockerfile
1# Start with a base image
2FROM ubuntu:latest
3
4# Add the necessary packages
5RUN apt-get update && apt-get install -y \
6    sudo \
7    && rm -rf /var/lib/apt/lists/*
8
9# Create a group and a user
10RUN groupadd -g 1001 myusergroup \
11    && useradd -r -u 1001 -g myusergroup myuser
12
13# Switch to the new user
14USER myuser
15
16# Continue with other instructions...

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 group myusergroup.
  • useradd: Adds a user myuser with a user ID (uid) of 1001, associated with myusergroup.
  • USER myuser: Sets the default user for subsequent RUN, CMD, and ENTRYPOINT directives 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:

bash
1# Start a container
2docker run -it ubuntu:latest /bin/bash
3
4# Inside the container, add a new user and group
5groupadd -g 1001 myusergroup
6useradd -r -u 1001 -g myusergroup myuser

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:

bash
# Switch to the new user
su - myuser

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>:

bash
# Example of running a command as 'myuser'
sudo -u myuser echo "Running as myuser"

Table of Key Points

TaskCommand/Instruction
Create a group and user in Dockerfilegroupadd and useradd in Dockerfile
Changing default user in DockerfileUSER <username>
Start a container and switch usersdocker run... and su - <username>
Execute command as a specific usersudo -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 sudo as 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.