Skip to content

Commit

Permalink
Update distroless nodejs image (#1224)
Browse files Browse the repository at this point in the history
* use nodejs18-debian11 image

* Add changeset

* patch dockerfile automatically

* add changeset

* Tweak changesets

---------

Co-authored-by: Ryan Ling <[email protected]>
  • Loading branch information
samchungy and 72636c authored Jul 25, 2023
1 parent 49f43e4 commit 884f3f6
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-swans-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'skuba': patch
---

template/\*-rest-api: Switch distroless image from `nodejs:18` to `nodejs18-debian11`
7 changes: 7 additions & 0 deletions .changeset/wild-plums-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'skuba': minor
---

format, lint: Switch distroless image from `nodejs` to `nodejs-debian11`

`skuba format` and `skuba lint` will now automatically switch your `gcr.io/distroless/nodejs:18` image to `gcr.io/distroless/nodejs18-debian11`. This is now the [recommended](https://github.com/GoogleContainerTools/distroless/blob/main/nodejs/README.md) base image for Node.js.
8 changes: 4 additions & 4 deletions docs/deep-dives/arm64.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ Replace the relevant `--platform` values in your Dockerfile(s),
then ensure that you run your builds on AMD64 hardware:

```diff
- FROM --platform=arm64 gcr.io/distroless/nodejs:18 AS runtime
+ FROM --platform=${BUILDPLATFORM:-amd64} gcr.io/distroless/nodejs:18 AS runtime
- FROM --platform=arm64 gcr.io/distroless/nodejs18-debian11 AS runtime
+ FROM --platform=${BUILDPLATFORM:-amd64} gcr.io/distroless/nodejs18-debian11 AS runtime
- FROM --platform=${BUILDPLATFORM:-arm64} gcr.io/distroless/nodejs:18 AS runtime
+ FROM --platform=${BUILDPLATFORM:-amd64} gcr.io/distroless/nodejs:18 AS runtime
- FROM --platform=${BUILDPLATFORM:-arm64} gcr.io/distroless/nodejs18-debian11 AS runtime
+ FROM --platform=${BUILDPLATFORM:-amd64} gcr.io/distroless/nodejs18-debian11 AS runtime
```

For a [Gantry] service, modify the `cpuArchitecture` property in your `gantry.build.yml` and `gantry.apply.yml` resource files:
Expand Down
44 changes: 44 additions & 0 deletions src/cli/configure/patchDockerfile.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import memfs, { vol } from 'memfs';

import { tryPatchDockerfile } from './patchDockerfile';

jest.mock('fs-extra', () => memfs);

const volToJson = () => vol.toJSON(process.cwd(), undefined, true);

beforeEach(jest.clearAllMocks);
beforeEach(() => vol.reset());

const dockerfile = `
ARG BASE_IMAGE
ARG BASE_TAG
FROM --platform=\${BUILDPLATFORM:-<%- platformName %>} gcr.io/distroless/nodejs:18 AS runtime
WORKDIR /workdir
`;

it('patches an existing Dockerfile', async () => {
vol.fromJSON({ Dockerfile: dockerfile });

await expect(tryPatchDockerfile()).resolves.toBeUndefined();

expect(volToJson()).toMatchInlineSnapshot(`
{
"Dockerfile": "
ARG BASE_IMAGE
ARG BASE_TAG
FROM --platform=\${BUILDPLATFORM:-<%- platformName %>} gcr.io/distroless/nodejs18-debian11 AS runtime
WORKDIR /workdir
",
}
`);
});

it('ignores when a Dockerfile is missing', async () => {
vol.fromJSON({});

await expect(tryPatchDockerfile()).resolves.toBeUndefined();
});
38 changes: 38 additions & 0 deletions src/cli/configure/patchDockerfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { inspect } from 'util';

import fs from 'fs-extra';

import { log } from '../../utils/logging';

import { createDestinationFileReader } from './analysis/project';

const DOCKERFILE_FILENAME = 'Dockerfile';

const VERSION_REGEX = /gcr.io\/distroless\/nodejs:(16|18|20)/g;
const VERSION_DEBIAN_REPLACE = 'gcr.io/distroless/nodejs$1-debian11';

const patchDockerfile = async (dir: string) => {
const readFile = createDestinationFileReader(dir);

const maybeDockerfile = await readFile(DOCKERFILE_FILENAME);

if (!maybeDockerfile) {
return;
}

const patched = maybeDockerfile.replaceAll(
VERSION_REGEX,
VERSION_DEBIAN_REPLACE,
);

await fs.promises.writeFile(DOCKERFILE_FILENAME, patched);
};

export const tryPatchDockerfile = async (dir = process.cwd()) => {
try {
await patchDockerfile(dir);
} catch (err) {
log.warn('Failed to patch Dockerfile.');
log.subtle(inspect(err));
}
};
2 changes: 2 additions & 0 deletions src/cli/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createLogger, log } from '../utils/logging';
import { runESLint } from './adapter/eslint';
import { runPrettier } from './adapter/prettier';
import { tryAddEmptyExports } from './configure/addEmptyExports';
import { tryPatchDockerfile } from './configure/patchDockerfile';
import { tryPatchRenovateConfig } from './configure/patchRenovateConfig';
import { tryPatchServerListener } from './configure/patchServerListener';
import { tryRefreshIgnoreFiles } from './configure/refreshIgnoreFiles';
Expand All @@ -14,6 +15,7 @@ export const format = async (args = process.argv.slice(2)): Promise<void> => {
await Promise.all([
tryAddEmptyExports(),
tryPatchRenovateConfig(),
tryPatchDockerfile(),
tryPatchServerListener(),
tryRefreshIgnoreFiles(),
]);
Expand Down
2 changes: 1 addition & 1 deletion template/express-rest-api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ RUN yarn build

###

FROM --platform=${BUILDPLATFORM:-<%- platformName %>} gcr.io/distroless/nodejs:18 AS runtime
FROM --platform=${BUILDPLATFORM:-<%- platformName %>} gcr.io/distroless/nodejs18-debian11 AS runtime

WORKDIR /workdir

Expand Down
2 changes: 1 addition & 1 deletion template/koa-rest-api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ RUN yarn build

###

FROM --platform=${BUILDPLATFORM:-<%- platformName %>} gcr.io/distroless/nodejs:18 AS runtime
FROM --platform=${BUILDPLATFORM:-<%- platformName %>} gcr.io/distroless/nodejs18-debian11 AS runtime

WORKDIR /workdir

Expand Down

0 comments on commit 884f3f6

Please sign in to comment.