Search code examples
dockerapt

speeding up 'apt-get update' to speed up Docker image builds


I want to add curl to a Docker image, and I'm using the following commands to in a Dockerfile to do so:

RUN apt-get update
RUN apt-get install curl ca-certificates -y

My issue is that the initial update takes a pretty long time to run (2 min), so while I'm debugging my Dockerfile, iteration is slow. In particular when I make changes before the RUN apt-get update, which invalidate Docker's image cache.

Is there any way to be more selective with apt-get update, so it only updates enough to index where to download curl? Or some other technique I can use to speed up my Docker builds?

Here is the whole Dockerfile,

FROM postgres:9.6.10
ADD data/tsvs.tar.gz /standard_data
COPY postgres/*.sql /docker-entrypoint-initdb.d/

RUN apt-get update
RUN apt-get install curl ca-certificates -y
RUN curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
RUN apt-get install postgis postgresql-9.6-postgis-scripts -y

I'm currently making changes to the SQL files in postgres/*.sql, hence the cache invalidation.


Solution

  • An image is organized in layers and each layer depends on the previous later. Layers are also cached for speed.

    When you run the build again do keep checks if the ch ckecksum of a command line in the dockerfile changed. If it didn't then it pulls the layer from cache. But if it did then it rebuilds the later and all successive layers.

    In your particular case the ADD command generates a new layer each time you make a change and that triggers all the successive layers to be rebuilt.

    By move moving the installation before you fix this issue.

    You should also put all the installations on 1 line and clean the apt cache when you are done.

    RUN apt-get install curl ca-certificates -y && \
      curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
      apt-get install postgis postgresql-9.6-postgis-scripts -y && \
      rm -rf /var/cache/apt && \
      apt-get clean