title: How to connect to netcat running in docker container?
body: <p>I'm looking small rest server for sent request and execute scenario.</p>
  
  <p>I've found it here:
  <a href="https://stackoverflow.com/questions/26455434/create-a-minimum-rest-web-server-with-netcat-nc">Create a minimum REST Web Server with netcat nc</a></p>
  
  <p>I'm trying to execute this small rest server.</p>
  
  <p>Below Dockerfile and bash script.</p>
  
  <p>Dockerfile</p>
  
  <pre><code>FROM debian
  ADD  ./rest.sh /rest.sh
  RUN apt-get update \
    &amp;&amp; DEBIAN_FRONTEND=noninteractive \
    &amp;&amp; apt-get install -y net-tools netcat curl \
    &amp;&amp; apt-get clean \
    &amp;&amp; rm -rf /var/lib/apt/lists/* \
    &amp;&amp; chmod +x /rest.sh
  EXPOSE 80
  CMD /rest.sh
  </code></pre>
  
  <p>rest.sh</p>
  
  <pre><code>#!/bin/bash
  /bin/rm -f out
  /usr/bin/mkfifo  out
  trap "rm -f out" EXIT
  while true
  do
    /bin/cat out | /bin/nc -l 80 &gt; &gt;( # parse the netcat output, to build the answer redirected to the pipe "out".
      export REQUEST=
      while read line
      do
        line=$(echo "$line" | tr -d '[\r\n]')
        if /bin/echo "$line" | grep -qE '^GET /' # if line starts with "GET /"
        then
          REQUEST=$(echo "$line" | cut -d ' ' -f2) # extract the request
        elif [ "x$line" = x ] # empty line / end of request
        then
          HTTP_200="HTTP/1.1 200 OK"
          HTTP_LOCATION="Location:"
          HTTP_404="HTTP/1.1 404 Not Found"
          # call a script here
          # Note: REQUEST is exported, so the script can parse it (to answer 200/403/404 status code + content)
          if echo $REQUEST | grep -qE '^/echo/'
          then
              printf "%s\n%s %s\n\n%s\n" "$HTTP_200" "$HTTP_LOCATION" $REQUEST ${REQUEST#"/echo/"} &gt; out
          elif echo $REQUEST | grep -qE '^/date'
          then
              date &gt; out
          elif echo $REQUEST | grep -qE '^/stats'
          then
              vmstat -S M &gt; out
          elif echo $REQUEST | grep -qE '^/net'
          then
              ifconfig &gt; out
          else
              printf "%s\n%s %s\n\n%s\n" "$HTTP_404" "$HTTP_LOCATION" $REQUEST "Resource $REQUEST NOT FOUND!" &gt; out
          fi
        fi
      done
    )
  done
  </code></pre>
  
  <ol>
  <li><p><code>docker build -t ncimange .</code></p></li>
  <li><p><code>docker run -d  -i -p 80:80 --name ncrest ncimange</code></p></li>
  <li><p><code>docker container ls</code></p>
  
  <pre><code>IMAGE               COMMAND                 CREATED             STATUS              PORTS                NAMES
  ncimange            "/bin/sh -c /rest.sh"   8 seconds ago       Up 2 seconds        0.0.0.0:80-&gt;80/tcp   ncrest
  </code></pre></li>
  <li><p><code>docker ps</code></p>
  
  <pre><code>IMAGE               COMMAND                 CREATED             STATUS              PORTS                NAMES
  ncimange            "/bin/sh -c /rest.sh"   41 seconds ago      Up 34 seconds       0.0.0.0:80-&gt;80/tcp   ncrest
  </code></pre></li>
  <li><p><code>docker logs ncrest</code></p>
  
  <p>empty</p></li>
  <li><p>From host:</p>
  
  <pre><code>curl -i http://127.0.0.1:80/date
  
  curl: (56) Recv failure: Connection reset by peer
  </code></pre></li>
  <li><p>From container: </p>
  
  <pre><code>docker exec -it ncrest /bin/bash
  
  netstat -an|grep LISTEN
  tcp        0      0 0.0.0.0:41783           0.0.0.0:*               LISTEN
  
  curl -i http://127.0.0.1:80/date
  curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused
  
  curl -i http://127.0.0.1:41783/date
  curl: (56) Recv failure: Connection reset by peer
  </code></pre></li>
  </ol>
  
  <p>How to connect to netcat rest docker container?</p>