Containerized Node server inaccessible with server.listen(port, '127.0.0.1')
<p>I set up a simple Node server in Docker.</p>
<p><strong>Dockerfile</strong></p>
<pre><code>FROM node:latest
RUN apt-get -y update
ADD example.js .
EXPOSE 1337   
CMD node example.js
</code></pre>
<p><strong>example.js</strong></p>
<pre class="lang-js prettyprint-override"><code>var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n'+new Date);
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
</code></pre>
<p>Now build the image</p>
<pre><code>$ docker build -t node_server .
</code></pre>
<p>Now run in container</p>
<pre><code>$ docker run -p 1337:1337 -d node_server  
$ 5909e87302ab7520884060437e19ef543ffafc568419c04630abffe6ff731f70
</code></pre>
<p>Verify the container is running and ports are mapped:</p>
<pre><code>$ docker ps  

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
5909e87302ab        node_server         &quot;/bin/sh -c 'node exa&quot;   7 seconds ago       Up 6 seconds        0.0.0.0:1337-&gt;1337/tcp   grave_goldberg
</code></pre>
<p>Now let's attach to the container and verify the server is running inside:</p>
<pre><code>$ docker exec -it 5909e87302ab7520884060437e19ef543ffafc568419c04630abffe6ff731f70 /bin/bash 
</code></pre>
<p>And in the container command line type:</p>
<pre><code>root@5909e87302ab:/# curl http://localhost:1337
Hello World
Mon Feb 15 2016 16:28:38 GMT+0000 (UTC)
</code></pre>
<p>Looks good right?</p>
<p><strong>The problem</strong></p>
<p>When I execute the same curl command on the host (or navigate with my browser to http://localhost:1337) I see nothing.</p>
<p>Any idea why the port mapping between container and host doesn't work?</p>
<p>Things I already tried:</p>
<ul>
<li>Running with the <code>--expose 1337</code> flag</li>
</ul>