Skarab
Skarab

Reputation: 7141

How to save intermediary docker images created in multi-stage builds?

I want to cache my docker on travis. The best method would be just saving newly created image on disk with docker save.

It would be super simple with a classic one-image-docker build, but I need to have a small final image, so I do multi-stage:

FROM danlynn/ember-cli:3.0.0 AS deps

RUN mkdir -p /client

# get nodejs deps:
COPY client/package.json /client/
COPY client/package-lock.json /client/

WORKDIR /client

RUN npm install node-sass
RUN npm install
RUN npm rebuild node-sass
... more long operations

FROM node:wheezy AS server
COPY --from=deps /client/ /client/

Is there a way to save immediately image deps and restore it later?

Upvotes: 3

Views: 1892

Answers (1)

takacsmark
takacsmark

Reputation: 4441

You can build a specific stage with docker build --target, so you don't need to build all stages all the time. I think this should be helpful in your case.

You can read about this in Docker docs here: https://docs.docker.com/develop/develop-images/multistage-build/#stop-at-a-specific-build-stage.

When you build your image, you don’t necessarily need to build the entire Dockerfile including every stage. You can specify a target build stage. The following command assumes you are using the previous Dockerfile but stops at the stage named builder: Blockquote

$ docker build --target builder -t alexellis2/href-counter:latest .

You can even use an external image as a stage, it is described in the same docs, so you can do something like this:

COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf

Upvotes: 3

Related Questions