Env var not available at build time

Issues information

  • OS:
  • databases: postgresql
  • Programming language and version: node v16
  • Link to your project on Github/Gitlab: private

Your issue

DATABASE_URL seems not to be defined, despite the fact to have it in my .env.production:

DATABASE_URL="${QOVERY_POSTGRESQL_Z234F4230_DATABASE_URL_INTERNAL}"
$ dotenv -c production -- prisma migrate deploy
Prisma schema loaded from prisma/schema.prisma
Error: Get config: Schema Parsing P1012
error: Environment variable not found: DATABASE_URL.
--> schema.prisma:8
|
7 | provider = "postgresql"
8 | url = env("DATABASE_URL")
Validation Error Count: 1
error Command failed with exit code 1.

Dockerfile content (if any)

FROM node:16-alpine as build
WORKDIR /app

COPY package.json .
COPY yarn.lock .
COPY .npmrc .
COPY backend/package.json backend/
RUN yarn --frozen-lockfile --ignore-scripts --ignore-engines

WORKDIR /app/backend
COPY backend/prisma prisma
RUN yarn dotenv -c production -- yarn prisma generate

COPY backend .
RUN yarn graphql-codegen
RUN yarn build

FROM node:16-alpine
WORKDIR /app
EXPOSE 3100

COPY --from=build /app/package.json .
COPY --from=build /app/yarn.lock .
COPY --from=build /app/node_modules node_modules

WORKDIR /app/backend
COPY --from=build /app/backend/package.json .
COPY --from=build /app/backend/prisma prisma
COPY --from=build /app/backend/dist dist

ARG QOVERY_APPLICATION_Z5A52AF3F_HOST_EXTERNAL
ENV QOVERY_APPLICATION_Z5A52AF3F_HOST_EXTERNAL $QOVERY_APPLICATION_Z5A52AF3F_HOST_EXTERNAL
ARG QOVERY_POSTGRESQL_Z234F4230_DATABASE_URL_INTERNAL
ENV QOVERY_POSTGRESQL_Z234F4230_DATABASE_URL_INTERNAL $QOVERY_POSTGRESQL_Z234F4230_DATABASE_URL_INTERNAL
ARG QOVERY_REDIS_ZC5CB5F2F_HOST_INTERNAL
ENV QOVERY_REDIS_ZC5CB5F2F_HOST_INTERNAL $QOVERY_REDIS_ZC5CB5F2F_HOST_INTERNAL
ARG QOVERY_REDIS_ZC5CB5F2F_PORT
ENV QOVERY_REDIS_ZC5CB5F2F_PORT $QOVERY_REDIS_ZC5CB5F2F_PORT
ARG QOVERY_REDIS_ZC5CB5F2F_PASSWORD
ENV QOVERY_REDIS_ZC5CB5F2F_PASSWORD $QOVERY_REDIS_ZC5CB5F2F_PASSWORD
ARG DATABASE_URL
ENV DATABASE_URL $DATABASE_URL

CMD ["yarn", "start:prod"]

package.json’s scripts:

{
  "scripts": {
    "build": "dotenv -c production -- nest build --webpack",
    "prestart:prod": "dotenv -c production -- prisma migrate deploy",
    "start:prod": "dotenv -c production -- node dist/main"
  },
}

Hi @soywod ,

When you connect to your application via qovery shell can you type env | grep DATABASE_URL command? Do you see the environment variable?

Can you check that you have create an alias for DATABASE_URL in your Qovery interface?

Thank you

I do not use the qovery CLI, I will install it and let you know!

If I set directly DATABASE_URL from the dashboard it works well, but I want it from my .env.production file. I tried to setup an alias called DATABASE_URL_QOVERY and put in my env file DATABASE_URL="${DATABASE_URL_QOVERY}" but it does not work. I want it from the env file because I need to override some env vars given by qovery, like QOVERY_APPLICATION_XXX_HOST_EXTERNAL: this one returns the domain of the frontend without https, so I put in my env file FRONTEND_URL="https://${QOVERY_APPLICATION_XXX_HOST_EXTERNAL}". But no luck with this one also. Any other suggestion?

Ok, it makes total sense what you want to do.

Can your app interpolate environment variables from a .env file? Meaning, does your app will replace the placeholders vars $XXX_YYY to the real value?

Yes it does, see Documentation | NestJS - A progressive Node.js framework. So it is technically possible but it looks like sth is preventing me to doing so :frowning:

I suspect a missing configuration in the Dockerfile. I DM you in private and we’ll report back the solution here.

Same issue here with CONFIG_ENV=production not passed to the build nextjs process when configured as a projet/environment/application env var. The only solution I have found is to force that env var value in the Dockerfile but that’s not as flexible

Unfortunately, it’s a requirement of Docker.

Hi Romaric
Well, unless Qovery allowed for --build-arg passing to docker build … would that be feasable, says some list of --build-args in the same way as the env var list perhaps ?

It’s already the case. Qovery passes --env options which leads you to indicate which environment variables you want to use at the build time.

You can take a look at the engine code here:


Indeed, an improvement that we can do is using --build-arg and giving the option to the user to specify which environment variable to use at the build time. Then we do not risk unnecessary invalidation of Docker builds cache layers.

cc @a_carrano

1 Like

I was able to do what I wanted be prefixing env directly inside the Dockerfile:

ARG FRONTEND_URL_QOVERY
ENV FRONTEND_URL "https://$FRONTEND_URL_QOVERY"

Thanks a lot for you help!

1 Like

Thank you @soywod for reporting back :pray: well done