Does a goroutine terminates after its Kubernetes container gets terminated?


For example, a container runs the main thread and a goroutine. The main thread encounters an issue and terminates. Note that for Golang, termination of the main thread does not result in auto-termination of the goroutine.

  1. As the main thread has been terminated, will the container be killed and re-created? Or will the container continue running due to the goroutine is still running?

  2. If the container will be killed and re-created after the main thread has been terminated, will this result in the goroutine getting terminated as well? Or will the goroutine continue running indefinitely and there is no easy way to terminate it now?


If the main functions exists, the program is stopped. Nothing will run any more. It will release any used resource, like file descriptors and database connections.

In the below program, we will never see done being printed.

func main() {
    go func() {

    time.Sleep(time.Second * 3)

If the program with that main function was the foreground process of the container, then the container shuts down as its standard behaviour with containers.

If you run the below example, you can observe how the container shuts down as soon as the sleep finishes.

$ docker run --name sample busybox sleep 3 && docker ps -a
CONTAINER ID   IMAGE     COMMAND     CREATED         STATUS                              PORTS     NAMES
fd4319261a0d   busybox   "sleep 3"   4 seconds ago   Exited (0) Less than a second ago             sample

If the container, the program is running in, is shut down, it’s more or less as if you would pull the plug of your computer. Nothing will run on your computer any more. It’s impossible.

I would encourage you to create some test scenarios yourself and validate this.

Answered By – The Fool

Answer Checked By – Willingham (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.