This article is about CodeShip Pro.

Caching Multi-stage Dockerfile

You will need about one minute to read this article.

Because CodeShip Pro only caches image layers of the final Dockerfile stage, artifacts copied into the final stage may continually invalidate subsequent cache layers.

In our contrived Dockerfile, we have a multi-stage build:

# Dockerfile

FROM ubuntu:xenial as stage-one
COPY ./artifact-materials .
RUN ./make-artifact.sh

FROM ubuntu:xenial as app
COPY --from=stage-one ./artifact.sh ./artifact.sh
RUN ./time-consuming-build-process.sh
COPY . .

Our recommendation would be to split the stages into separate services:

# Dockerfile.stage-one

FROM ubuntu:xenial

COPY ./artifact-materials .
RUN ./make-artifact.sh
# Dockerfile.final

FROM stage-one as stage-one

FROM ubuntu:xenial

COPY --from=stage-one ./artifact.sh ./artifact.sh
RUN ./time-consuming-build-process.sh
COPY . .
# codeship-services.yml

stage-one:
  build:
    dockerfile: Dockerfile.stage-one
    image: stage-one # assert image name to prevent `codeship_` prefix from being applied
  cached: true

app:
  build:
    dockerfile: Dockerfile.final
  cached: true
# codeship-steps.yml

- name: build stage-one first in order to be available for `app` docker build
  service: stage-one
  command: true

- service: app
  command: ./run-tests.sh


Need more help?

Contact our support team or post on Stack Overflow using the tag #codeship. Did you check the status page and changelog?

There are also several code examples and sample projects available for you to get started with.

Article not helpful?

Does this article need improvement? If so, please send feedback or submit a pull request!