Question

DO App Platform Env Vars Not Available During Build

I currently have my Dockerfile set to the following

FROM node:17-alpine3.15 As development

WORKDIR /usr/src/app

COPY package*.json ./
COPY prisma ./prisma/

RUN npm install

COPY . .

RUN echo ${DATABASE_URL}

RUN npx prisma migrate deploy
RUN npx prisma generate

RUN npm run build

FROM node:17-alpine3.15 as production

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install --only=production

COPY . .

RUN npx prisma generate

COPY --from=development /usr/src/app/dist ./dist

CMD ["node", "dist/main"]

And my App Spec set to:

alerts:
- rule: DEPLOYMENT_FAILED
- rule: DOMAIN_FAILED
databases:
- engine: PG
  name: db
  num_nodes: 1
  size: db-s-dev-database
  version: "12"
domains:
- domain: xxxxxxxxxxxx.com
  type: PRIMARY
- domain: www.xxxxxxxxx.com
  type: ALIAS
envs:
- key: DATABASE_URL
  scope: RUN_AND_BUILD_TIME
  value: ${db.DATABASE_URL}
name: xxxxxxxxxxxxxxx
region: nyc
services:
- dockerfile_path: Dockerfile
  github:
    branch: master
    deploy_on_push: true
    repo: xxxxxx/xxxxxxx
  http_port: 3000
  instance_count: 1
  instance_size_slug: basic-xxs
  name: xxxxxxxxxxx
  routes:
  - path: /
  source_dir: /

The relevant env var is DATABASE_URL which I have set to the bind-able variable ${db.DATABASE_URL}. When I log into the console and run echo ${DATABASE_URL}, it returns the expected value; however, in the Dockerfile, I have added a line that should do the exact same RUN echo ${DATABASE_URL}. I added this line as a debug step since Prisma has not been able to find the env var during build time.

2022-04-03T18:52:24.065869742Z INFO[0068] cmd: /bin/sh                                 
2022-04-03T18:52:24.065923766Z INFO[0068] args: [-c echo $DATABASE_URL]                
2022-04-03T18:52:24.066156470Z INFO[0068] Running: [/bin/sh -c echo $DATABASE_URL]     
2022-04-03T18:52:24.070341477Z 
2022-04-03T18:52:24.070512989Z INFO[0068] Taking snapshot of full filesystem...        
2022-04-03T18:52:26.795716689Z INFO[0070] No files were changed, appending empty layer to config. No layer added to image. 
2022-04-03T18:52:26.795753400Z INFO[0070] RUN npx prisma migrate deploy                
2022-04-03T18:52:26.795759108Z INFO[0070] cmd: /bin/sh                                 
2022-04-03T18:52:26.795762315Z INFO[0070] args: [-c npx prisma migrate deploy]         
2022-04-03T18:52:26.795829343Z INFO[0070] Running: [/bin/sh -c npx prisma migrate deploy] 
2022-04-03T18:52:26.805099942Z INFO[0070] Pushing layer <registry-uri-4> to cache now 
2022-04-03T18:52:26.806076954Z INFO[0070] Pushing image to <registry-uri-5> 
2022-04-03T18:52:30.573795320Z Environment variables loaded from .env
2022-04-03T18:52:30.587384625Z Prisma schema loaded from prisma/schema.prisma
2022-04-03T18:52:30.767634422Z Error: Get config: Schema Parsing P1012
2022-04-03T18:52:30.767658122Z 
2022-04-03T18:52:30.767662027Z error: Environment variable not found: DATABASE_URL.
2022-04-03T18:52:30.767666757Z   -->  schema.prisma:10
2022-04-03T18:52:30.767670924Z    | 
2022-04-03T18:52:30.767673327Z  9 |   provider = "postgres"
2022-04-03T18:52:30.767676852Z 10 |   url      = env("DATABASE_URL")
2022-04-03T18:52:30.767679481Z    | 
2022-04-03T18:52:30.767681587Z 
2022-04-03T18:52:30.767683994Z Validation Error Count: 1
2022-04-03T18:52:30.767689885Z 
2022-04-03T18:52:30.808100760Z error building image: error building stage: failed to execute command: waiting for process to exit: exit status 1
2022-04-03T18:52:30.809647186Z 
2022-04-03T18:52:30.809659552Z command exited with code 1
2022-04-03T18:52:33.253784241Z  ! Build failed (exit code 1)

Here is the output in the console

I have RUN_AND_BUILD_TIME set in the App Spec, so I’m pretty confused as to why my variable isn’t available at build time.

Any help is appreciated. Thank you in advance!


Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
July 27, 2022

Hi there,

As you ar using a Dockerfile, this passes variables down to the docker build process (with a --build-arg parameter) so you can access the values of the environment variables using the ARG keyword in your Dockerfile as you normally would if you were building this on a local machine.

For more information on how to use environment variables with a Dockerfile and the App Platform I could suggest the docs here:

https://docs.digitalocean.com/products/app-platform/reference/dockerfile/#environment-variables

Hope that this helps!

Best,

Bobby