medusa-stack-dockerized icon indicating copy to clipboard operation
medusa-stack-dockerized copied to clipboard

Environment Variable Issue: NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY Not Set Properly During Build

Open SigmaSeven7 opened this issue 1 year ago • 9 comments

Steps taken before encountering the issue:

  1. ran 'npx create-dockerized-medusa-app'. prod mode, no seeding, all the default settings.
  2. cd output/proj-name
  3. docker-compose up -d

error:

=> ERROR [storefront builder 9/9] RUN corepack enable yarn & 2.8s

[storefront builder 9/9] RUN corepack enable yarn && NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=$(sh /app/create-publishable-key.sh -u http://127.0.0.1:9000 -e [email protected] -p supersecret) yarn run build: 0.756 ! Corepack is about to download https://repo.yarnpkg.com/3.2.3/packages/yarnpkg-cli/bin/yarn.js 2.730 2.730 🚫 Error: Missing required environment variables 2.730 2.732 NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY 2.732 Learn how to create a publishable key: https://docs.medusajs.com/v2/resources/storefront-development/publishable-api-keys
2.732 2.732 2.732 Please set these variables in your .env file or environment before starting the application. 2.732


failed to solve: process "/bin/sh -c corepack enable yarn && NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=$(sh /app/create-publishable-key.sh -u $MEDUSA_BACKEND_URL -e $EMAIL -p $PASSWORD) yarn run build" did not complete successfully: exit code: 1

SigmaSeven7 avatar Dec 14 '24 14:12 SigmaSeven7

Hello! Try to run the stack like this:

docker compose up postgres redis medusa_server medusa_worker -d && docker compose up storefront -d

The backend is required to be running before the storefront is built.

beakman avatar Dec 14 '24 14:12 beakman

C:\Users\Oryan\output\pixelsjourney2.0>docker compose up postgres redis medusa_server medusa_worker -d && docker compose up storefront -d [+] Running 5/5 ✔ Network pixelsjourney20_medusa-network Created 0.1s ✔ Container redis Healthy 11.6s ✔ Container postgres Healthy 11.6s ✔ Container medusa_server Started 11.0s ✔ Container medusa_worker Started 11.8s [+] Building 4.3s (25/30) docker:desktop-linux => [storefront internal] load build definition from Dockerfile.storefront.prod 0.0s => => transferring dockerfile: 2.31kB 0.0s => [storefront] resolve image config for docker-image://docker.io/docker/dockerfile:1 0.7s => CACHED [storefront] docker-image://docker.io/docker/dockerfile:1@sha256:db1ff77fb637a5955317c7a3a62540196396d 0.0s => [storefront internal] load build definition from Dockerfile.storefront.prod 0.0s => [storefront internal] load metadata for docker.io/library/node:20-alpine 0.5s => [storefront internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [storefront internal] load build context 0.0s => => transferring context: 155B 0.0s => [storefront base 1/4] FROM docker.io/library/node:20-alpine@sha256:426f843809ae05f324883afceebaa2b9cab9cb6970 0.0s => CACHED [storefront base 2/4] RUN echo "MEDUSA_BACKEND_URL=http://127.0.0.1:9000" >> .env 0.0s => CACHED [storefront base 3/4] RUN echo "NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=" >> .env 0.0s => CACHED [storefront base 4/4] WORKDIR /app 0.0s => CACHED [storefront builder 1/9] WORKDIR /app 0.0s => CACHED [storefront runner 2/8] RUN addgroup --system --gid 1001 nodejs 0.0s => CACHED [storefront runner 3/8] RUN adduser --system --uid 1001 nextjs 0.0s => CACHED [storefront deps 1/3] RUN apk add --no-cache libc6-compat git 0.0s => CACHED [storefront deps 2/3] RUN git clone https://github.com/medusajs/nextjs-starter-medusa.git /app 0.0s => CACHED [storefront deps 3/3] RUN corepack enable yarn && yarn install 0.0s => CACHED [storefront builder 2/9] COPY --from=deps /app/node_modules ./node_modules 0.0s => CACHED [storefront builder 3/9] COPY --from=deps /app ./ 0.0s => CACHED [storefront builder 4/9] RUN apk add --no-cache curl jq 0.0s => CACHED [storefront builder 5/9] COPY ./.env ./.env 0.0s => CACHED [storefront builder 6/9] COPY ./storefront.next.config.js ./next.config.js 0.0s => CACHED [storefront builder 7/9] COPY ./create-publishable-key.sh /app/create-publishable-key.sh 0.0s => CACHED [storefront builder 8/9] RUN chmod +x /app/create-publishable-key.sh 0.0s => ERROR [storefront builder 9/9] RUN corepack enable yarn && NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=$(sh /app/creat 2.9s

[storefront builder 9/9] RUN corepack enable yarn && NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=$(sh /app/create-publishable-key.sh -u http://127.0.0.1:9000 -e [email protected] -p supersecret) yarn run build: 0.784 ! Corepack is about to download https://repo.yarnpkg.com/3.2.3/packages/yarnpkg-cli/bin/yarn.js 2.809 2.809 🚫 Error: Missing required environment variables 2.809 2.810 NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY 2.811 Learn how to create a publishable key: https://docs.medusajs.com/v2/resources/storefront-development/publishable-api-keys 2.811 2.811 2.811 Please set these variables in your .env file or environment before starting the application. 2.811


failed to solve: process "/bin/sh -c corepack enable yarn && NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=$(sh /app/create-publishable-key.sh -u $MEDUSA_BACKEND_URL -e $EMAIL -p $PASSWORD) yarn run build" did not complete successfully: exit code: 1

C:\Users\Oryan\output\pixelsjourney2.0>

SigmaSeven7 avatar Dec 14 '24 15:12 SigmaSeven7

Can you try to do a

docker system prune --af --volumes

Also. Check if the backend started up correctly by going to http://localhost:9000/app

beakman avatar Dec 14 '24 15:12 beakman

Hey, first of all thanks a lot.

I ran - docker system prune --af --volumes followed by - docker compose up postgres redis medusa_server medusa_worker -d && docker compose up storefront -d

And got:

=> ERROR [storefront builder 9/9] RUN corepack enable yarn && NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=$(sh /app/crea 81.2s

[storefront builder 9/9] RUN corepack enable yarn && NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=$(sh /app/create-publishable-key.sh -u http://127.0.0.1:9000 -e [email protected] -p supersecret) yarn run build: 1.334 ! Corepack is about to download https://repo.yarnpkg.com/3.2.3/packages/yarnpkg-cli/bin/yarn.js 3.822 Attention: Next.js now collects completely anonymous telemetry regarding usage. 3.824 This information is used to shape Next.js' roadmap and prioritize features. 3.824 You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL: 3.824 https://nextjs.org/telemetry 3.824 3.917 ▲ Next.js 15.0.3 3.917 - Environments: .env 3.917 4.048 Creating an optimized production build ... 61.99 ✓ Compiled successfully 62.00 Skipping validation of types 62.00 Skipping linting 62.35 Collecting page data ... 63.54 Failed to generate static paths for product pages: Error setting up the request: A valid publishable key is required to proceed with the request. 63.56 Error: A valid publishable key is required to proceed with the request 63.56 at (/app/.next/server/chunks/911.js:1:77765) 63.56 at Generator.next () 63.56 at s (/app/.next/server/chunks/911.js:1:76729) { 63.56 statusText: 'Bad Request', 63.56 status: 400 63.56 } 63.57 63.57 > Build error occurred 63.58 Error: Failed to collect page data for /[countryCode]/categories/[...category] 63.58 at (/app/node_modules/next/dist/build/utils.js:1234:15) { 63.58 type: 'Error' 63.58 }


failed to solve: process "/bin/sh -c corepack enable yarn && NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=$(sh /app/create-publishable-key.sh -u $MEDUSA_BACKEND_URL -e $EMAIL -p $PASSWORD) yarn run build" did not complete successfully: exit code: 1

The server is up and running

SigmaSeven7 avatar Dec 14 '24 17:12 SigmaSeven7

If I manually create an publishable API key in the admin panel and then paste it here : RUN corepack enable yarn && NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=$(sh /app/create-publishable-key.sh -u $MEDUSA_BACKEND_URL -e $EMAIL -p $PASSWORD) yarn run build

instead of '$(sh /app/create-publishable-key.sh -u $MEDUSA_BACKEND_URL -e $EMAIL -p $PASSWORD)' and everything works. I'm guessing the issue is with the script that creates the api key.

SigmaSeven7 avatar Dec 14 '24 18:12 SigmaSeven7

actually, when trying to navigate to localhost:8000 I'm getting 500 :/

SigmaSeven7 avatar Dec 14 '24 18:12 SigmaSeven7

No problem mate! Can you share the generated entrypoint.sh? And also the 500 error trace?

beakman avatar Dec 14 '24 21:12 beakman

Wow. Thank you very much Beakman! Continue the valuable work!

3 questions:

  1. Do you have a chat channel for questions/answer ?
  2. Should I just checkout my own medusa source code and adapt the docker file to copy the files to the container when developing local with live reloading instead of using the default medusa-starter from their repo or what is your suggestion or thoughts ?
  3. Same issue here with the

Error: Missing required environment variables 0.845 0.846 NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY 0.846 Learn how to create a publishable key: https://docs.medusajs.com/v2/resources/storefront-development/publishable-api-keys 0.846 0.846 0.846 Please set these variables in your .env file or environment before starting the application.

tuncay-dev avatar Dec 17 '24 22:12 tuncay-dev

Hiya, thanks for the repo. Just looking at moving my medusa v1 docker compose repo to v2.

The requirement to have a Publishable API key and call the running server before doing storefront build is a real pain. I made workflow with just that separates out the key creation into a distinct step, which makes it a bit more idempotent. So that as long as you have docker and just you should be able to run just devand it'll build the images in the right order: https://github.com/irab/medusa-container-build/blob/v2/justfile

Will raise a PR once I've done some more fiddling...

irab avatar Jan 13 '25 01:01 irab