Viewing logs from the previous container instance when your container is crashing

Paul Dally
2 min readMar 19, 2024

--

Photo by Matt Hudson on Unsplash

When a container is repeatedly crashing, it may not be obvious how to determine what is wrong. You might look at kubectl log , but the problem that caused your container to crash will often have been immediately prior to the container restarting. Since the logs returned by kubectl log (by default) are only from the current instance of the container, and kubernetes will likely restart your container pretty quickly after your container crashes, the logs that you get may not have the information that you need.

Kubernetes will introduce a progressively longer delay between restarts., and that will tend to give you more time in which to retrieve the logs of interest before the container restarts. However, there is a much simpler and more reliable way than simply hoping the timing works out…

Thankfully, kubectl log has a handy flag, --previous or -p, that lets you see the log not for the current container instance, but rather for the previously started container (if there is one) — and at the end of the log for the previous container is likely where the relevant error messages will be.

If your container hasn’t restarted, you will receive an error message similar to the following:

Error from server (BadRequest): previous terminated container “mycontainer” in pod “mypod-75549d7bf6–78gr5” not found

Of course, you probably should have a logging agent on your worker nodes pushing your logs to a backend of some sort. This may also provide a solution to the question at hand. However, even when you have this, sometimes you just want something quick and dirty — and the -p flag may be just what you are looking for.

This is another of those things that seems to be well documented, but nonetheless does not seem to be well known — so hopefully this helps!

--

--

Paul Dally

AVP, IT Foundation Platforms Architecture at Sun Life Financial. Views & opinions expressed are my own, not necessarily those of Sun Life