Matteo
Matteo

Reputation: 2634

Docker CMD cannot add new user, permission denied

I have a docker image that at the end launch this script:


# Start the server
CMD [ "bash", "/app/start.sh"]

And then my script start.sh


# Create ssh user
useradd -m -d /home/${SSH_MASTER_USER} -G ssh ${SSH_MASTER_USER} -s /bin/bash
echo "${SSH_MASTER_USER}:${SSH_MASTER_PASS}" | chpasswd
echo 'PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin"' >> /home/${SSH_MASTER_USER}/.profile

And I keep getting this error:


useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.

If run with sudo it says:

sudo: no tty present and no askpass program specified


Tried to fix it like this:


sudo -S useradd -m -d /home/${SSH_MASTER_USER} -G ssh ${SSH_MASTER_USER} -s /bin/bash

But now is asking me for a password, but there is not password...

Upvotes: 0

Views: 2606

Answers (2)

Matteo
Matteo

Reputation: 2634

I have solved by adding my existing user to the sudo group:


RUN echo "myUser ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/docker && chmod 0440 /etc/sudoers.d/docker


then the:


sudo -S useradd -m -d /home/${SSH_MASTER_USER} -G ssh ${SSH_MASTER_USER} -s /bin/bash

worked without password

Upvotes: 0

user2932688
user2932688

Reputation: 1704

my base image is using root. so i am adding appuser and installing sudo like this:

RUN adduser --disabled-password --gecos "" appuser
RUN apt-get update
RUN apt-get install -y sudo
RUN echo "appuser ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/appuser && chmod 0440 /etc/sudoers.d/appuser

Later on when you ready to use new user for all commands:

USER appuser:appuser

that is it, now appuser which is going to be user by default can use sudo without password.

Upvotes: 1

Related Questions