Next.js Discord

Discord Forum

How to deal with env variables?

Answered
Blanc de Hotot posted this in #help-forum
Open in Discord
Original message was deleted.
Answered by Texas leafcutting ant
use next/env
View full answer

33 Replies

Texas leafcutting ant
use next/env
Answer
@Texas leafcutting ant use next/env
Blanc de Hotot
thanks, next/env is a package?
@Blanc de Hotot thanks, next/env is a package?
Texas leafcutting ant
yeap
@Texas leafcutting ant yeap
Blanc de Hotot
thank you, appreciate it! Havec you successfully done that?
Texas leafcutting ant
alternative methods are available
i didn't encounter the same problem but hope this will solve urs
Blanc de Hotot
Thank you, I will test it out, do you have to copy env file into docker image btw?
Texas leafcutting ant
can u please tell me more specifically whats the problem is
why u need to copy it into docker image
for what ur using the docker
@Texas leafcutting ant can u please tell me more specifically whats the problem is
Blanc de Hotot
This is the first time I am trying to work with Dock and Next JS, everything works fine, except I don't know how to setup and config env file when building the image
I'm trying to self host
Texas leafcutting ant
ohh u have to put the environment in the next configuration file and from there u can access it like this process.env.name
like u don't need to make a .env file for that
like this 'module.exports = {
env: {
customKey: 'my-value',
},
}'
hope u get it
@Texas leafcutting ant ohh u have to put the environment in the next configuration file and from there u can access it like this process.env.name
Blanc de Hotot
oh, I see, to put all the env variables in the Next config ? so at the end of the day all the variables still live in the Docker image built right?
Texas leafcutting ant
Yes, that's right. If you put environment variables in the Next.js config or .env files, and then build the Docker image, those variables will be part of the image. However, you can also pass them in at runtime when the container starts, which keeps them out of the image itself.
Texas leafcutting ant
passing the env variables explicitly as arguments to rhe docker
command
docker run -e NEXT_PUBLIC_API_URL=https://api.example.com -e SECRET_KEY=mysecretkey -p 3000:3000 my-next-app
@Texas leafcutting ant passing the env variables explicitly as arguments to rhe docker
Blanc de Hotot
you mean pass them in the Dockerfile directly?
Texas leafcutting ant
not to dockerfile but to pass them to the docker when it starts
like in the above command
for what u are using the docker
??
Blanc de Hotot
Got you, why am I using Docker? Just trying to self host on AWS
Texas leafcutting ant
so 2 ways to start the docker container either access them from the .env or access them directly at docker run time
Blanc de Hotot
Got it, thanks man
Texas leafcutting ant
🥰
@Blanc de Hotot for archival purposes please avoid deleting the original question unless strictly necessary, such as when the question contains secrets (that you should have never posted in the first place). and remember to mark answers to acknowledge other people when they help you.