Skip to content

Commit 098c091

Browse files
committed
feat(docker): support inferring additional args for targets with interpolation support
1 parent a24f416 commit 098c091

File tree

6 files changed

+868
-66
lines changed

6 files changed

+868
-66
lines changed

astro-docs/src/content/docs/technologies/build-tools/docker/introduction.mdoc

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,129 @@ The `@nx/docker` plugin is configured in the `plugins` array in `nx.json`.
5858

5959
The `buildTarget` and `runTarget` options control the names of the inferred Docker tasks. The default names are `docker:build` and `docker:run`.
6060

61+
### Advanced Plugin Configuration
62+
63+
The `buildTarget` and `runTarget` options can be configured as objects to provide additional customization beyond just naming the targets. This allows you to pass custom arguments, set environment variables, and use pattern interpolation.
64+
65+
```json
66+
// nx.json
67+
{
68+
"plugins": [
69+
{
70+
"plugin": "@nx/docker",
71+
"options": {
72+
"buildTarget": {
73+
"name": "docker:build",
74+
"args": [
75+
"--platform",
76+
"linux/amd64,linux/arm64",
77+
"--label",
78+
"project={projectName}"
79+
],
80+
"env": {
81+
"DOCKER_BUILDKIT": "1"
82+
},
83+
"envFile": ".env.docker",
84+
"cwd": "{projectRoot}/docker"
85+
},
86+
"runTarget": {
87+
"name": "docker:run",
88+
"args": ["--rm"],
89+
"env": {
90+
"NODE_ENV": "production"
91+
}
92+
}
93+
}
94+
}
95+
]
96+
}
97+
```
98+
99+
#### Configuration Properties
100+
101+
When using the object format, the following properties are available:
102+
103+
- **`name`** (string, required): The name of the inferred target
104+
- **`args`** (string[], optional): Additional arguments to pass to the Docker command. Supports pattern interpolation.
105+
- **`env`** (object, optional): Environment variables to set when running the Docker command. Values support pattern interpolation.
106+
- **`envFile`** (string, optional): Path to an environment file to load
107+
- **`cwd`** (string, optional): Working directory for the command. Supports pattern interpolation.
108+
109+
#### Pattern Interpolation
110+
111+
All string values in the configuration support pattern interpolation using the following tokens:
112+
113+
- **`{projectName}`** - The name of the project from `project.json` or `package.json`
114+
- **`{projectRoot}`** - The root directory of the project (e.g., `apps/api`)
115+
- **`{imageRef}`** - The default image reference derived from the project root (e.g., `apps-api`)
116+
- **`{currentDate}`** - Current date in ISO format (e.g., `2025-01-30T14:30:00.000Z`)
117+
- **`{currentDate|FORMAT}`** - Current date with custom formatting (e.g., `{currentDate|YYYY.MM.DD}` → `2025.01.30`)
118+
- **`{commitSha}`** - Full Git commit SHA
119+
- **`{shortCommitSha}`** - First 7 characters of the commit SHA
120+
- **`{env.VAR_NAME}`** - Environment variable values (e.g., `{env.BUILD_NUMBER}`)
121+
122+
Date formatting supports these tokens: `YYYY` (year), `YY` (2-digit year), `MM` (month), `DD` (day), `HH` (hours), `mm` (minutes), `ss` (seconds).
123+
124+
#### Example: Multi-Platform Build with Metadata
125+
126+
Here's a practical example that builds Docker images for multiple platforms and includes build metadata:
127+
128+
```json
129+
// nx.json
130+
{
131+
"plugins": [
132+
{
133+
"plugin": "@nx/docker",
134+
"options": {
135+
"buildTarget": {
136+
"name": "build",
137+
"args": [
138+
"--platform",
139+
"linux/amd64,linux/arm64",
140+
"--label",
141+
"org.opencontainers.image.title={projectName}",
142+
"--label",
143+
"org.opencontainers.image.source={projectRoot}",
144+
"--label",
145+
"org.opencontainers.image.revision={commitSha}",
146+
"--label",
147+
"org.opencontainers.image.created={currentDate}",
148+
"--build-arg",
149+
"BUILD_DATE={currentDate|YYYY-MM-DD}",
150+
"--build-arg",
151+
"VERSION={env.VERSION}",
152+
"--tag",
153+
"{imageRef}:{currentDate|YYMMDD}.{shortCommitSha}"
154+
],
155+
"env": {
156+
"DOCKER_BUILDKIT": "1",
157+
"IMAGE_NAME": "{imageRef}"
158+
}
159+
},
160+
"runTarget": {
161+
"name": "run",
162+
"args": [
163+
"--rm",
164+
"--publish",
165+
"3000:3000",
166+
"--env",
167+
"NODE_ENV=production"
168+
]
169+
}
170+
}
171+
}
172+
]
173+
}
174+
```
175+
176+
This configuration:
177+
178+
- Builds for both AMD64 and ARM64 architectures
179+
- Adds OpenContainers metadata labels with project information
180+
- Tags images with a calendar version and commit SHA (e.g., `my-app:250130.abc1234`)
181+
- Enables Docker BuildKit for improved build performance
182+
- Injects build arguments and environment variables from the CI/CD pipeline
183+
61184
## Using Nx Release to Publish Docker Images
62185

63186
The `@nx/docker` plugin provides a `nx-release-publish` target for publishing docker images to a registry.

0 commit comments

Comments
 (0)