Home >Operation and Maintenance >Docker >How to switch to normal user in docker
In Docker, in order to achieve more secure operations, non-root users are usually used to run containers, because the root user has full permissions to perform any operations inside the container. In this article, we will explain how to switch to a non-root user.
1. Create a non-root user
Before using Docker, we need to create a non-root user. To do this, we can create it in the following way:
sudo useradd -ms /bin/bash dockeruser
sudo passwd dockeruser
sudo usermod -aG docker dockeruser
2. Switch to a normal user in the container
docker run -it --name mycontainer ubuntu:latest /bin/bash
su dockeruser
3. Use Dockerfile to switch to an ordinary user
Using Dockerfile can more easily automate the container building process. The following is an example of using a Dockerfile to implement non-root user switching:
FROM ubuntu:latest RUN groupadd -g 1000 dockeruser && \ useradd -r -u 1000 -g dockeruser dockeruser USER dockeruser
docker build -t myimage . docker run -it --name mycontainer myimage /bin/bash
Here we create a user named "dockeruser" through the Dockerfile and set it as the default user when the container starts.
Summary
Using non-root users to run containers in Docker can improve security and protect the host system and sensitive data. In this article, we covered two methods of switching to a non-root user: manually switching in the container and automatically using a Dockerfile.
With the development of container technology, we believe that more security and convenience features will be introduced into Docker. If you also want to learn more about Docker containers, please follow our blog.
The above is the detailed content of How to switch to normal user in docker. For more information, please follow other related articles on the PHP Chinese website!