受到 this post 的启发,我尝试在 Mac 上使用 docker-machine
来停止正在运行的容器。但是,awk
在 docker-machine ssh
中的行为似乎与直接“在”容器中运行时不同。
当我直接通过 ssh 连接到容器时,一切都按预期工作:
me@myMac:~$ docker-machine ssh default
docker@default:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
028fd7148881 myrepo/mycontainer "apachectl -DFOREGROU" 14 minutes ago Up 14 minutes 0.0.0.0:80->80/tcp gloomy_mayer
docker@default:~$ docker ps | awk 'NR > 1 {print $1}'
028fd7148881
docker@default:~$ docker ps | awk 'NR > 1 {print $1}' | xargs --no-run-if-empty docker kill
028fd7148881
docker@default:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
但是当远程运行时,
awk
表现不同:me@myMac:~$ docker-machine ssh default "docker ps"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7927b10b8a28 myrepo/mycontainer "apachectl -DFOREGROU" 11 seconds ago Up 10 seconds 80/tcp happy_wozniak
me@myMac:~$ docker-machine ssh default "docker ps | awk 'NR > 1 {print $1}'"
7927b10b8a28 myrepo/mycontainer "apachectl -DFOREGROU" About a minute ago Up About a minute 80/tcp happy_wozniak
me@myMac:~$ docker-machine ssh default "docker ps | awk 'NR > 1 {print $1}' | xargs --no-run-if-empty docker kill"
7927b10b8a28
Failed to kill container (myrepo/mycontainer): Error response from daemon: Cannot kill container myrepo/mycontainer: No such container: myRepo/myContainer
Failed to kill container (apachectl -DFOREGROU): Error response from daemon: Cannot kill container apachectl -DFOREGROU: No such container: apachectl -DFOREGROU
Failed to kill container (3): Error response from daemon: Cannot kill container 3: No such container: 3
...
为什么
awk
在 docker-machine
下表现不同? 最佳答案
"docker ps | awk 'NR > 1 {print \$1}' | xargs --no-run-if-empty docker kill"
$ ==> \$
docker ps -q
关于awk - 来自 docker-machine 中 awk 的意外行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36560576/