Fraser Langton
Fraser Langton

Reputation: 661

Docker, can you cache apt-get package installs?

The beginning of my docker file is below, I change the stuff after it often but never this part, but it takes a while to run, especially RUN apt-get -y install npm, can I cache the package download somehow? I looked at docker caching but I don't think that is what it does?

FROM ubuntu:20.04

RUN apt-get -y update
RUN apt-get -y install ruby
RUN apt-get -y install ruby-dev
RUN apt-get -y install gcc
RUN apt-get -y install make
RUN gem install compass
RUN apt-get -y install nodejs
RUN apt-get -y install npm
RUN apt-get -y install git

Upvotes: 34

Views: 21585

Answers (1)

kdauria
kdauria

Reputation: 6671

This will store apt-get packages in the docker cache so they won't need to be re-downloaded. You need to be using buildkit.

# syntax=docker/dockerfile:1.3.1
FROM ubuntu

RUN --mount=target=/var/lib/apt/lists,type=cache,sharing=locked \
    --mount=target=/var/cache/apt,type=cache,sharing=locked \
    rm -f /etc/apt/apt.conf.d/docker-clean \
    && apt-get update \
    && apt-get -y --no-install-recommends install \
        ruby ruby-dev gcc

See this documentation for more details.

Upvotes: 42

Related Questions