Skip to content

Commit

Permalink
Prune devDependencies in Dockerfiles (#435)
Browse files Browse the repository at this point in the history
This was raised by @jahredhope and @brian-captain-crosby in Slack.
Thanks!

Co-authored-by: Brian Crosby <[email protected]>
  • Loading branch information
72636c and brian-captain-crosby authored Jun 1, 2021
1 parent 88ad97f commit 50792c8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 36 deletions.
7 changes: 7 additions & 0 deletions .changeset/silent-suits-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'skuba': patch
---

**template:** Prune `devDependencies` instead of installing twice in Docker

The template-bundled Dockerfiles would previously run `yarn install` twice to build a separate stage for production `dependencies` only. These have been updated to correctly share the Yarn cache across stages and to use `yarn install --production` to perform offline pruning.
28 changes: 10 additions & 18 deletions template/express-rest-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
# Docker image history includes ARG values, so never target this stage directly
FROM node:14-alpine AS unsafe-deps
FROM node:14-alpine AS unsafe-dev-deps

WORKDIR /workdir

COPY package.json yarn.lock ./

ARG NPM_READ_TOKEN

RUN \
echo '//registry.npmjs.org/:_authToken=${NPM_READ_TOKEN}' > .npmrc && \
yarn install --frozen-lockfile --ignore-optional --non-interactive --production && \
rm .npmrc

###

# Docker image history includes ARG values, so never target this stage directly
FROM unsafe-deps AS unsafe-dev-deps

ARG NPM_READ_TOKEN
RUN yarn config set cache-folder /yarn

RUN \
echo '//registry.npmjs.org/:_authToken=${NPM_READ_TOKEN}' > .npmrc && \
Expand All @@ -26,19 +16,21 @@ RUN \

###

FROM node:14-alpine AS deps
FROM node:14-alpine AS dev-deps

WORKDIR /workdir

COPY --from=unsafe-deps /workdir .
COPY --from=unsafe-dev-deps /workdir .

###
COPY --from=unsafe-dev-deps /yarn /yarn

FROM node:14-alpine AS dev-deps
RUN yarn config set cache-folder /yarn

WORKDIR /workdir
###

COPY --from=unsafe-dev-deps /workdir .
FROM dev-deps AS deps

RUN yarn install --ignore-optional --ignore-scripts --non-interactive --offline --production

###

Expand Down
6 changes: 6 additions & 0 deletions template/greeter/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ COPY package.json yarn.lock ./

ARG NPM_READ_TOKEN

RUN yarn config set cache-folder /yarn

RUN \
echo '//registry.npmjs.org/:_authToken=${NPM_READ_TOKEN}' > .npmrc && \
yarn install --frozen-lockfile --ignore-optional --non-interactive && \
Expand All @@ -20,6 +22,10 @@ WORKDIR /workdir

COPY --from=unsafe-dev-deps /workdir .

COPY --from=unsafe-dev-deps /yarn /yarn

RUN yarn config set cache-folder /yarn

###

FROM dev-deps AS build
Expand Down
28 changes: 10 additions & 18 deletions template/koa-rest-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
# Docker image history includes ARG values, so never target this stage directly
FROM node:14-alpine AS unsafe-deps
FROM node:14-alpine AS unsafe-dev-deps

WORKDIR /workdir

COPY package.json yarn.lock ./

ARG NPM_READ_TOKEN

RUN \
echo '//registry.npmjs.org/:_authToken=${NPM_READ_TOKEN}' > .npmrc && \
yarn install --frozen-lockfile --ignore-optional --non-interactive --production && \
rm .npmrc

###

# Docker image history includes ARG values, so never target this stage directly
FROM unsafe-deps AS unsafe-dev-deps

ARG NPM_READ_TOKEN
RUN yarn config set cache-folder /yarn

RUN \
echo '//registry.npmjs.org/:_authToken=${NPM_READ_TOKEN}' > .npmrc && \
Expand All @@ -26,19 +16,21 @@ RUN \

###

FROM node:14-alpine AS deps
FROM node:14-alpine AS dev-deps

WORKDIR /workdir

COPY --from=unsafe-deps /workdir .
COPY --from=unsafe-dev-deps /workdir .

###
COPY --from=unsafe-dev-deps /yarn /yarn

FROM node:14-alpine AS dev-deps
RUN yarn config set cache-folder /yarn

WORKDIR /workdir
###

COPY --from=unsafe-dev-deps /workdir .
FROM dev-deps AS deps

RUN yarn install --ignore-optional --ignore-scripts --non-interactive --offline --production

###

Expand Down
6 changes: 6 additions & 0 deletions template/lambda-sqs-worker-cdk/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ COPY package.json yarn.lock ./

ARG NPM_READ_TOKEN

RUN yarn config set cache-folder /yarn

RUN \
echo '//registry.npmjs.org/:_authToken=${NPM_READ_TOKEN}' > .npmrc && \
yarn install --frozen-lockfile --ignore-optional --non-interactive && \
Expand All @@ -20,3 +22,7 @@ FROM node:14-alpine AS dev-deps
WORKDIR /workdir

COPY --from=unsafe-dev-deps /workdir .

COPY --from=unsafe-dev-deps /yarn /yarn

RUN yarn config set cache-folder /yarn
6 changes: 6 additions & 0 deletions template/lambda-sqs-worker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ COPY package.json yarn.lock ./

ARG NPM_READ_TOKEN

RUN yarn config set cache-folder /yarn

RUN \
echo '//registry.npmjs.org/:_authToken=${NPM_READ_TOKEN}' > .npmrc && \
yarn install --frozen-lockfile --ignore-optional --non-interactive && \
Expand All @@ -20,6 +22,10 @@ WORKDIR /workdir

COPY --from=unsafe-dev-deps /workdir .

COPY --from=unsafe-dev-deps /yarn /yarn

RUN yarn config set cache-folder /yarn

###

FROM dev-deps AS build
Expand Down

0 comments on commit 50792c8

Please sign in to comment.