1 import { Table, Note, CodeTabs, CodeTab, Prerequisites } from "docs-ui"
2
3 export const metadata = {
4 title: `local Command - Medusa Cloud CLI Reference`,
5 }
6
7 # {metadata.title}
8
9 In this guide, you'll learn how to reproduce a Cloud build on your machine using the CLI. This is useful for debugging build failures without pushing changes and waiting for a Cloud [deployment](../deployments/page.mdx).
10
11 <Note>
12
13 This command is available starting Medusa mcloud CLI v0.1.10. Make sure to update your CLI to the latest version if you don't see this command.
14
15 </Note>
16
17 ## local build
18
19 <Prerequisites
20 items={[
21 {
22 text: "This command builds a Docker image locally, so you must have Docker installed and running.",
23 link: "https://docs.docker.com/get-docker/",
24 }
25 ]}
26 />
27
28 Run a Cloud build on your machine, mirroring how Cloud builds your project. Run the command from within your project.
29
30 By default, the command builds the backend. Pass `--type storefront` to build the storefront instead.
31
32 The CLI infers the project's root path and environment variables from the linked Cloud project and environment, so it reproduces the Cloud build as closely as possible.
33
34 <CodeTabs group="cli-auth">
35 <CodeTab label="With Flags" value="flags">
36
37 ```bash
38 mcloud local build \
39 --organization org_123 \
40 --project proj_123 \
41 --environment env_123
42 ```
43
44 </CodeTab>
45 <CodeTab label="Using mcloud use" value="use">
46
47 ```bash
48 # Run once: mcloud use --organization org_123 --project proj_123 --environment env_123
49
50 mcloud local build
51 ```
52
53 </CodeTab>
54 </CodeTabs>
55
56 The command streams the build progress to your terminal. It prepares a build directory, generates a Dockerfile that mirrors the Cloud build, then builds the Docker image. The output looks similar to the following:
57
58 ```bash
59 Running local backend build in /tmp/mcloud-local-build/myuser-my-repo/backend
60 Generating Dockerfile
61 #1 [internal] load build definition from Dockerfile
62 #1 transferring dockerfile: 2.34kB done
63 #1 DONE 0.0s
64
65 # ... Docker build output for each layer ...
66
67 #24 exporting to image
68 #24 exporting layers done
69 #24 writing image sha256:0a1b2c3d4e5f done
70 #24 naming to docker.io/library/my-repo:cloud-local-build-a1b2c3d done
71 #24 DONE 1.2s
72 ```
73
74 If the build succeeds, the resulting image is tagged `<repository-name>:cloud-local-build-<commit-hash>`. If it fails, the command exits with the failing command's error so you can debug it the same way you would a Cloud build.
75
76 <Note>
77
78 Before the build starts, the CLI may print warnings if your installed CLI version is outdated, or if the current repository or branch doesn't match the linked Cloud project and environment. These are warnings only and don't stop the build.
79
80 If your repository does not have a remote origin configured, the CLI falls back to using the local folder name as the repository name.
81
82 </Note>
83
84 ### Override Environment Variables
85
86 To build with a local `.env` file instead of the Cloud environment's variables, or to override specific variables, use `--env-file` or `--var`:
87
88 ```bash
89 mcloud local build \
90 --env-file .env \
91 -v NODE_ENV=production
92 ```
93
94 ### Build the Storefront
95
96 To build the storefront instead of the backend, pass `--type storefront` (or `-t storefront`). The CLI infers the storefront's path from the linked Cloud project, or you can set it with `--storefront-path`:
97
98 ```bash
99 mcloud local build \
100 --type storefront \
101 --storefront-path apps/storefront
102 ```
103
104 The command builds the storefront in a Docker container and writes the build output to a local directory. When it finishes, it prints the output path:
105
106 ```bash
107 Running local storefront build in /tmp/mcloud-local-build/myuser-my-repo/storefront
108
109 # ... Docker build output ...
110
111 Storefront build complete. Output: /tmp/mcloud-local-build/myuser-my-repo/storefront/output
112 ```
113
114 ### Options
115
116 <Table>
117 <Table.Header>
118 <Table.Row>
119 <Table.HeaderCell>
120 Option
121 </Table.HeaderCell>
122 <Table.HeaderCell>
123 Description
124 </Table.HeaderCell>
125 <Table.HeaderCell>
126 Required
127 </Table.HeaderCell>
128 <Table.HeaderCell>
129 Default
130 </Table.HeaderCell>
131 </Table.Row>
132 </Table.Header>
133 <Table.Body>
134 <Table.Row>
135 <Table.Cell>
136
137 `-o <id>`, `--organization <id>`
138
139 </Table.Cell>
140 <Table.Cell>
141
142 The [ID of the organization](../organizations/page.mdx#find-organization-id) that the project belongs to.
143
144 </Table.Cell>
145 <Table.Cell>
146
147 No
148
149 </Table.Cell>
150 <Table.Cell>
151
152 Falls back to the organization in the [active context](../use/page.mdx), if set.
153
154 </Table.Cell>
155 </Table.Row>
156 <Table.Row>
157 <Table.Cell>
158
159 `-p <id-or-handle>`, `--project <id-or-handle>`
160
161 </Table.Cell>
162 <Table.Cell>
163
164 The [ID or handle of the project](../projects/page.mdx#find-project-ids-and-handles) to build.
165
166 </Table.Cell>
167 <Table.Cell>
168
169 No
170
171 </Table.Cell>
172 <Table.Cell>
173
174 Falls back to the project in the [active context](../use/page.mdx), if set.
175
176 </Table.Cell>
177 </Table.Row>
178 <Table.Row>
179 <Table.Cell>
180
181 `-e <handle>`, `--environment <handle>`
182
183 </Table.Cell>
184 <Table.Cell>
185
186 The [handle of the environment](../environments/page.mdx#find-environment-handles) whose variables are used for the build.
187
188 </Table.Cell>
189 <Table.Cell>
190
191 No
192
193 </Table.Cell>
194 <Table.Cell>
195
196 Falls back to the environment in the [active context](../use/page.mdx), if set.
197
198 </Table.Cell>
199 </Table.Row>
200 <Table.Row>
201 <Table.Cell>
202
203 `-t <type>`, `--type <type>`
204
205 </Table.Cell>
206 <Table.Cell>
207
208 The build type to run. Accepts `backend` or `storefront`.
209
210 </Table.Cell>
211 <Table.Cell>
212
213 No
214
215 </Table.Cell>
216 <Table.Cell>
217
218 `backend`
219
220 </Table.Cell>
221 </Table.Row>
222 <Table.Row>
223 <Table.Cell>
224
225 `--root-path <path>`
226
227 </Table.Cell>
228 <Table.Cell>
229
230 The root path of the Medusa project relative to the repository root. Used for backend builds. Inferred from the Cloud project if not specified, or `.` if a Cloud project isn't found.
231
232 </Table.Cell>
233 <Table.Cell>
234
235 No
236
237 </Table.Cell>
238 <Table.Cell>
239
240 Inferred
241
242 </Table.Cell>
243 </Table.Row>
244 <Table.Row>
245 <Table.Cell>
246
247 `--storefront-path <path>`
248
249 </Table.Cell>
250 <Table.Cell>
251
252 The path to the storefront project relative to the repository root. Used for storefront builds. Inferred from the Cloud project if not specified.
253
254 </Table.Cell>
255 <Table.Cell>
256
257 No
258
259 </Table.Cell>
260 <Table.Cell>
261
262 Inferred
263
264 </Table.Cell>
265 </Table.Row>
266 <Table.Row>
267 <Table.Cell>
268
269 `--env-file <path>`
270
271 </Table.Cell>
272 <Table.Cell>
273
274 Path to a `.env` file to use for the build. If not specified, environment variables are inferred from the Cloud environment.
275
276 </Table.Cell>
277 <Table.Cell>
278
279 No
280
281 </Table.Cell>
282 <Table.Cell>
283
284 Inferred
285
286 </Table.Cell>
287 </Table.Row>
288 <Table.Row>
289 <Table.Cell>
290
291 `-v <var>`, `--var <var>`
292
293 </Table.Cell>
294 <Table.Cell>
295
296 An environment variable to override for the build, in `KEY=VALUE` format. Pass it multiple times to override more than one variable.
297
298 </Table.Cell>
299 <Table.Cell>
300
301 No
302
303 </Table.Cell>
304 <Table.Cell>
305
306 \-
307
308 </Table.Cell>
309 </Table.Row>
310 <Table.Row>
311 <Table.Cell>
312
313 `--docker-cache`
314
315 </Table.Cell>
316 <Table.Cell>
317
318 Enable the Docker build cache. Disabled by default so that environment variable changes always invalidate the cache.
319
320 </Table.Cell>
321 <Table.Cell>
322
323 No
324
325 </Table.Cell>
326 <Table.Cell>
327
328 `false`
329
330 </Table.Cell>
331 </Table.Row>
332 </Table.Body>
333 </Table>