Junior — Middle
How can you change the user inside a Dockerfile during container build?
sobes.tech AI
Answer from AI
In a Dockerfile, to change the user under which subsequent commands are run or the container is executed, the USER instruction is used.
Example:
FROM ubuntu:20.04
# Create a user and group
RUN groupadd -r myuser && useradd -r -g myuser myuser
# Change the user
USER myuser
# Subsequent commands will run as myuser
CMD ["bash"]
If you need to switch to a user with a specific UID/GID, you can specify them directly:
USER 1001:1001
It is important that the user exists in the container, otherwise the command will result in an error.