How to Add User in Dockerfile | Docker image

Docker Apr 5, 2022

This one is very simple but can be very useful in some cases.

FROM ubuntu:bionic
ENV DEBIAN_FRONTEND noninteractive
# Get the basic stuff
RUN apt-get update && \
    apt-get -y upgrade && \
    apt-get install -y \
    sudo nano procps net-tools

# Create ubuntu user with sudo privileges
RUN useradd -ms /bin/bash ubuntu && \
    usermod -aG sudo ubuntu
# New added for disable sudo password
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

# Set as default user
USER ubuntu
WORKDIR /home/ubuntu


CMD ["/bin/bash"]
docker build -t give-it-a-tag .



Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.