docker tutorials

How to add new user to the Docker container using Dockerfile

This tutorial explains you how to add new user to the Docker container using Dockerfile.

Add new user to the docker container – Dockerfile

To create admin user from command line you need to run the following linux command.

$ sudo useradd -ms /bin/bash admin

Therefore in Dockerfile, first you need to have the below RUN instruction which creates user “admin“.

RUN useradd -ms /bin/bash admin

Then, you can set the user using the following USER instruction. This instruction sets the user name or UID to use when running the image and for any RUN, CMD and ENTRYPOINT instructions that follow afterwards in the Dockerfile.

USER admin

For example, let’s say you wanted to Dockerize an SSH service using openssh-server and wanted to add new user to the Docker container so that user can do SSH login, the following is the sample Dockerfile to do the same.

FROM ubuntu

MAINTAINER Sneppets Admin <[email protected]>

RUN apt-get update && apt-get install -y openssh-server

RUN mkdir -p /var/run/sshd

RUN useradd -ms /bin/bash admin

ADD sshd_config /etc/ssh/sshd_config

CMD /usr/sbin/sshd -D

USER admin


ENV hello "Hello World"

Now let’s try to build docker image using the following command docker build -t sneppets/sshd-example .

$ docker build -t sneppets/sshd-example .

Sending build context to Docker daemon  6.501MB
Step 1/11 : FROM ubuntu
latest: Pulling from library/ubuntu
692c352adcf2: Pull complete
97058a342707: Pull complete
2821b8e766f4: Pull complete
4e643cc37772: Pull complete
Digest: sha256:55cd38b70425947db71112eb5dddfa3aa3e3ce307754a3df2269069d2278ce47
Status: Downloaded newer image for ubuntu:latest

Removing intermediate container 1ee737251737
 ---> d5a72f019670
Step 4/11 : RUN mkdir -p /var/run/sshd
 ---> Running in 54e70a7391e3
Removing intermediate container 54e70a7391e3
 ---> 98f070fb4c18
Step 5/11 : RUN useradd -ms /bin/bash admin
 ---> Running in d632c3e6299c
Removing intermediate container d632c3e6299c
 ---> 4daaf53e2270
Step 6/11 : ADD sshd_config /etc/ssh/sshd_config
 ---> 3344af18d94d
Step 7/11 : CMD /usr/sbin/sshd -D
 ---> Running in fb199547e682
Removing intermediate container fb199547e682
 ---> 9f3a41e18d27
Step 8/11 : USER admin
 ---> Running in addc73628094
Removing intermediate container addc73628094
 ---> d9cc526a5fda
Step 9/11 : WORKDIR /tmp
 ---> Running in 0bb4c4a4de84
Removing intermediate container 0bb4c4a4de84
 ---> cc74111945b6
Step 10/11 : ENV hello "Hello World"
 ---> Running in b740999b27ef
Removing intermediate container b740999b27ef
 ---> 3013135dbcee
Step 11/11 : EXPOSE 2222
 ---> Running in 3ce325ddea83
Removing intermediate container 3ce325ddea83
 ---> 309cf095e26c
Successfully built 309cf095e26c
Successfully tagged sneppets/sshd-example:latest

Once docker image is built successfully, then you can check docker image that is built using the following command docker images .

[email protected]:~/tempD (sneppets-gcp)$ docker images

REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
sneppets/sshd-example   latest              309cf095e26c        20 minutes ago      221MB
ubuntu                  latest              adafef2e596e        2 days ago          73.9MB

Finally, run the following docker run command with id as shown below.

$ docker run --rm -it sneppets/sshd-example id

uid=1000(admin) gid=1000(admin) groups=1000(admin)

That’s all. This is how you need to add new user to the Docker container. This tells to run its processes with user ID 1000(admin) and group ID 1000(admin).

Hope this helped 🙂

Also See:


Leave a Reply

Notify of