Questions about Docker's graceful exit

Source: Internet
Author: User
Tags docker run
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.