R
Railway2y ago
Coop

Help with env variables

Hey all. This is my first time working with docker and I can't get the enviornment variables to work with my sveltekit project. For refrence, here is the docker:
# Use Node.js 20.3.1 with Alpine 3.17
FROM node:20.3.1-alpine3.17

# Create app directory
WORKDIR /usr/src/app

# Install pnpm globally
RUN npm install -g pnpm

# Copy package.json and pnpm-lock.yaml (if you have one)
COPY package.json pnpm-lock.yaml* ./

# Install app dependencies
RUN pnpm install

# Bundle app source
COPY . .

# Build the application
RUN pnpm build

# This command will run your app
CMD ["pnpm", "start"]
# Use Node.js 20.3.1 with Alpine 3.17
FROM node:20.3.1-alpine3.17

# Create app directory
WORKDIR /usr/src/app

# Install pnpm globally
RUN npm install -g pnpm

# Copy package.json and pnpm-lock.yaml (if you have one)
COPY package.json pnpm-lock.yaml* ./

# Install app dependencies
RUN pnpm install

# Bundle app source
COPY . .

# Build the application
RUN pnpm build

# This command will run your app
CMD ["pnpm", "start"]
Here is the error I get
#11 5.090 ✓ 121 modules transformed.
#11 5.090 ✓ built in 2.14s
#11 5.090 "GOOGLE_CLIENT_ID" is not exported by "$env/static/private", imported by "src/hooks.server.ts".
#11 5.090 file: /usr/src/app/src/hooks.server.ts:1:9
#11 5.090 1: import { GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, AUTH_SECRET } from '$env/static/private'
#11 5.090 ^
#11 5.090 2: import prisma from '$lib/server/prisma'
#11 5.090 3: import Google from '@auth/core/providers/google'
#11 5.090 ✓ 121 modules transformed.
#11 5.090 ✓ built in 2.14s
#11 5.090 "GOOGLE_CLIENT_ID" is not exported by "$env/static/private", imported by "src/hooks.server.ts".
#11 5.090 file: /usr/src/app/src/hooks.server.ts:1:9
#11 5.090 1: import { GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, AUTH_SECRET } from '$env/static/private'
#11 5.090 ^
#11 5.090 2: import prisma from '$lib/server/prisma'
#11 5.090 3: import Google from '@auth/core/providers/google'
Please help
Solution:
well at least I was right about it being a code issues haha
Jump to solution
43 Replies
Percy
Percy2y ago
Project ID: 94911b1f-964d-4305-9412-64feedd98d6e
Coop
CoopOP2y ago
94911b1f-964d-4305-9412-64feedd98d6e
Coop
CoopOP2y ago
Should I provide an ARG for NODE_ENV or PORT?
Brody
Brody2y ago
for PORT, no, since it's not used in the build stage for NODE_ENV, yes, it would be used during build
Coop
CoopOP2y ago
Im still a little confused. From the screenshot I provided could you help edit my docker file?
Brody
Brody2y ago
did you read the docs section I linked?
Coop
CoopOP2y ago
Yes
Brody
Brody2y ago
and what are you confused about exactly? I'll do it for you, but that's not the best method of learning
Coop
CoopOP2y ago
So you don't specify arg, you just say arg exists So that when you call docker with args it takes them in?
# Bundle app source
COPY . .

# Use ARG for build-time variables
ARG DATABASE_URL
ARG GOOGLE_CLIENT_ID
ARG GOOGLE_CLIENT_SECRET
ARG AUTH_SECRET

# Set Environment Variable
ENV DATABASE_URL=$DATABASE_URL
ENV GOOGLE_CLIENT_ID=$GOOGLE_CLIENT_ID
ENV GOOGLE_CLIENT_SECRET=$GOOGLE_CLIENT_SECRET
ENV AUTH_SECRET=$AUTH_SECRET

# Build the application
RUN pnpm build
# Bundle app source
COPY . .

# Use ARG for build-time variables
ARG DATABASE_URL
ARG GOOGLE_CLIENT_ID
ARG GOOGLE_CLIENT_SECRET
ARG AUTH_SECRET

# Set Environment Variable
ENV DATABASE_URL=$DATABASE_URL
ENV GOOGLE_CLIENT_ID=$GOOGLE_CLIENT_ID
ENV GOOGLE_CLIENT_SECRET=$GOOGLE_CLIENT_SECRET
ENV AUTH_SECRET=$AUTH_SECRET

# Build the application
RUN pnpm build
this is what I have. I need to specify the node env and port right?
Brody
Brody2y ago
^ I would also put the ARG and ENV stuff directly after the FROM directive
Coop
CoopOP2y ago
so this?
# Use Node.js 20.3.1 with Alpine 3.17
FROM node:20.3.1-alpine3.17

# Use ARG for build-time variables
ARG DATABASE_URL
ARG GOOGLE_CLIENT_ID
ARG GOOGLE_CLIENT_SECRET
ARG AUTH_SECRET
ARG PORT

# Set Environment Variable
ENV DATABASE_URL=$DATABASE_URL
ENV GOOGLE_CLIENT_ID=$GOOGLE_CLIENT_ID
ENV GOOGLE_CLIENT_SECRET=$GOOGLE_CLIENT_SECRET
ENV AUTH_SECRET=$AUTH_SECRET
ENV PORT=$PORT
ENV NODE_ENV=production

# Create app directory
WORKDIR /usr/src/app

# Install pnpm globally
RUN npm install -g pnpm

# Copy package.json and pnpm-lock.yaml (if you have one)
COPY package.json pnpm-lock.yaml* ./

# Install app dependencies
RUN pnpm install

# Bundle app source
COPY . .



# Build the application
RUN pnpm build

# This command will run your app
CMD ["pnpm", "start"]
# Use Node.js 20.3.1 with Alpine 3.17
FROM node:20.3.1-alpine3.17

# Use ARG for build-time variables
ARG DATABASE_URL
ARG GOOGLE_CLIENT_ID
ARG GOOGLE_CLIENT_SECRET
ARG AUTH_SECRET
ARG PORT

# Set Environment Variable
ENV DATABASE_URL=$DATABASE_URL
ENV GOOGLE_CLIENT_ID=$GOOGLE_CLIENT_ID
ENV GOOGLE_CLIENT_SECRET=$GOOGLE_CLIENT_SECRET
ENV AUTH_SECRET=$AUTH_SECRET
ENV PORT=$PORT
ENV NODE_ENV=production

# Create app directory
WORKDIR /usr/src/app

# Install pnpm globally
RUN npm install -g pnpm

# Copy package.json and pnpm-lock.yaml (if you have one)
COPY package.json pnpm-lock.yaml* ./

# Install app dependencies
RUN pnpm install

# Bundle app source
COPY . .



# Build the application
RUN pnpm build

# This command will run your app
CMD ["pnpm", "start"]
Brody
Brody2y ago
again, you don't need to specify the PORT ^
Coop
CoopOP2y ago
so I don't need to include port anywhere
Brody
Brody2y ago
that is what I have said 4 times now
Coop
CoopOP2y ago
My bad
Brody
Brody2y ago
no worries well how did it go?
Coop
CoopOP2y ago
Well my build kept stopping at transforming so I used a differnt docker container and that didn't work either

#12 5.057 6:08:45 AM [vite-plugin-svelte] /src/routes/(protected)/explore/+page.svelte:1:29 Page has unused export property 'data'. If it is for external reference only, please consider using `export const data`
#12 5.057 1: <script lang="ts">export let data;
#12 5.057 ^
#12 5.057 2: </script>
#12 5.057 3:

#12 5.679 ✓ 121 modules transformed.

#12 5.785 rendering chunks...

#12 7.311 Promise { <pending> }
#12 7.361
#12 7.361 vite v4.4.2 building for production...
#12 7.372 transforming...

#12 7.729  ELIFECYCLE  Command failed.

#12 ERROR: process "/bin/sh -c pnpm build" did not complete successfully: exit code: 1

#12 5.057 6:08:45 AM [vite-plugin-svelte] /src/routes/(protected)/explore/+page.svelte:1:29 Page has unused export property 'data'. If it is for external reference only, please consider using `export const data`
#12 5.057 1: <script lang="ts">export let data;
#12 5.057 ^
#12 5.057 2: </script>
#12 5.057 3:

#12 5.679 ✓ 121 modules transformed.

#12 5.785 rendering chunks...

#12 7.311 Promise { <pending> }
#12 7.361
#12 7.361 vite v4.4.2 building for production...
#12 7.372 transforming...

#12 7.729  ELIFECYCLE  Command failed.

#12 ERROR: process "/bin/sh -c pnpm build" did not complete successfully: exit code: 1
I've been through a few docker containers and there seems to be a slight problem on all of them that doesn't allow me to fully build and work them
Brody
Brody2y ago
thats a problem with your code, not the docker image you are using
Coop
CoopOP2y ago
But I can build it fine, and before when i was just using nixpacks it worked fine I only started using docker containers because I didn't like having to change the settings: autoInstallPeers: true in my pnpm file
Brody
Brody2y ago
so use nixpacks again?
Coop
CoopOP2y ago
I saw your comment about manually using docker containers so I decided to give it a try I think I'm going to stick with nixpacks until I can get a fully working docker container
Brody
Brody2y ago
only if there's a good enough reason, what's your reason?
Coop
CoopOP2y ago
My reason is that I don't like changing a line in a file every time I install a new package lol but I also just wanted to learn docker
Brody
Brody2y ago
then I'd recommend using docker locally
Coop
CoopOP2y ago
Yea you are probably right Thanks for the help though, that got me through half of my docker problems haha
Brody
Brody2y ago
no problem!
Coop
CoopOP2y ago
Is there a way to delete build cache? I think I completely screwed over my server from those docker builds. Ive never seen this before but look at my builds now even with nixpacks.
> [stage-0 8/10] RUN --mount=type=cache,id=s/0daff7c6-0a31-4449-97b5-186a0906dbca-node_modules/cache,target=/app/node_modules/.cache pnpm run build:
#12 4.464 3:
#12 5.155 ✓ 121 modules transformed.
#12 5.270 rendering chunks...
#12 6.915 Promise { <pending> }
#12 6.965
#12 6.965 vite v4.4.2 building for production...
#12 6.983 transforming...
#12 6.994 free(): invalid pointer
#12 7.353 Aborted (core dumped)
#12 7.357  ELIFECYCLE  Command failed with exit code 134.
> [stage-0 8/10] RUN --mount=type=cache,id=s/0daff7c6-0a31-4449-97b5-186a0906dbca-node_modules/cache,target=/app/node_modules/.cache pnpm run build:
#12 4.464 3:
#12 5.155 ✓ 121 modules transformed.
#12 5.270 rendering chunks...
#12 6.915 Promise { <pending> }
#12 6.965
#12 6.965 vite v4.4.2 building for production...
#12 6.983 transforming...
#12 6.994 free(): invalid pointer
#12 7.353 Aborted (core dumped)
#12 7.357  ELIFECYCLE  Command failed with exit code 134.
I reverted my github to the last commit that worked fine
Coop
CoopOP2y ago
so in nixpakcs.toml:
NIXPACKS_NO_CACHE = true
NIXPACKS_NO_CACHE = true
Brody
Brody2y ago
no in a service variable
Coop
CoopOP2y ago
oohhhh yea I understand No way Only in programming does something work, then not work when I make a change, then still not work when I revert that change
Brody
Brody2y ago
have you tried running nixpacks and docker locally
Coop
CoopOP2y ago
nope, but I've never had to
Brody
Brody2y ago
and now you do!
Coop
CoopOP2y ago
Bruh. It was a single console.log A single console.log of my prisma.users.findMany()
Brody
Brody2y ago
so all good now?
Coop
CoopOP2y ago
And that single console.log ended up allocating 139terabytes of ram Yea Nodejs can be so strange
Solution
Brody
Brody2y ago
well at least I was right about it being a code issues haha
Coop
CoopOP2y ago
yep
Brody
Brody2y ago
glad you've solved it! lmao that's what you mark
Coop
CoopOP2y ago
Thanks man! That was seriously a journey through learning docker to running nixpacks locally All to find out it was a single console.log
Brody
Brody2y ago
programming is werid
Want results from more Discord servers?
Add your server