Large python image size in docker

I want to test my application using Docker . So, I have this in the Dockerfile :

FROM python:3-onbuild CMD [ "python", "./test.py" ] 

test.py:

 print(123) 

Then I run:

 docker build -t my_test_app . 

So, I have one big image. docker images return:

 REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE python 3-onbuild b258eb0a5195 8 days ago 757 MB 

Why is the file size so big?

Is this size normal?

+11
source share
5 answers

I just checked the standard ubuntu on my machine: the reliable image is 188 MB and the image with all python materials is 480 MB. I see 800MB images quite often, usually those that contain some kind of meaningful application.

However, these images are based on our private images, the official image of the Docker library seems a lot larger for some reason. They are aware of this fact and are trying to reduce it. Check out the discussion thread here

If you need a slightly smaller image, try this "rouge8 / pythons", it will be 100 MB less.

 rouge8/pythons latest … 680.3 MB 

Keep in mind that docker images are ordered as a hierarchical layer structure. Therefore, if you reuse the same base image for many containers, the size added by each individual container is quite small. It will be only the difference between the base plus everything that you added to a specific container.

+2
source

Alpine Linux is a very meager distribution available for Docker. Without Python, it is about 5 MB. With Python, I get images from 60 to 120 MB. The following Dockerfile has a 110 MB image.

 FROM alpine:3.4 RUN apk --update add \ build-base python-dev \ ca-certificates python \ ttf-droid \ py-pip \ py-jinja2 \ py-twisted \ py-dateutil \ py-tz \ py-requests \ py-pillow \ py-rrd && \ pip install --upgrade arrow \ pymongo \ websocket-client \ XlsxWriter && \ apk del build-base python-dev && \ rm -rf /var/cache/apk/* && \ adduser -D -u 1001 noroot USER noroot CMD ["/bin/sh"] 

In addition, it is very well maintained.

+5
source

You can try the python version: {version} -alpine. This is much smaller:

 >> docker image ls |grep python python 3.6-alpine 89.4 MB python 3.6 689 MB python 3.5 689 MB python 3.5.2 687 MB python 3.4 833 MB python 2.7 676 MB 

At the time of writing, the official image -alpine to support -alpine in all versions of Python.

https://hub.docker.com/_/python/

+4
source

They add various system packages for things like database clients, image file manipulation, and XML parsing libraries. Thus, there is no additional work that the user must do if they want to install Python packages for psycopg2, MySQLdb, Pillow or lxml. Adding these extra packages means the image will be thicker, and if you do not need these packages, it will be a waste of space.

They also do not try to trim material from the Python installation, which is not required, for example, all standard library test code catalogs. Even .pyc files can be trimmed to save space without any real effect, since a web application usually loads once during the life of the container, so having .pyc files doesn't really help you much.

As a comparison, consider the options "pythonX.Y-slim" and their size. There is no onbuild option for thin images.

You can also see my own Docker images for Python with Apache / mod_wsgi support. They are trimmed and rely on additional packages set by the user as prefabricated hooks only if necessary. For these users, Python 3.4 onbuild image size specifically for the WSGI application:

 grahamdumpleton/mod-wsgi-docker python-3.4-onbuild ... 409.9 MB 

The given size even includes Apache and mod_wsgi, providing you with a full-fledged WSGI server with the ability to handle static file contents and much more.

If the WSGI application is not running, start with the base image.

Mod_wsgi docker images can be found at:

Various blog entries on how to use these images for WSGI applications and how to build Docker images for Python and WSGI applications can be found by contacting the image description on the Docker site. Also stay tuned for my blog as a whole, as I will post more about Docker and Python over time.

+1
source

Yes, this is a normal size. The image contains an operating system image and various packages, and therefore size.

0
source

All Articles