Question about ARGs/Envs in dockerfile suggested in T3 website.

I am not a docker veteran however on this page the example seems a bit wrong. so on this block
##### BUILDER

FROM --platform=linux/amd64 node:20-alpine AS builder
ARG DATABASE_URL
ARG NEXT_PUBLIC_CLIENTVAR
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
##### BUILDER

FROM --platform=linux/amd64 node:20-alpine AS builder
ARG DATABASE_URL
ARG NEXT_PUBLIC_CLIENTVAR
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ARG DATABASE_URL is not gonna be used on the build step isn't it? and it is not provided also with the build command. so it might be not needed here? and the next line ARG NEXT_PUBLIC_CLIENTVAR gets the ARG but never actually puts it into ENV so the build can have it. shoudn't we add a line here like: ENV NEXT_PUBLIC_CLIENTVAR=$NEXT_PUBLIC_CLIENTVAR ? Would be nice if someone with more experience with Docker can take a look at this and let me know if my suspissions are right?
Create T3 App
Docker 🚀 Create T3 App
The best way to start a full-stack, typesafe Next.js app.
3 Replies
Rivenris
Rivenris•2mo ago
@amir The example is correct. Arguments are available to run commands as environment variables but only during image build. Once container is started there is no such variable (that is why they have separate example with DATABASE_URL env passed during docker run Take look at this snippet:
FROM ubuntu:latest
ARG TEST_ARG=abc
RUN bash -c 'env | grep ARG'
FROM ubuntu:latest
ARG TEST_ARG=abc
RUN bash -c 'env | grep ARG'
It produces this log during build:
...
#5 [2/2] RUN bash -c 'env | grep ARG'
#5
sha256:20173d73bc2f83335ee40b01b5821bf0b898e9fe5895697ca1727d52edd7fcf2
#5 0.373 TEST_ARG=abc
...
#5 [2/2] RUN bash -c 'env | grep ARG'
#5
sha256:20173d73bc2f83335ee40b01b5821bf0b898e9fe5895697ca1727d52edd7fcf2
#5 0.373 TEST_ARG=abc
It all depends on whether the app has those values baked in and next does this a lot. Unfortunately, because changing some of those values at runtime becomes much harder depending on where they are used.
amir
amirOP•2mo ago
@Rivenris thanks for the info. did not know that ARGs are available as env vars in build time 🙂 so if i understood it right, the DATABASE_URL is also defined in build time even though it should technically be only run time var since next might use it somehow in the build?
Rivenris
Rivenris•2mo ago
Next will use this to get data on static pages So if you have any static pages (including notFound) that might have data dependency on it, then next will connect to the db to use it

Did you find this page helpful?