<p>I have an application that gets deployed from a docker image to a Kubernetes pod. Inside of my docker image I run the following command</p>
<pre><code>FROM openjdk:17.0.1-slim

USER root

WORKDIR /opt/app
ARG JAR_FILE
ARG INFO_APP_BUILD

RUN apt-get update
RUN apt-get install -y sshpass
RUN apt-get install -y openssh-service

COPY /build/libs/*SNAPSHOT.jar /opt/app/app.jar
ENV INFO_APP_BUILD=${INFO_APP_BUILD}

EXPOSE 8080

CMD java -jar /opt/app/app.jar
</code></pre>
<p>When the application gets deployed, out of my control, the user gets set to a non root user.</p>
<p>Now the important part is that when i try to launch an ssh command i get an error message <code>no user exists for uid [random id here]</code></p>
<p>My goal is to configure the docker image to create a user and grant it permission to use the SSH command.</p>

<blockquote>
<p>When the application gets deployed, out of my control, the user gets set to a non root user.</p>
</blockquote>
<p>Inside the container, the user running <code>java -jar /opt/app/app.jar</code> is root, because of <code>USER root</code>.</p>
<p>Outside the container, on the host, a deployed application is usually (almost exclusively) never executed/accessed as <code>root</code>.</p>
<p>But it should still make ssh request from within the container to a server:</p>
<ul>
<li>the <a href="https://ubuntu.com/server/docs/service-openssh" rel="nofollow noreferrer">openssh service</a> is started</li>
<li>the container /root/.ssh has the right public/private key</li>
<li>the <code>~user/.ssh</code> folder, on the target server where the Docker application is running, has the authorized_keys with the public one in it.</li>
</ul>
<p>But if the user does not exist inside the container, you need to create it on <code>docker run</code>, as <a href="https://unix.stackexchange.com/a/613055/7490">in here</a>:</p>
<pre class="lang-sh prettyprint-override"><code>docker run -it --rm --entrypoint sh &quot;$@&quot; \
-c &quot;[ -x /usr/sbin/useradd ] &amp;&amp; useradd -m -u $(id -u) u1 -s /bin/sh || adduser -D -u $(id -u) u1 -s /bin/sh;
exec su - u1&quot;
</code></pre>
