Unable to ssh localhost within a running Docker container : <p>I'm building a Docker image for an application which requires to ssh into localhost (i.e ssh user@localhost)</p>

<p>I'm working on a Ubuntu desktop machine and started with a basic ubuntu:16.04 container.
Following is the content of my Dockerfile:</p>

<pre><code>FROM ubuntu:16.04

RUN apt-get update &amp;&amp; apt-get install -y \
        openjdk-8-jdk \
        ssh &amp;&amp; \
        groupadd -r custom_group &amp;&amp; useradd -r -g custom_group -m user1

USER user1

RUN ssh-keygen -b 2048 -t rsa -f ~/.ssh/id_rsa -q -N "" &amp;&amp; \
        cat ~/.ssh/id_rsa.pub &gt;&gt; ~/.ssh/authorized_keys
</code></pre>

<p>Then I build this container using the command:</p>

<pre><code>docker build -t test-container .
</code></pre>

<p>And run it using:</p>

<pre><code>docker run -it test-container
</code></pre>

<p>The container opens with the following prompt and the keys are generated correctly to enable ssh into localhost:</p>

<pre><code>user1@0531c0f71e0a:/$ 
user1@0531c0f71e0a:/$ cd ~/.ssh/
user1@0531c0f71e0a:~/.ssh$ ls
authorized_keys  id_rsa  id_rsa.pub
</code></pre>

<p>Then ssh into localhost and greeted by the error:</p>

<pre><code>user1@0531c0f71e0a:~$ ssh user1@localhost
ssh: connect to host localhost port 22: Cannot assign requested address
</code></pre>

<p>Is there anything I'm doing wrong or any additional network settings that needs to be configured? I just want to ssh into localhost within the running container.</p>
