This is a creation in Article, where the information may have evolved or changed.
Container signal Use
The program we run in the container usually wants to do some cleanup before the container exits, and the more common way is to listen for a signal and delay closing the container.
Docker provides such features as:
╰─➤ docker stop --helpUsage: docker stop [OPTIONS] CONTAINER [CONTAINER...]Stop one or more running containersOptions: --help Print usage -t, --time int Seconds to wait for stop before killing it (default 10)
Docker 1.13 or later can specify the Stop_timeout and stop_signal parameters directly when creating a container:
$ docker run --help...--stop-signal string Signal to stop a container, SIGTERM by default (default "SIGTERM")--stop-timeout int Timeout (in seconds) to stop a container...
But...
We test one:
package mainimport ( "fmt" "os" "os/signal" "syscall" "time")func main() { fmt.Println("signal test") go func() { for { c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGTERM) s := <-c fmt.Println("Got signal:", s) } }() time.Sleep(time.Second * 100)}
Dockerfile:
FROM dev.reg.iflytek.com/base/golang:1.8.0COPY main.go .RUN go build -o signal && cp signal $GOPATH/binCMD signal
Build:
docker build -t dev.reg.iflytek.com/test/signal:latest .
Run:
docker run --name signal dev.reg.iflytek.com/test/signal:latest
Open one more terminal, run:
docker stop -t 10 signal
Found and did not print out got signal: ... The monitoring signal failed.
The question again: we docker inspect signal look at
Can see
Path:/bin/shArgs:[ -c, signal]
or Docker exec signal PS to see that the process of PID 1 is not signal, but the shell.
The reason for this is that the Docker engine only sends a signal to the PID 1 process, and SH receives the signal and the signal process we want is not receiving a signal.
Workaround:
FROM dev.reg.iflytek.com/base/golang:1.8.0COPY main.go .RUN go build -o signal && cp signal $GOPATH/binCMD ["signal"] # 不能写成 CMD signal, 这会直接exec,否则会以shell的方式派生子进程。
More Questions I hope you will pay attention to my github:https://github.com/fanux