Have you ever wondered which Docker image to choose as the execution environment for your Python application? I tended to decide that I would settle down on the official image after worrying about it every time, so I decided to make a comparison here. I hope it will be helpful to everyone.
When choosing an image, I think there are the following perspectives.
Perspective | Contents |
---|---|
Stability | It is difficult to use it for a long time if the file structure and contents change frequently, so some stability is required.(I don't think there is much)。 Therefore, I would like to use an image that is close to the official one. |
Image size | The size of the image can be evaluated most quantitatively. The smaller this is, the better. |
Security updates | Security updates vary by OS distribution, but you need to know how long you will support them. |
Is Python installed? | You can install it yourself, but it will save you time and build time if it is installed from the beginning. |
It is very stable because you can check the stability and the support period of security updates using a standard distribution. However, it also contains many common packages that are not needed to run Python applications, and the image size tends to be large. You also need to install the latest version of Python yourself.
If you want to make the image smaller, alpine-linux may be the first candidate. However, according to the article linked below, it seems that using alpine will eventually cause various problems such as increased image capacity and increased build time. Using Alpine can make Python Docker builds 50× slower
The following is a summary of the content mentioned in the article.
Therefore, alpine has a small raw image size, but it remains uneasy as a Python execution environment.
When using Python with Docker, I think many people basically use this image. The types of tags are roughly as follows.
Image type | The latest previous tag(2020/As of 03) | size | Remarks |
---|---|---|---|
alpine | 3.8.1-alpine3.11 | 109MB | There is an image of Alpine here as well, but it is not recommended due to the disadvantages mentioned above. |
buster | 3.8.1-buster | 933MB | An image based on Buster, the latest version of Debian. It's not lightweight because it has many basic packages installed, but it's a good choice for general purpose use. |
buster-slim | 3.8.1-slim-buster | 193MB | This is a version in which the common package in the Debian image is omitted and the image size is reduced accordingly. If you only use Python applications, there seems to be no problem. |
If you compare the tags, you can see that there is a slim version in addition to the main Debian image. This is an image specialized for executing Python, and it seems that many other functions have been dropped. A note on the python-docker page said, "Use slim if you have strict image size restrictions for Python execution only, and we recommend the default image otherwise." The choice depends on how far you want to run in the container.
From the above considerations, we came to the conclusion that the Docker Image suitable for running Python applications simply is Debian Buster slim. The image size is 198MB, which is quite compact.
Recommended Posts