I can't access the env variables during docker run commands
devtghosh
PROOP

a year ago

I run db migrate commands with docker run. It keeps giving me this error

0.372 Error  Please provide required params for Postgres driver:

0.372     [x] url: undefined

0.379 error: script "db:migrate" exited with code 1

Environment variables can be accessed once the Docker image is built and the container is running, but they are not available during the build process.

What my dockerfile looks like

# Following https://andrekoenig.de/articles/using-bun-as-the-package-manager-in-production-ready-docker-images
# currrent lts version of node
ARG NODE_VERSION=20.17.0

FROM node:${NODE_VERSION}-slim as build

ARG BUN_VERSION=1.1.26

WORKDIR /build

# Install Bun in the specified version
RUN apt update && apt install -y bash curl unzip && \
 curl https://bun.sh/install | bash -s -- bun-v${BUN_VERSION}

ENV PATH="${PATH}:/root/.bun/bin"

#
# Copy the lock file and app manifest, then install
# the dependencies, including the dev dependencies
#
COPY bun.lockb package.json ./

RUN bun install --frozen-lockfile

# Copy the application sources into the build stage
COPY . .

RUN bun run db:generate
# where the error occurs
RUN bun run db:migrate
RUN bun run build

#
# After building the application, we will remove the node_modules
# directory and install only the production dependencies.
#
# Note that clearing the Bun package cache is necessary because I encountered
# extremely slow install times during building the image. This issue seems to be
# related to: https://github.com/oven-sh/bun/issues/4066
#
RUN rm -rf node_modules && \
  rm -rf /root/.bun/install/cache/ && \
  bun install --frozen-lockfile --production

#
# Optional step: Here we will prune all unnecessary files from our
# node_modules directory, such as markdown and TypeScript source files,
# to further reduce the container image size.
#
RUN curl -sf https://gobinaries.com/tj/node-prune | sh && \
    node-prune

FROM node:${NODE_VERSION}-slim as distribution

ENV NODE_ENV="production"

WORKDIR /app

# ADJUST: Copy application build artifacts.
COPY --from=build --chown=node:node /build/node_modules ./node_modules
COPY --from=build --chown=node:node /build/build ./build
COPY --from=build --chown=node:node /build/public ./public
COPY --from=build --chown=node:node /build/package.json .

RUN chown -R node:node /app

USER node

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

The database configuration file is showing that process.env.DATABASE_URL is undefined during db:migrate

import { type Config, defineConfig } from 'drizzle-kit'

const connectionString = process.env.DATABASE_URL

export default defineConfig({
    schema: './drizzle/schema/',
    out: './migrations',
    dialect: 'postgresql', 
    dbCredentials: {
        url: connectionString,
    },
    verbose: true,
    strict: true,
}) satisfies Config

View Deploy details

ⓘ Deployment information is only viewable by project members and Railway employees.

1 Replies

a year ago

They are available during build, please read this - https://docs.railway.app/guides/dockerfiles#using-variables-at-build-time

But ideally you would do your migrations during runtime so that you can use the private network and avoid egress costs.


Status changed to Solved brody over 1 year ago


Loading...