Joelgullander
Joelgullander

Reputation: 1684

Convert from multi stage build to single

As i'm limited to use docker 1.xxx instead of 17x on my cluster, I need some help on how to convert this multi stage build to a valid build for the older docker version.

Could someone help me?

FROM node:9-alpine as deps

ENV NODE_ENV=development

RUN apk update && apk upgrade && \
    apk add --no-cache bash

WORKDIR /app
COPY . .
RUN npm set progress=false  \
    && npm config set depth 0 \
    && npm install --only=production \
    && cp -R node_modules/ ./prod_node_modules \
    && npm install

FROM deps as test

RUN rm -r ./prod_node_modules \
  && npm run lint

FROM node:9-alpine
RUN apk add --update tzdata

ENV PORT=3000
ENV NODE_ENV=production

WORKDIR /root/
COPY --from=deps /app .
COPY --from=deps /app/prod_node_modules ./node_modules

EXPOSE 3000
CMD ["node", "index.js"]

Currently it gives me error on "FROM node:9-alpine as deps"

Upvotes: 1

Views: 633

Answers (1)

VonC
VonC

Reputation: 1327912

"FROM node:9-alpine as deps" means you are defining an intermediate image that you will be able to COPY from COPY --from=deps.

Having a single image means you don't need to COPY --from anymore, and you don't need "as deps" since everything happens in the same image (which will be bigger as a result)

So:

FROM node:9-alpine

ENV NODE_ENV=development

RUN apk update && apk upgrade && \
    apk add --no-cache bash

WORKDIR /app
COPY . .
RUN npm set progress=false  \
    && npm config set depth 0 \
    && npm install --only=production \
    && cp -R node_modules/ ./prod_node_modules \
    && npm install

RUN rm -r ./prod_node_modules \
  && npm run lint

RUN apk add --update tzdata

ENV PORT=3000
ENV NODE_ENV=production

WORKDIR /root/
RUN cp -r /app .
RUN cp -r /app/prod_node_modules ./node_modules

EXPOSE 3000
CMD ["node", "index.js"]

Only one FROM here.

Upvotes: 5

Related Questions