Running containers as non-root in Docker - Time & Space Complexity
We want to understand how the time it takes to run a container changes when we run it as a non-root user.
Specifically, we ask: does switching to a non-root user affect how long container startup takes as we add more steps?
Analyze the time complexity of this Dockerfile snippet.
FROM ubuntu:latest
# Create a non-root user
RUN useradd -m appuser
# Switch to non-root user
USER appuser
CMD ["/bin/bash"]
This code creates a user and runs the container as that user instead of root.
Look for repeated steps that affect startup time.
- Primary operation: Running commands during image build (useradd, switching user)
- How many times: Each command runs once during build, no loops or recursion
Adding more commands or users increases build time linearly.
| Input Size (number of commands) | Approx. Operations |
|---|---|
| 10 | 10 commands run once each |
| 100 | 100 commands run once each |
| 1000 | 1000 commands run once each |
Pattern observation: More commands mean more time, growing roughly in a straight line.
Time Complexity: O(n)
This means the time to build and start the container grows directly with the number of commands you add.
[X] Wrong: "Running as non-root makes container startup much slower regardless of commands."
[OK] Correct: Running as non-root adds almost no extra time itself; the main time depends on how many commands run during build.
Understanding how container build time grows helps you explain trade-offs in security and performance clearly.
What if we added a loop in the Dockerfile to create many users? How would the time complexity change?