By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Run containers as non-root users to improve security and isolation. Use the USER instruction in the Dockerfile or the --user flag with docker run.
USER
--user
docker run
echo "FROM ubuntu:latest\nUSER myuser" > Dockerfile
docker build -t myimage .
docker run -it --user myuser myimage
docker exec -it mycontainer id -u
docker rm mycontainer
docker rmi myimage
Purpose: Run a container with the user ID 1001.Command: docker run -it --user 1001 myimage Brief explanation of flags: -it runs the container in interactive mode, --user 1001 sets the user ID to 1001.
docker run -it --user 1001 myimage
-it
--user 1001
Purpose: Build an image with a custom user.Command: docker build -t myimage . Brief explanation of flags: docker build builds the image from the Dockerfile, -t myimage tags the image with the name myimage.
docker build
-t myimage
myimage
Purpose: Run a container with the user ID 1001 and group ID 1002.Command: docker run -it --user 1001:1002 myimage Brief explanation of flags: --user 1001:1002 sets both the user ID and group ID to 1001 and 1002, respectively.
docker run -it --user 1001:1002 myimage
--user 1001:1002
Error message or symptom: docker: Error response from daemon: user not found: 1001.Why it happens: The user ID 1001 does not exist in the container's user database.One-line fix: docker run -it --user 1001:1001 myimage (use the same user ID for both user and group).
docker: Error response from daemon: user not found: 1001
docker run -it --user 1001:1001 myimage
Error message or symptom: docker: Error response from daemon: group not found: 1002.Why it happens: The group ID 1002 does not exist in the container's group database.One-line fix: docker run -it --user 1001:1001 myimage (use the same user ID for both user and group).
docker: Error response from daemon: group not found: 1002
Error message or symptom: docker: Error response from daemon: user ID out of range: 65536.Why it happens: The user ID 65536 is out of the range of valid user IDs (0-65535).One-line fix: docker run -it --user 1000:1001 myimage (use a valid user ID).
docker: Error response from daemon: user ID out of range: 65536
docker run -it --user 1000:1001 myimage
Situation: You want to verify that the container is running with the correct user ID.Command: docker exec -it mycontainer id -u Correct response: 1001.
1001
Situation: You want to verify that the container is running with the correct group ID.Command: docker exec -it mycontainer id -g Correct response: 1001.
docker exec -it mycontainer id -g
Situation: You want to verify that the container is running with the correct user and group IDs.Command: docker exec -it mycontainer id -u -g Correct response: 1001 1001.
docker exec -it mycontainer id -u -g
1001 1001
id
docker exec
docker rm
docker rmi
docker version
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.