Bootstrap FreeKB - Docker - Add user to a container using the useradd or adduser command
Docker - Add user to a container using the useradd or adduser command

Updated:   |  Docker articles

This assumes you have installed Docker on Linux and Docker is running.

A Docker image contains the code used to create a Docker container, such as creating a Nginx web server, or a mySQL server, or a home grown app, and the list goes on. In this way, an image is like a template used to create a container. An image is kind of like a virtual machine, but much more light weight, using significantly less storage a memory (containers are usually megabytes in size).

 

It almost always make the most sense to use a Dockerfile to build an image that includes the useradd or adduser (busybox systems) to create a user. With adduser, you'll probably also want to use the chpasswd command to create the users encrypted password.

  • -D = do not assign a password
  • -G = add the user to one or more groups
  • -H = do not assign a home directory (e.g. /home/john.doe)
  • -s = define the users shell
  • -u = give the user a unique user ID
FROM hello-world
RUN adduser -s /bin/bash john.doe
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN echo 'john.doe:itsasecret' | chpasswd

 

Then use the docker build command to create the image, running this command in the same directory as the Dockerfile.

docker build . --tag hello-world:latest

 

The docker exec command can then be used to see the users home directory exists in the container.

~]$ sudo docker exec helloworld ls -l /home
drwxr-sr-x    2 john.doe john.doe         6 Mar  5 11:41 /home/john.doe

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter bfa67f in the box below so that we can be sure you are a human.