-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5140 from jsternberg/lint-constant-platform-disal…
…lowed checks: add check for constant in from platform flag
- Loading branch information
Showing
6 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
frontend/dockerfile/docs/rules/from-platform-flag-const-disallowed.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
title: FromPlatformFlagConstDisallowed | ||
description: FROM --platform flag should not use a constant value | ||
aliases: | ||
- /go/dockerfile/rule/from-platform-flag-const-disallowed/ | ||
--- | ||
|
||
## Output | ||
|
||
```text | ||
FROM --platform flag should not use constant value "linux/amd64" | ||
``` | ||
|
||
## Description | ||
|
||
Specifying `--platform` in the Dockerfile `FROM` instruction forces the image to build on only one target platform. This prevents building a multi-platform image from this Dockerfile and you must build on the same platform as specified in `--platform`. | ||
|
||
The recommended approach is to: | ||
|
||
* Omit `FROM --platform` in the Dockerfile and use the `--platform` argument on the command line. | ||
* Use `$BUILDPLATFORM` or some other combination of variables for the `--platform` argument. | ||
* Stage name should include the platform, OS, or architecture name to indicate that it only contains platform-specific instructions. | ||
|
||
## Examples | ||
|
||
❌ Bad: using a constant argument for `--platform` | ||
|
||
```dockerfile | ||
FROM --platform=linux/amd64 alpine AS base | ||
RUN apk add --no-cache git | ||
``` | ||
|
||
✅ Good: using the default platform | ||
|
||
```dockerfile | ||
FROM alpine AS base | ||
RUN apk add --no-cache git | ||
``` | ||
|
||
✅ Good: using a meta variable | ||
|
||
```dockerfile | ||
FROM --platform=${BUILDPLATFORM} alpine AS base | ||
RUN apk add --no-cache git | ||
``` | ||
|
||
✅ Good: used in a multi-stage build with a target architecture | ||
|
||
```dockerfile | ||
FROM --platform=linux/amd64 alpine AS build_amd64 | ||
... | ||
|
||
FROM --platform=linux/arm64 alpine AS build_arm64 | ||
... | ||
|
||
FROM build_${TARGETARCH} AS build | ||
... | ||
``` | ||
|
51 changes: 51 additions & 0 deletions
51
frontend/dockerfile/linter/docs/FromPlatformFlagConstDisallowed.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
## Output | ||
|
||
```text | ||
FROM --platform flag should not use constant value "linux/amd64" | ||
``` | ||
|
||
## Description | ||
|
||
Specifying `--platform` in the Dockerfile `FROM` instruction forces the image to build on only one target platform. This prevents building a multi-platform image from this Dockerfile and you must build on the same platform as specified in `--platform`. | ||
|
||
The recommended approach is to: | ||
|
||
* Omit `FROM --platform` in the Dockerfile and use the `--platform` argument on the command line. | ||
* Use `$BUILDPLATFORM` or some other combination of variables for the `--platform` argument. | ||
* Stage name should include the platform, OS, or architecture name to indicate that it only contains platform-specific instructions. | ||
|
||
## Examples | ||
|
||
❌ Bad: using a constant argument for `--platform` | ||
|
||
```dockerfile | ||
FROM --platform=linux/amd64 alpine AS base | ||
RUN apk add --no-cache git | ||
``` | ||
|
||
✅ Good: using the default platform | ||
|
||
```dockerfile | ||
FROM alpine AS base | ||
RUN apk add --no-cache git | ||
``` | ||
|
||
✅ Good: using a meta variable | ||
|
||
```dockerfile | ||
FROM --platform=${BUILDPLATFORM} alpine AS base | ||
RUN apk add --no-cache git | ||
``` | ||
|
||
✅ Good: used in a multi-stage build with a target architecture | ||
|
||
```dockerfile | ||
FROM --platform=linux/amd64 alpine AS build_amd64 | ||
... | ||
|
||
FROM --platform=linux/arm64 alpine AS build_arm64 | ||
... | ||
|
||
FROM build_${TARGETARCH} AS build | ||
... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters