How to connect to netcat running in docker container?

回眸只為那壹抹淺笑 提交于 2021-01-29 15:24:17

问题


I'm looking small rest server for sent request and execute scenario.

I've found it here: Create a minimum REST Web Server with netcat nc

I'm trying to execute this small rest server.

Below Dockerfile and bash script.

Dockerfile

FROM debian
ADD  ./rest.sh /rest.sh
RUN apt-get update \
  && DEBIAN_FRONTEND=noninteractive \
  && apt-get install -y net-tools netcat curl \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/* \
  && chmod +x /rest.sh
EXPOSE 80
CMD /rest.sh

rest.sh

#!/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 > >( # 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/"} > out
        elif echo $REQUEST | grep -qE '^/date'
        then
            date > out
        elif echo $REQUEST | grep -qE '^/stats'
        then
            vmstat -S M > out
        elif echo $REQUEST | grep -qE '^/net'
        then
            ifconfig > out
        else
            printf "%s\n%s %s\n\n%s\n" "$HTTP_404" "$HTTP_LOCATION" $REQUEST "Resource $REQUEST NOT FOUND!" > out
        fi
      fi
    done
  )
done
  1. docker build -t ncimange .

  2. docker run -d -i -p 80:80 --name ncrest ncimange

  3. docker container ls

    IMAGE               COMMAND                 CREATED             STATUS              PORTS                NAMES
    ncimange            "/bin/sh -c /rest.sh"   8 seconds ago       Up 2 seconds        0.0.0.0:80->80/tcp   ncrest
    
  4. docker ps

    IMAGE               COMMAND                 CREATED             STATUS              PORTS                NAMES
    ncimange            "/bin/sh -c /rest.sh"   41 seconds ago      Up 34 seconds       0.0.0.0:80->80/tcp   ncrest
    
  5. docker logs ncrest

    empty

  6. From host:

    curl -i http://127.0.0.1:80/date
    
    curl: (56) Recv failure: Connection reset by peer
    
  7. From container:

    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
    

How to connect to netcat rest docker container?


回答1:


You are installing the "wrong" netcat. Debian has two netcat packages: netcat-traditional and netcat-openbsd, and both are slighty different. The netcat package is an alias of netcat-traditional.

For example, in your case your nc command should be nc -l -p 80, because nc -l 80 will only work on netcat-openbsd.

tl;dr: install netcat-openbsd instead of ǹetcat if you wish to use your script unmodified..



来源:https://stackoverflow.com/questions/58920615/how-to-connect-to-netcat-running-in-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!