Docker Build Layers¶
19 January 2020 ยท learn++
Docker build layers are an amazing concept and can greatly speedup your development workflow.
Docker Layers¶
A layer in Docker is a snapshot of the most atomic part of a Docker build. This means, that Docker will not need to rerun the part of the build that is part of a layer.
Layers are created by commands that have an internally consistent state.
For example a RUN command does not get called again unless it depends on another non-deterministic directive in the Dockerfile.
Non-deterministic directives are for example:
You can easily make this deterministic by downloading the file first and then using
This way, Docker can create a layer from it and cache the result.
Another example where this is helpful is apt-get:
You could write:
and then later discover, you also need less.
Instead of adding less to the line like so:
you can add another apt-get RUN command like so:
This way, Docker can use the cached layer from
and just add
Also, if you upload to the Docker Hub or even your own container registry, you will be saving space for other containers that install the same layer.