dockerfile - running netcat inside docker container -
i have created docker images using below dockerfile.
ubuntu run apt-get update \ && debian_frontend=noninteractive apt-get install -y \ net-tools \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* run apt-get update \ && debian_frontend=noninteractive apt-get install -y \ netcat \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* expose 1234 entrypoint bin/bash cmd ["nc", "-l", "1234"] i created image aboce docker file , run docker container using image running below command.
docker run -d -i -p 1234:1234 --name daemon nc-ubuntu nc -l 1234 in terminal, run below command.
telnet localhost 1234 i got below output.
$ telnet localhost 1234 trying ::1... connected localhost. escape character '^]'. connection closed foreign host. i trying this sample book docker in practice in chapter 2 manning running docker daemon process.
as per author should below result.
$ telnet localhost 1234 trying ::1... connected localhost. escape character '^]'. hello daemon any idea why i'm not getting expected output.
that's never going work. there several problems dockerfile.
1
setting entrypoint /bin/bash means docker run ... going start bash. read this question entrypoint , cmd.
2
since you're in non-interactive mode, bash going exit immediately. consider:
host$ docker run nc-ubuntu host$ vs:
host$ docker run -it nc-ubuntu root@e3e1a1f4e453:/# the latter, because of -it (which allocates tty device, bash requires in interactive mode), gets bash prompt.
3
neither invocation cause container run netcat...and if did, nothing in dockerfile generate hello daemon response you're expecting.
4
the nc command line incorrect. syntax is:
nc -l -p <port> so need:
cmd ["nc", "-l", "-p", "1234"] 5
if want nc provide hello daemon response, need add appropriate -c command nc command line, in:
cmd ["nc", "-l", "-p", "1234", "-c", "echo hello daemon"] this makes final dockerfile like:
from ubuntu run apt-get update \ && debian_frontend=noninteractive apt-get install -y \ net-tools \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* run apt-get update \ && debian_frontend=noninteractive apt-get install -y \ netcat \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* expose 1234 cmd ["nc", "-l", "-p", "1234", "-c", "echo hello daemon"] and if build that:
docker build -t nc-ubuntu . and run that:
docker run -d -i -p 1234:1234 --name daemon nc-ubuntu i can telnet port 1234 on host , see expected response:
host$ telnet localhost 1234 trying ::1... connected localhost. escape character '^]'. hello daemon connection closed foreign host. at point, container have exited because nc exits after accepting single connection (without additional parameters), , docker contain exits when foreground process exits.
i don't have access book can't tell if problem book or if have made mistake in implementation, suggest there number of online docker tutorials @ least good.
Comments
Post a Comment