5 words of inspiration to containerize your code: Did you try restarting it?

Shubham Sakhuja
2 min readJul 31, 2019

--

Upon encountering an issue with your workstation, have you ever been asked by IT staff the question: “Did you try restarting it?” Of course you have, and of course, more often than not, it has resolved the issue! How well this works is dependent on how well the code running on your workstation was written. We should strive to have all code written to be able to heal itself. This is made easier with containers, which automatically restart upon failure.

How it Works

When issues surface, we prefer that they automatically resolve to minimize service disruption in the short-term. This is facilitated by containers via the following behavior:

Each container runs a single process, if this exits (e.g. there’s a runtime exception), the container is automatically restarted.

This means your application code has a standard mechanism to automatically heal when it reaches a bad state: exit the process -> restart. This draws the relationship Restart = Heal, leading to code that is written to handle failure conditions by gracefully restarting, while decreasing the costly burden of remediating issues manually in production.

Considerations

In the case your code needs to clean up before restarting (similar to how you may want to save some files before restarting your workstation), it’s important to implement code that can do so in response to a kill signal. Various container runtimes also offer the capability of running a command in the container and waiting until it completes before restarting it.

Health checks are always a best practice, especially as a first line of defense in cases where restarting a container can entail an undesirable amount of overhead.

Auto healing helps get your code back to a good state, but it doesn’t prevent the same issues from resurfacing in the future. This is why it’s important to still analyze errors and implement fixes to prevent their reoccurrence.

So next time an unknown issue arises when running your code, you can ask the famous question: “Did you try restarting it?”

--

--

Shubham Sakhuja

Working @ aramse.io, delivering cloud-native solutions to help companies streamline DevOps