Long Build times using Docker Alpine images

Rajat Jain
2 min readDec 31, 2022

Do you use alpine image but suffer from long build times?

Alpine images are very small (python:alpine-3.10 5 MB) and are built for systems requiring small image sizes. Although, there are slim images available in the market but they are bigger in size and provide faster build times..

Alpine images also have a drawback:

  1. Standard PyPI wheels don’t work on Alpine. Alpine uses musl compiler which is a small & compact version of gnu-gcc. But PyPI wheels are compiled by glibc (GNU Version). So, if we have any python dependencies then it needs to download all the C code and compile it, which is time taking.
  2. Alpine Linux can cause unexpected runtime bugs. Alpine has a smaller default stack size for threads. Also, many users have reported unexpected crashes when using alpine images in the docker files.

But major companies still use Alpine images given that its size is vastly smaller than others. So, if we have image size as a constraint then we have no other option left but to use Alpine images.

People further tend to use Multistage builds to further reduce the size of their final image. For example, in the following Dockerfile, we can use first-stage to build python dependencies and carry-forward the dependencies to the next stage for the final image. This way we can make our image size compact and keep only what we need.

Dockerfile

But if you noticed, there is still a problem.

Building the dependencies still takes time.

Solution?

If our requirements don’t change that frequently, we can create a new base image using Alpine with all the requirements installed on top of it. This way, we need not compile it everytime and save hours for our development team.

We will now use this newly created base image in our projects and just copy all the pre-built dependencies into our image

But whenever there’s a change in requirements/dependencies, we can update the new base image and use this image in our Dockerfile.

Dockerfile to build new source image.
Main Dockerfile (using our new base image).

References:

  1. https://pythonspeed.com/articles/alpine-docker-python/
  2. https://pythonspeed.com/articles/base-image-python-docker-images/

--

--

Rajat Jain

Tech Blogger. Addicted to OSS, PHP & Performance. Born & brought up in India. Travelled 5 countries. A Table-Tennis player.