Using Docker Cache for Faster Builds
Often applications need to install it’s dependencies through a package manager, and most langauge don’t use system package manager to do this job. ex: python uses pip, php uses composer, ruby uses gem. and all of these package managers install packages for a project based on a package list file. and it is a common pattern to copy our code into docker container in this fashion.
COPY . /data/
pip install -r /data/requirements.txt
the problem with this is that everytime you change anyfile in your project, the docker build will try to install all of the packages in requirements.txt file. but most of the time we don’t want that. a better way would be to have multiple COPY instructions of which the first will install the packages the next will copy your code.
COPY ./requirements.txt /data/requirements.txt
pip install -r /data/requirements.txt
COPY . /data/
although both does the same job, with second one docker build won’t try to install your pip packages everytime any file is updated. it will only install requirements when the requirements.txt file gets updated.