Non-root user in Docker

2019-05-30 14:02发布

问题:

I have a basic Dockerfile:

FROM ubuntu:xenial
USER test
ENTRYPOINT ["/bin/bash"]

For this Dockerfile, I want to be able to create a user without a password, and when the Docker container is run, I want that user to be used, instead of root. When I try to run the container, docker run -it test:1, I get this error:

docker: Error response from daemon: linux spec user: unable to find user test: no matching entries in passwd file.

How can I create a user in a Dockerfile and have that user be the default user when the container is run interactively?

回答1:

Add RUN useradd -s /bin/bash user before the USER directive.