Multiple RUN commands as opposed to a single chained RUN command

Docker images should be kept as small and efficient as possible. If there are files on the shipping image that you won’t be using, you should get rid of them throughout the build process.

When you breakdown your commands into multiple RUNs, docker adds a corresponding layer to that image. The results are the alterations to the file system that may be inspected with docker diff on the intermediate container.

Removing unnecessary files in a separate RUN defeats the purpose of reducing the image footprint because, on top of a new layer is created, Docker will still keep the deleted files in the previous inactive layer, which is shipped over the network and stored on disk.

1
2
3
4
5
6
7
8
RUN python -m venv /py && \
    /py/bin/pip install --upgarde && \
    /py/bin/pip install -r /tmp/requirements.txt && \
    rm -rf /tmp && \
    adduser \
        --disabled-password \
        --no-create-home \
        django-user

By combining commands in a single RUN, you avoid the creation of a new layer and the retention of unnecessary files.

updatedupdated2024-01-172024-01-17