Docker and application exit codes

by George Y. Kussumoto

At the company I currently work, we start to using docker in a few projects. So far the experience has been really nice, but from time to time I still feel it needs some maturity. For instance, docker-compose is really neat and provides a nice experience if your application depends in more than one service like database, cache, broker and so on.

The development and tests feel much more close to production environments (I still don't have this experience), without cost too high on performance and a learning curve that fit in a couple of days. But it also brings a set of issues that you would not expect, as I said it lack some maturity and I'm not alone in my concerns.

On the other hand, the idea of an isolated and stateless application containers enhances a lot of good practices. One that I often note is how many times developers forget about the exit codes (including me). Consider the following snippet:

if __name__ == '__main__':
    main()

This is a very common pattern and in the most cases will not hurt anybody. The main function will finish or maybe an error happen but nothing critical, except perhaps if you are testing using docker.

The pattern above might mask some exit code returned by a code inside the main function, so if you are running the application inside a container you will never know the original meaning of the returned code. Fixing that is just a case of good practice:

import sys

if __name__ == '__main__':
    sys.exit(main())

You also have to consider if you are using run ou up commands, this issue should give you some thoughts about it.

o/