dongshi8038
dongshi8038
2015-10-27 22:26

将信号发送到Docker中的Golang应用程序

已采纳

I am trying to run servers written in golang inside docker containers. For example:

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

I then proceeded to use docker to send signals to the container.

docker kill --signal=INT 9354f574afd4

Still running...

docker kill --signal=TERM 9354f574afd4

Still running...

docker kill --signal=KILL 9354f574afd4

Finally dead.

I'm not catching any signals in my code and ignoring them. I've even tried augmented the code above to catch signals and print them out (which works on my host, but when in the container, it's as if the signals never got to the program).

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?

Not sure if this makes a difference, but I am on OSX and using docker-machine.

Any help is much appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douzhi2017 douzhi2017 6年前

    You are running your server inside a shell, and the shell is the process receiving the signals. Your server doesn't exit until you force the shell to quit.

    When you use the "shell" form of CMD, it starts your server as an argument to /bin/sh -c. In order to exec the server binary directly, you need to provide an array of arguments to either CMD or ENTRYPOINT, starting with the full path of the executable.

    CMD ["/go/bin/simple_server"]
    

    A note from ENTRYPOINT in the Dockerfile docs:

    The shell form prevents any CMD or run command line arguments from being used, but has the disadvantage that your ENTRYPOINT will be started as a subcommand of /bin/sh -c, which does not pass signals.

    点赞 评论 复制链接分享