我正在尝试在 docker 容器内运行用 golang 编写的服务器。例如:
package main
import "net/http"
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello"))
})
http.ListenAndServe(":3000", nil)
}
If I run this code on my local machine, I can send it a SIGINT
with Ctrl-C and it will close the application. When I run it inside a docker container, I can't seem to kill it with Ctrl-C.
# Dockerfile
FROM ubuntu:14.04
RUN apt-get update && apt-get -y upgrade
RUN apt-get install -y golang
ENV GOPATH /go
COPY . /go/src/github.com/ehaydenr/simple_server
RUN cd /go/src/github.com/ehaydenr/simple_server && go install
CMD /go/bin/simple_server
然后我继续使用 docker 向容器发送信号。
docker kill --signal=INT 9354f574afd4
仍在运行...
docker kill --signal=TERM 9354f574afd4
仍在运行...
docker kill --signal=KILL 9354f574afd4
终于死了。
我没有在代码中捕获任何信号并忽略它们。我什至尝试增强上面的代码来捕获信号并将其打印出来(这在我的主机上工作,但在容器中时,就好像信号从未到达程序一样)。
Has anyone experienced this before? I haven't tried something like this in another language, but I able to kill servers (e.g. mongo
, nginx
) using Ctrl-C while they're in a docker container.. Why isn't Go be getting the signals?
不确定这是否有什么不同,但我在 OSX 上并使用 docker-machine。
任何帮助深表感谢。