DEV Community

Discussion on: docker learn #06: Hello Python in Alpine

 
autoferrit profile image
Shawn McElroy

Awesome! Glad you got it. One thing to keep in mind, is that each RUN command will result in another docker layer that can be cached. So its good to string them together like so:

RUN apk update \
    apk add libffi-dev \
    apk add mysql-client \
    apk add gawk

That way, that one layer can be cached for future builds. This can also effect image sizes. running api update and so on will download other metadata and those will stay on the image. Anything added in a layer will take up space in all future layers, unless its also removed in that same layer. So when using apk (or other package managers) in docker you want to clean up afterwards. Here is how I would re-write the above to do that

RUN apk add --no-cache --virtual .build-deps \
    # requirements you usually don't need to keep around but need to build libs
    gcc libc-dev make \
    # requirements you need to make your app run
    libffi-dev mysql-client gawk \
    # example installing anything that needs those build requirements
    && pip install --no-cache-dir uvicorn gunicorn \ 
    # this will remove the deps no longer needed, as well as clean up anything else
    && apk del .build-deps gcc libc-dev make

And all that will be cached in 1 layer, and those temporary deps installed by apk won't take up any space. this is the image I took this from
github.com/tiangolo/uvicorn-gunico...