This page looks best with JavaScript enabled

docker cache

build 時安裝套件使用 cache 加速

記得先在 Dockerfile 加上 # syntax = docker/dockerfile:XXX

apk add

1
2
3
4
5
6
# syntax = docker/dockerfile:1.3
RUN --mount=type=cache,target=/var/cache/apk \
    apk add \
    # --no-cache \
    libxslt-dev \
    mariadb-connector-c-dev

apt-get install

1
2
3
4
5
6
7
8
9
# syntax = docker/dockerfile:1.3
RUN rm -f /etc/apt/apt.conf.d/docker-clean; \
    echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' \
    > /etc/apt/apt.conf.d/keep-cache
RUN --mount=type=cache,target=/var/cache/apt \
    --mount=type=cache,target=/var/lib/apt \
    apt update \
    && apt-get --no-install-recommends install -y \
    ...

pip install

1
2
3
4
5
# syntax = docker/dockerfile:1.3
RUN --mount=type=cache,target=/root/.cache/pip \
    pip install \
    # --no-cache-dir \
    -r requirements.txt

go build

1
2
3
4
# syntax = docker/dockerfile:1.3
RUN --mount=type=cache,target=/root/.cache/go-build \
    go build \
    ...

參考資料

https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/syntax.md#build-mounts-run---mount