转自:https://forums.manning.com/posts/list/43093.page
以下是原文的提问:
Running the following code:
docker run -d --name namespaceA \
busybox:latest /bin/sh -c "sleep 30000"
docker run -d --name namespaceB \
busybox:latest /bin/sh -c "nc -l -p 0.0.0.0:80"
Creates the docker containers and returns the container ID. I then run the following as instructed in the book:
docker exec namespaceA ps
Which returns the what it should according to the book:
PID USER COMMAND
1 root /bin/sh -c sleep 30000
5 root sleep 30000
6 root ps
However, when I run the last line instructed:
docker exec namespaceB ps
I get the following:
Error response from daemon: Container 5305b378d1702831bc1661840ad24341280298957f80d4f7b1e20b4e26761693 is not running
Instead of what the book shows:
view sourceprint?
PID USER COMMAND
1 root /bin/sh -c nc -l -p 0.0.0.0:80
7 root nc -l -p 0.0.0.0:80
8 root ps
按书上所写,运行 docker exec namespaceB ps 会显示PID的列表,但实际上却是
Error response from daemon: Container 5305b378d1702831bc1661840ad24341280298957f80d4f7b1e20b4e26761693 is not running
查看log:
docker logs namespaceB
结果是:
nc: bad local port '0.0.0.0:80'
以下是原文的回答:
The problem is this command
nc -l -p 0.0.0.0:80
What the autor is trying to do here is run a container with this nc command as the argument for /bin/sh, opening the port 80 inside the container and waiting for a incoming connection in that port, when the command ends its execution the container stops.
With the container called namespaceA the command is sleep 30000 so when you do docker exec namespaceA ps you are seeing this commands still in execution (but if you wait 30000 seconds what you’ll get is the same message of the container namespaceB, that this has stopped)
But when you run the namespaceB command, this inmediatly stops because the command you send it is wrong. According to Busybox nc documentation the syntax you need follow if you want open a port and waiting for any incoming is:
nc -l PORT_NUMBER
So, the right command is
nc -l -p 80
You can run docker logs namespaceB in order to see the logs of this container.
nc: bad local port '0.0.0.0:80'
结论就是:书上写错了,nc命令的使用方式不对,应该为
nc -l -p 80