Now is the era of containers, cooperation between teams must be packaged unified Docker Image to solve the problem of inconsistent environment, but the size of the container determines the time to deploy microservices, this article to introduce how to build the smallest Python container. If you need to collaborate across departments, Docker is definitely the best tool for you, so you don’t have to bother with the environment. The following Flask application shows how to write a Dockerfile.
Writing the Flask service
Use the Flask service as an example directly and store the file in
Write all related packages in
The developer can write all the dependency packages with the following commands.
Writing a Dockerfile
Find the official Python Docker Image page, which has an example of how to write a
If you don’t care much about the image size, there is nothing wrong with writing it this way. To create a smaller Image, you can start with
FROM. In fact, it is officially mentioned that if you want to use it as Production, it is recommended to use
python:<version>-slim, which removes all unnecessary packages and leaves only the most basic Python packages to run. developers can use this image to overlay the packages they want to use, so the image will be minimal. Let’s replace
In addition to
slim, I would personally recommend using the
alpine version (previous introductory article), where the entire In order to reduce the system to such a small size, Alpine version removes most of the software, including
bash large packages, so basically, after logging into the system, it is almost empty, and you have to install all the packages yourself, which is very suitable for microservices and can achieve rapid deployment.
Please note that you must process the requirements.txt part first, and then process the
COPY file, so that you can enjoy the advantages of Layer Cache, otherwise the whole build image time will not be saved after the Source Code changes.
Using Multistage Build
In addition to using the smallest size Alpine Image, you can also use Multistage Build to copy the required files into a new Image for final packing, and change the writing style as follows to reduce some space.
Take a look at the results below.
Here the files are placed under the root with
--user, and then copied to another image with