1 import {
2 Prerequisites,
3 CodeTabs,
4 CodeTab,
5 } from "docs-ui"
6 import Feedback from "@/components/Feedback"
7
8 export const metadata = {
9 title: `${pageNumber} Install Medusa with Docker`,
10 }
11
12 # {metadata.title}
13
14 In this chapter, you'll learn how to install and run a Medusa application using Docker.
15
16 The main supported installation method is using [create-medusa-app](../page.mdx). However, since it requires prerequisites like PostgreSQL, Docker may be a preferred and easier option for some users. You can follow this guide to set up a Medusa application with Docker in this case.
17
18 You can follow this guide on any operating system that supports Docker, including Windows, macOS, and Linux.
19
20 <Prerequisites
21 items={[
22 {
23 text: "Docker",
24 link: "https://docs.docker.com/get-docker/"
25 },
26 {
27 text: "Docker Compose",
28 link: "https://docs.docker.com/compose/install/"
29 },
30 {
31 text: "Git CLI tool",
32 link: "https://git-scm.com/downloads"
33 },
34 ]} />
35
36 ## 1. Clone the DTC Starter Repository
37
38 To get started, clone the DTC Starter repository:
39
40 ```bash
41 git clone https://github.com/medusajs/dtc-starter.git --depth=1 my-medusa-store
42 cd my-medusa-store
43 ```
44
45 This command clones the repository into a directory named `my-medusa-store`.
46
47 The rest of the steps are performed inside this directory.
48
49 ### Change Package Manager
50
51 The project uses pnpm as the package manager in a monorepo workspace setup, as declared in `package.json`. This demonstrates a working pnpm monorepo configuration for Medusa.
52
53 To use a different package manager:
54
55 1. Change the `packageManager` field in `package.json` to your preferred package manager, such as `npm@{version}` or `yarn@{version}`, where `{version}` is the version of the package manager you have installed.
56 2. Add a `workspaces` field in `package.json` with the following content:
57
58 ```json title="package.json"
59 {
60 "workspaces": [
61 "apps/*"
62 ]
63 }
64 ```
65
66 3. Remove the `node_modules` directory and `pnpm-lock.yaml` and `pnpm-workspace.yaml` files.
67 4. If you're using `yarn`, add a `.yarnrc.yml` file with the following content:
68
69 ```yaml title=".yarnrc.yml"
70 nodeLinker: node-modules
71 ```
72
73 5. If you're using `npm`, add an `overrides` field in `package.json`:
74
75 ```json title="package.json"
76 {
77 "overrides": {
78 "ajv": "^8.0.0"
79 }
80 }
81 ```
82
83 6. Install dependencies with your preferred package manager to generate the appropriate lock file (`package-lock.json` for npm or `yarn.lock` for Yarn).
84
85 ---
86
87 ## 2. Create `docker-compose.yml`
88
89 In the cloned repository, create a file named `docker-compose.yml` with the following content:
90
91 ```yaml title="docker-compose.yml"
92 services:
93 # PostgreSQL Database
94 postgres:
95 image: postgres:15-alpine
96 container_name: medusa_postgres
97 restart: unless-stopped
98 environment:
99 POSTGRES_DB: medusa-store
100 POSTGRES_USER: postgres
101 POSTGRES_PASSWORD: postgres
102 ports:
103 - "5432:5432"
104 volumes:
105 - postgres_data:/var/lib/postgresql/data
106 networks:
107 - medusa_network
108
109 # Redis
110 redis:
111 image: redis:7-alpine
112 container_name: medusa_redis
113 restart: unless-stopped
114 ports:
115 - "6379:6379"
116 networks:
117 - medusa_network
118
119 # Medusa Server
120 # This service runs the Medusa backend application
121 # and the admin dashboard.
122 medusa:
123 build: .
124 container_name: medusa_backend
125 restart: unless-stopped
126 depends_on:
127 - postgres
128 - redis
129 ports:
130 - "9000:9000"
131 - "5173:5173"
132 environment:
133 - NODE_ENV=development
134 - DATABASE_URL=postgres://postgres:postgres@postgres:5432/medusa-store
135 - REDIS_URL=redis://redis:6379
136 env_file:
137 - apps/backend/.env
138 volumes:
139 - .:/server
140 - /server/node_modules
141 - /server/apps/backend/node_modules
142 networks:
143 - medusa_network
144
145 # Storefront
146 # This service runs the Next.js Starter Storefront
147 storefront:
148 build: .
149 container_name: medusa_storefront
150 restart: unless-stopped
151 depends_on:
152 - medusa
153 ports:
154 - "8000:8000"
155 environment:
156 - NEXT_PUBLIC_MEDUSA_BACKEND_URL=http://medusa:9000
157 env_file:
158 - apps/storefront/.env
159 volumes:
160 - .:/server
161 - /server/node_modules
162 - /server/apps/backend/node_modules
163 - /server/apps/storefront/node_modules
164 - /server/apps/storefront/.next
165 entrypoint: ["./start-storefront.sh"]
166 networks:
167 - medusa_network
168
169 volumes:
170 postgres_data:
171
172 networks:
173 medusa_network:
174 driver: bridge
175 ```
176
177 You define four services in this file:
178
179 - `postgres`: The PostgreSQL database service that stores your Medusa application's data.
180 - `redis`: The Redis service that stores session data.
181 - `medusa`: The Medusa service that runs the server and the admin dashboard. It connects to the PostgreSQL and Redis services.
182 - `storefront`: The Next.js Starter Storefront service. It connects to the Medusa service.
183 - If you don't want to use the storefront, you can remove the `apps/storefront` directory and the `storefront` service from the `docker-compose.yml` file.
184
185 The `env_file` paths are relative to the project root: `apps/backend/.env` for the backend and `apps/storefront/.env` for the storefront.
186
187 The anonymous volumes for `node_modules` directories are required because pnpm workspaces create per-package `node_modules` directories with symlinks. Without these volumes, the Docker bind mount overwrites the installed dependencies. This configuration is essential for pnpm to work correctly in a monorepo Docker setup.
188
189 ### Port Configuration Explained
190
191 The ports configuration maps container ports to host ports using the format `"host_port:container_port"`:
192
193 - `"5432:5432"`: PostgreSQL database accessible at `localhost:5432`
194 - `"6379:6379"`: Redis accessible at `localhost:6379`
195 - `"9000:9000"`: Medusa backend server accessible at `localhost:9000`
196 - `"5173:5173"`: Medusa Admin development server with Hot Module Replacement (HMR)
197 - `"8000:8000"`: Storefront accessible at `localhost:8000`
198
199 <Note>
200
201 If these ports conflict with other services on your host machine, change the host port (left side). For example, use `"9001:9000"` to access the Medusa backend at `localhost:9001` instead.
202
203 </Note>
204
205 ### Recommendations for Multiple Local Projects
206
207 If this isn't the first Medusa project you're setting up with Docker on your machine, make sure to:
208
209 - Change the `container_name` for each service to avoid conflicts.
210 - For example, use `medusa_postgres_myproject`, `medusa_redis_myproject`, `medusa_backend_myproject`, and `medusa_storefront_myproject` instead of the current names.
211 - Change the volume names to avoid conflicts.
212 - For example, use `postgres_data_myproject` instead of `postgres_data`.
213 - Change the network name to avoid conflicts.
214 - For example, use `medusa_network_myproject` instead of `medusa_network`.
215 - Change the ports to avoid conflicts with other projects. For example:
216 - Use `"5433:5432"` for PostgreSQL.
217 - Use `"6380:6379"` for Redis.
218 - Use `"9001:9000"` for the Medusa server.
219 - Use `"5174:5173"` for the Medusa Admin dashboard.
220 - Use `"8001:8000"` for the storefront.
221 - Update the `DATABASE_URL` and `REDIS_URL` environment variables accordingly.
222
223 ---
224
225 ## 3. Create `start.sh`
226
227 Create a script file that [runs database migrations](../../fundamentals/data-models/write-migration/page.mdx) and starts the Medusa development server.
228
229 Create the file `start.sh` with the following content:
230
231 <Note title="Windows Users">
232
233 Ensure that the `start.sh` file uses LF line endings instead of CRLF. Git on Windows can sometimes automatically convert line endings, which causes errors when running the script inside the Linux-based Docker container. Learn how to configure your environment to maintain LF line endings in [this guide from GitHub](https://docs.github.com/en/get-started/git-basics/configuring-git-to-handle-line-endings).
234
235 </Note>
236
237 <CodeTabs group="npm2yarn" defaultValue="pnpm">
238 <CodeTab value="pnpm" label="pnpm">
239 ```shell title="start.sh"
240 #!/bin/sh
241 cd /server/apps/backend
242
243 echo "Running database migrations..."
244 pnpm medusa db:migrate
245
246 echo "Seeding database..."
247 pnpm seed || echo "Seeding failed, continuing..."
248
249 echo "Starting Medusa development server..."
250 pnpm dev
251 ```
252 </CodeTab>
253 <CodeTab value="yarn" label="yarn">
254 ```shell title="start.sh"
255 #!/bin/sh
256 cd /server/apps/backend
257
258 echo "Running database migrations..."
259 yarn medusa db:migrate
260
261 echo "Seeding database..."
262 yarn seed || echo "Seeding failed, continuing..."
263
264 echo "Starting Medusa development server..."
265 yarn dev
266 ```
267 </CodeTab>
268 <CodeTab value="npm" label="npm">
269 ```shell title="start.sh"
270 #!/bin/sh
271 cd /server/apps/backend
272
273 echo "Running database migrations..."
274 npx medusa db:migrate
275
276 echo "Seeding database..."
277 npm run seed || echo "Seeding failed, continuing..."
278
279 echo "Starting Medusa development server..."
280 npm run dev
281 ```
282 </CodeTab>
283 </CodeTabs>
284
285 ---
286
287 ## 4. Create `start-storefront.sh`
288
289 Create a separate script for the storefront service:
290
291 <Note title="Windows Users">
292
293 Apply the same LF line endings requirement mentioned in the previous step to this file as well.
294
295 </Note>
296
297 <CodeTabs group="npm2yarn" defaultValue="pnpm">
298 <CodeTab value="pnpm" label="pnpm">
299 ```shell title="start-storefront.sh"
300 #!/bin/sh
301 cd /server/apps/storefront
302
303 echo "Starting Next.js Starter Storefront development server..."
304 pnpm dev
305 ```
306 </CodeTab>
307 <CodeTab value="yarn" label="yarn">
308 ```shell title="start-storefront.sh"
309 #!/bin/sh
310 cd /server/apps/storefront
311
312 echo "Starting Next.js Starter Storefront development server..."
313 yarn dev
314 ```
315 </CodeTab>
316 <CodeTab value="npm" label="npm">
317 ```shell title="start-storefront.sh"
318 #!/bin/sh
319 cd /server/apps/storefront
320
321 echo "Starting Next.js Starter Storefront development server..."
322 npm run dev
323 ```
324 </CodeTab>
325 </CodeTabs>
326
327 Make sure to give both scripts executable permissions:
328
329 <Note>
330
331 Setting permissions isn't necessary on Windows, but it's recommended to run this command on Unix-based systems like macOS and Linux.
332
333 </Note>
334
335 ```bash
336 chmod +x start.sh start-storefront.sh
337 ```
338
339 ---
340
341 ## 5. Create `Dockerfile`
342
343 The `Dockerfile` defines how the Docker image is built. Both the `medusa` and `storefront` services reuse the same image — the storefront overrides the entrypoint in `docker-compose.yml`.
344
345 Create a file named `Dockerfile` with the following content:
346
347 <CodeTabs group="npm2yarn" defaultValue="pnpm">
348 <CodeTab value="pnpm" label="pnpm">
349 ```dockerfile title="Dockerfile"
350 FROM node:20-alpine
351
352 WORKDIR /server
353
354 RUN npm install pnpm@10.11.1 -g
355
356 COPY package.json pnpm-lock.yaml pnpm-workspace.yaml turbo.json .npmrc ./
357 COPY apps/backend/package.json ./apps/backend/
358 COPY apps/storefront/package.json ./apps/storefront/
359
360 RUN pnpm install --frozen-lockfile
361
362 COPY . .
363
364 EXPOSE 9000 5173 8000
365
366 ENTRYPOINT ["./start.sh"]
367 ```
368 </CodeTab>
369 <CodeTab value="yarn" label="yarn">
370 ```dockerfile title="Dockerfile"
371 FROM node:20-alpine
372
373 WORKDIR /server
374
375 COPY package.json yarn.lock .yarnrc.yml turbo.json ./
376 COPY .yarn/releases .yarn/releases
377 COPY apps/backend/package.json ./apps/backend/
378 COPY apps/storefront/package.json ./apps/storefront/
379
380 RUN yarn install
381
382 COPY . .
383
384 EXPOSE 9000 5173 8000
385
386 ENTRYPOINT ["./start.sh"]
387 ```
388 </CodeTab>
389 <CodeTab value="npm" label="npm">
390 ```dockerfile title="Dockerfile"
391 FROM node:20-alpine
392
393 WORKDIR /server
394
395 COPY package.json package-lock.json turbo.json ./
396 COPY apps/backend/package.json ./apps/backend/
397 COPY apps/storefront/package.json ./apps/storefront/
398
399 RUN npm install --legacy-peer-deps
400
401 COPY . .
402
403 EXPOSE 9000 5173 8000
404
405 ENTRYPOINT ["./start.sh"]
406 ```
407 </CodeTab>
408 </CodeTabs>
409
410 In the `Dockerfile`, you use the `node:20-alpine` image as the base since Medusa requires Node.js v20.19.0 or later.
411
412 The workspace configuration files and all apps' `package.json` files are copied before installing dependencies so that the workspace dependency graph is resolved correctly. After installing dependencies, the rest of the source code is copied.
413
414 <Note>
415
416 While it's more common to use `/app` as the working directory, it's highly recommended to use `/server` for the Medusa service to avoid conflicts with Medusa Admin customizations. Learn more in [this troubleshooting guide](!resources!/troubleshooting/medusa-admin/no-widget-route#errors-in-docker).
417
418 </Note>
419
420 ---
421
422 ## 6. Update Scripts in `package.json`
423
424 Update the `scripts` section in the root `package.json` file to start and stop the Docker services.
425
426 Add the following scripts:
427
428 ```json title="package.json"
429 {
430 "scripts": {
431 // Other scripts...
432 "docker:up": "docker compose up --build -d",
433 "docker:down": "docker compose down"
434 }
435 }
436 ```
437
438 Where:
439
440 - `docker:up` starts the development server in a Docker container as a background process.
441 - `docker:down` stops and removes the Docker containers.
442
443 ---
444
445 ## 7. Update Medusa Configuration
446
447 ### Disable SSL for PostgreSQL Connection
448
449 If you try to run the Medusa application now with Docker, you'll encounter an SSL error as the server tries to connect to the PostgreSQL database.
450
451 To resolve the error, add the following configurations in `apps/backend/medusa-config.ts`:
452
453 ```ts title="apps/backend/medusa-config.ts"
454 import { loadEnv, defineConfig } from "@medusajs/framework/utils"
455
456 loadEnv(process.env.NODE_ENV || "development", process.cwd())
457
458 module.exports = defineConfig({
459 projectConfig: {
460 // ...
461 databaseDriverOptions: {
462 ssl: false,
463 sslmode: "disable",
464 },
465 },
466 })
467 ```
468
469 You add the [projectConfig.databaseDriverOptions](../../configurations/medusa-config/page.mdx#databasedriveroptions) to disable SSL for the PostgreSQL database connection.
470
471 ### Add Vite Configuration for Medusa Admin
472
473 To ensure that the Medusa Admin dashboard works correctly when running inside Docker, connects to the Medusa server, and reloads properly with Hot Module Replacement (HMR), add the following Vite configuration in `apps/backend/medusa-config.ts`:
474
475 ```ts title="apps/backend/medusa-config.ts"
476 module.exports = defineConfig({
477 // ...
478 admin: {
479 vite: (config) => {
480 return {
481 server: {
482 host: "0.0.0.0",
483 // Allow all hosts when running in Docker (development mode)
484 // In production, this should be more restrictive
485 allowedHosts: [
486 "localhost",
487 ".localhost",
488 "127.0.0.1",
489 ],
490 hmr: {
491 // HMR websocket port inside container
492 port: 5173,
493 // Port browser connects to (exposed in docker-compose.yml)
494 clientPort: 5173,
495 },
496 },
497 }
498 },
499 },
500 })
501 ```
502
503 You configure the Vite development server to listen on all network interfaces (`0.0.0.0`) and set up HMR to work correctly within the Docker environment.
504
505 ### Redis Configuration
506
507 If you encounter Redis connection issues, you may need to explicitly configure the Redis URL in your `medusa-config.ts` file:
508
509 ```ts title="apps/backend/medusa-config.ts"
510 module.exports = defineConfig({
511 projectConfig: {
512 // ...
513 redisUrl: process.env.REDIS_URL,
514 },
515 })
516 ```
517
518 This ensures the Redis URL from the environment variable is properly used by Medusa.
519
520 ---
521
522 ## 8. Add `.dockerignore`
523
524 To ensure only the necessary files are copied into the Docker image, create a `.dockerignore` file with the following content:
525
526 ```dockerignore title=".dockerignore"
527 node_modules
528 apps/backend/node_modules
529 apps/storefront/node_modules
530 apps/backend/.medusa
531 apps/storefront/.next
532 npm-debug.log*
533 yarn-debug.log*
534 yarn-error.log*
535 .git
536 .gitignore
537 README.md
538 .env.test
539 .nyc_output
540 coverage
541 .DS_Store
542 *.log
543 dist
544 build
545 ```
546
547 ---
548
549 ## 9. Create `.env` Files
550
551 Create environment files for both the backend and storefront by copying from their templates.
552
553 Copy the backend template:
554
555 ```bash
556 cp apps/backend/.env.template apps/backend/.env
557 ```
558
559 Copy the storefront template:
560
561 ```bash
562 cp apps/storefront/.env.template apps/storefront/.env
563 ```
564
565 Update the values in each file as needed.
566
567 ---
568
569 ## 10. Start the Medusa Application with Docker
570
571 All configurations are now ready. Start the Medusa application using Docker by running the following command:
572
573 ```bash npm2yarn
574 npm run docker:up
575 ```
576
577 Docker pulls the necessary images, starts the PostgreSQL and Redis services, builds the Medusa and storefront services, and runs them in Docker containers.
578
579 Check the logs to ensure everything is running smoothly:
580
581 ```bash
582 docker compose logs -f
583 ```
584
585 Once you see the following message, the Medusa server and admin are ready:
586
587 ```shell
588 ✔ Server is ready on port: 9000 - 3ms
589 info: Admin URL → http://localhost:9000/app
590 ```
591
592 You can now access the Medusa server at `http://localhost:9000` and the Medusa Admin dashboard at `http://localhost:9000/app`.
593
594 <Feedback
595 question="Did the Docker installation finish successfully?"
596 negativeQuestion="What errors or issues did you run into?"
597 showDottedSeparator={false}
598 positiveBtn="Yes"
599 negativeBtn="No"
600 />
601
602 ---
603
604 ## Create Admin User and Configure Storefront
605
606 ### Create Admin User
607
608 To create an admin user, run the following command:
609
610 <CodeTabs group="npm2yarn" defaultValue="pnpm">
611 <CodeTab value="pnpm" label="pnpm">
612 ```bash
613 docker compose exec medusa sh -c "cd /server/apps/backend && pnpm medusa user -e admin@example.com -p supersecret"
614 ```
615 </CodeTab>
616 <CodeTab value="yarn" label="yarn">
617 ```bash
618 docker compose exec medusa sh -c "cd /server/apps/backend && yarn medusa user -e admin@example.com -p supersecret"
619 ```
620 </CodeTab>
621 <CodeTab value="npm" label="npm">
622 ```bash
623 docker compose exec medusa sh -c "cd /server/apps/backend && npx medusa user -e admin@example.com -p supersecret"
624 ```
625 </CodeTab>
626 </CodeTabs>
627
628 Replace `admin@example.com` and `supersecret` with your desired email and password.
629
630 You can now log in to the Medusa Admin dashboard at `http://localhost:9000/app`.
631
632 ### Set Publishable API Key for the Storefront
633
634 The storefront requires a publishable API key to connect to the Medusa backend:
635
636 1. Log in to the admin dashboard at `http://localhost:9000/app`.
637 2. Go to **Settings** > **API Key Management** and create a new Publishable API Key.
638 3. Add the key to `apps/storefront/.env`:
639
640 ```bash
641 NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=pk_...
642 ```
643
644 4. Restart the storefront service:
645
646 ```bash
647 docker compose up storefront -d
648 ```
649
650 <Note>
651
652 The storefront requires this key at startup and exits with an error if it's missing.
653
654 </Note>
655
656 ---
657
658 ## Stop the Medusa Application Running in Docker
659
660 To stop the Medusa application running in Docker, run the following command:
661
662 ```bash npm2yarn
663 npm run docker:down
664 ```
665
666 This command stops and removes the Docker containers created by the `docker-compose.yml` file.
667
668 This doesn't delete any data in your application or its database. Start the server again using the `docker:up` command.
669
670 ---
671
672 ## Check Logs
673
674 Check the logs of the Medusa backend running in Docker:
675
676 ```bash
677 docker compose logs -f medusa
678 ```
679
680 Check the logs of the storefront:
681
682 ```bash
683 docker compose logs -f storefront
684 ```
685
686 ---
687
688 ## Troubleshooting
689
690 ### start.sh Not Found Error
691
692 If you get the following error when starting the Medusa application with Docker:
693
694 ```bash
695 medusa_backend exited with code 127 (restarting)
696 medusa_backend | /usr/local/bin/docker-entrypoint.sh: exec: line 11: ./start.sh: not found
697 ```
698
699 This is a common error for Windows users. It usually occurs when the `start.sh` file uses CRLF line endings instead of LF.
700
701 To fix this, ensure that the `start.sh` file uses LF line endings. You can configure Git to maintain LF line endings by following [this guide from GitHub](https://docs.github.com/en/get-started/git-basics/configuring-git-to-handle-line-endings).
702
703 ### Couldn't Find X File or Directory Errors
704
705 If you encounter errors indicating that certain files or directories couldn't be found, make sure you're running the commands from the root directory of your Medusa application (the same directory where the `docker-compose.yml` file is located).
706
707 For example, if you run the `docker:up` command and see an error like this:
708
709 ```bash
710 error Couldn't find a package.json file in "/"
711 ```
712
713 Ensure that your terminal's current working directory is the root of your Medusa application, then try running the command again.
714
715 ### Container Name Conflicts
716
717 If you're running multiple Medusa projects with Docker or have previously run this guide, you may encounter container name conflicts.
718
719 To resolve this, ensure that the `container_name` values in your `docker-compose.yml` file are unique for each project. You can modify them by appending a unique identifier to each name.
720
721 For example, change:
722
723 ```yaml title="docker-compose.yml"
724 container_name: medusa_postgres
725 ```
726
727 to
728
729 ```yaml title="docker-compose.yml"
730 container_name: medusa_postgres_myproject
731 ```
732
733 ### Medusa Backend Returns "Cannot GET /" Error
734
735 If you access `http://localhost:9000` and see a "Cannot GET /" error, this usually means the Medusa backend started successfully but there's no route handler for the root path.
736
737 This is expected behavior. The Medusa backend API endpoints are available at paths like `/admin` and `/store`. To verify your installation:
738
739 1. Access the Medusa Admin dashboard at `http://localhost:9000/app`
740 2. Check the backend health at `http://localhost:9000/health`
741 3. Access the storefront at `http://localhost:8000` (if you deployed it)
742
743 The "Cannot GET /" message doesn't indicate an error - it just means there's no content at the root URL.
744
745 ---
746
747 ## Learn More about your Medusa Application
748
749 You can learn more about your Medusa application and its setup in the [Installation chapter](../page.mdx).