Golang apps do not need a supervisor like `S6` in a docker container

Paul Fortin
1 min readJul 1, 2018

--

In many languages, you need to run an application like S6 to detect a shutdown and send your app a graceful shutdown in order to avoid lost requests. In GO, this process is not necessary as you can easily detect and use the OS signals sent to the docker container to stop in your application and shutdown gracefully.

When you do a docker stop, docker sends a SIGTERM to the container and then proceeds to give the container 10 secs to shutdown on it’s own. If the container does not shutdown then a SIGKILL is issued and the container is killed. That 10 secs sweet spot is yours to do what you need to.

Here is a trivial example that gracefully shuts down on a docker stop and the steps to see it in action.

PREPARATION

  • Compile the app
GOOS=linux GOARCH=amd64 go build -o main main.go
  • build the docker container
docker build -t endless .```

TEST

  • Run docker
docker run — name endless endless
  • Open another CLI and type
docker stop endless;docker rm endless
  • Verify in the first CLI that the app output the message: “Shutting down…”

Happy GOing!!!

--

--