Unable to use environment vars inside dockerfile during build time

Issues information

  • OS: Alpine Docker Image
  • databases: Postgres
  • Programming language and version: Nodejs, Node v 17.8.0, npm v 8.5.5
  • Link to your project on Github/Gitlab: Private

Your issue

I am following the steps in this thread, and still unable to access env variables during build time.

So during build of my docker image, I have added a command that performs DB migrations. This command expects the env vars for DB to be available. I have

  • Set the corresponding env & secrets vars from Qovery UI
  • Updated the dockerfile, build
  • It fails

I also have another build var NODE_AUTH_TOKEN which is being correctly used, since it is used for npm ci and that step goes through, so I might be missing something here.

Dockerfile content (if any)

FROM node:17
ARG NODE_AUTH_TOKEN

ARG DB_HOST
ENV DB_HOST $DB_HOST

ARG DB_PORT
ENV DB_PORT $DB_PORT

ARG DB_USERNAME
ENV DB_USERNAME $DB_USERNAME

ARG DB_PASSWORD
ENV DB_PASSWORD $DB_PASSWORD

ARG DB_NAME
ENV DB_NAME $DB_NAME

WORKDIR /btpk
# Install app dependencies
COPY package*.json ./
COPY .npmrc ./
RUN npm ci

# Bundle app source
COPY . .

EXPOSE 8080

RUN npm run build
ENV NODE_ENV=production
RUN npm run migrate

CMD ["npm", "run", "start:prod"]

The following command works locally for me

docker build --build-arg NODE_AUTH_TOKEN=${NODE_AUTH_TOKEN} --build-arg DB_HOST=host.docker.internal --build-arg DB_PORT=5432 --build-arg DB_USERNAME=postgres --build-arg DB_NAME=dev-012 --build-arg DB_PASSWORD='' -f Dockerfile -t api:latest .

Screenshot 2022-06-28 at 9.08.14 AM

The error I get is attached below. I can reproduce this error when I change the DB connection params. So its evident that for some reason the env variables are not being passed correctly to the build command.

Hi @r4881t , I think you did it right. The only error I see is your alias DB_HOST targeting the QOVERY_POSTGRESQL_ZA7DD2F46_HOST instead of QOVERY_POSTGRESQL_ZA7DD2F46_HOST_INTERNAL.

image

The difference between both is QOVERY_POSTGRESQL_ZA7DD2F46_HOST use the external network to connect to your database. So your database must be public (not recommended). Where QOVERY_POSTGRESQL_ZA7DD2F46_HOST use the internal network (it’s recommended).

If:

  1. You change your alias to target QOVERY_POSTGRESQL_ZA7DD2F46_HOST_INTERNAL
  2. You redeploy your app.

It should work.

Hello @rophilogene . Thanks for answer. I tried that and still having the same issue.

Can you show me the configuration file content that is using DB_HOST for the migration?

Sure, here it is. It’s a TypeORM config

export const BtpkDataSource = new DataSource({
  type: 'postgres',
  host: process.env.DB_HOST,
  port: +process.env.DB_PORT,
  username: process.env.DB_USERNAME,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
  synchronize: false,
  logging: false,
  logger: 'advanced-console',
  entities: [entityPath],
  migrations: [migrationPath],
  subscribers: [subscribersPath]
});

@r4881t can you try this :point_down:

ARG DB_HOST $DB_HOST
ENV DB_HOST $DB_HOST

ARG DB_PORT $DB_PORT
ENV DB_PORT $DB_PORT

ARG DB_USERNAME $DB_USERNAME
ENV DB_USERNAME $DB_USERNAME

ARG DB_PASSWORD $DB_PASSWORD
ENV DB_PASSWORD $DB_PASSWORD

ARG DB_NAME $DB_NAME
ENV DB_NAME $DB_NAME

I updated it, still nothing

FROM node:17
ARG NODE_AUTH_TOKEN

ARG DB_HOST $DB_HOST
ENV DB_HOST $DB_HOST

ARG DB_PORT $DB_PORT
ENV DB_PORT $DB_PORT

ARG DB_USERNAME $DB_USERNAME
ENV DB_USERNAME $DB_USERNAME

ARG DB_PASSWORD $DB_PASSWORD
ENV DB_PASSWORD $DB_PASSWORD

ARG DB_NAME $DB_NAME
ENV DB_NAME $DB_NAME

WORKDIR /btpk
# Install app dependencies
COPY package*.json ./
COPY .npmrc ./
RUN npm ci

# Bundle app source
COPY . .

EXPOSE 8080

RUN npm run build

ENV NODE_ENV=production

RUN npm run migrate

CMD ["npm", "run", "start:prod"]

Ok, let me try locally to reproduce what happened and I come back to you

Much thanks. For now, I have moved the migrations command to npm start so it’s not a blocker for me.

1 Like

Hello @r4881t,

Sadly, you cannot reach your cluster/database/services that run in your environment during the build phase of your application (unless public).

Our builder that execute the Dockerfile are not in the same network than your environment, so it has no access/reach to databases.

When doing RUN npm run migrate, this command will be executed during the build ;(

If you want to run this command, you need to execute it during the start of your container application. So must be put inside the CMD section.

As you already have moved it to npm start, you should be good :slight_smile:

2 Likes

Thank you @Erebe :pray:

@r4881t I think it’s a better practice to run your DB migration while your app starts instead of during the build time.