How can I use the Container's ENV on DATABASE_URL Symfony - symfony

I have the following environment on my php container like so:
DATABASE_URL:mysql://root:${MYSQL_ROOT_PASSWORD}#db:3306/${MYSQL_DATABASE}
I tried to echo it inside my container, and do a db migration. All is good. Now I used it on my .env file on symfony like so:
DATABASE_URL=${DATABASE_URL}
When i tried to login, the app says:
Authentication request could not be processed due to a system problem.
When i try to manually put everything on .env DATABASE_URL all is good.
I suspect that when I tried to use the container's ENV it doesn't get it right.
My question is how can I use the actual Containers' environment variable?
Thanks!
Note:
I on dev environment.

I am not quite familiar with symfony, but it seems like symfony never overwrites existing environment variables. (Ref: https://symfony.com/doc/current/components/dotenv.html)
What if you remove that line in your .env file? Since DATABASE_URL is already an environment variable, calling getenv('DATABASE_URL') in symfony should return you the correct value even if you did not define it in .env. All dotenv does it to write those key value pairs as environment variables in your system. You don't need to define it again if it is already present.

note that there is separate environments when you run php from cli and when it gets run by webserver (when you access it from your browser).
For example, in case of nginx-fpm, the variables that you see in your cli by running printenv are not avaiable in php script run by nginx when you call getenv(). In order to set environment variable for php fpm you can edit php-fpm.conf:
....
[www]
env[DATABASE_URL] = 'mysql://...'
....
If you use another webserver than you should find out how to make env vars available in php script.
Your DATABASE_URL=${DATABASE_URL} in .env file didn't work bacause DATABASE_URL was not set for php fpm.
Hope this helps.
P.S. Note, that this construction VAR=${VAR} makes nothing because DotEnv will not override VAR as it is already defined.
P.P.S. It is advised that you use .env file for your dev server and "real" env variables in staging/production.

Related

How to set environment variables with Prisma, Nextjs, and Vercel

Nextjs wants you to use a .env.local file to store env vars.
Prisma uses .env
If I use a .env.local file then setting up the Prisma db
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
I get a DATABASE_URL does not exist error.
What's the right way to set up env vars for a Prisma, Nextjs, Vercel app?
You can use dotenv-cli to force loading specific environment file.
1- Install dotenv-cli package
2- create script to run env before prisma migration on your package
"scripts": {
...
"prismaDev": "dotenv -e .env.local prisma migrate dev ",
}
3- now you can simply run npm run prismaDev
You can load environment variables with process.env.DATABASE_URL in your case and you can leave it in .env as Prisma asks for. Nextjs can handle multiple .env files without any extra effort.
No need to use an extra package, Nextjs will handle the env vars for you.
https://nextjs.org/docs/basic-features/environment-variables
You can leave the standard setup and use the created .env file (by the prisma cli:
https://www.prisma.io/docs/getting-started/setup-prisma) and add your connection string.
the rest is magically handled by next.
Be sure to follow the instructions (manual or cli pointers) regarding gitignore and such..
To reiterate: both can live next to each other! Check the next documentation for load order if it matters. (see link above)

update wso2 deployment.toml configuration at the docker runtime

I am new to docker.
I can create image using dockerfile and successfully call WSO2-API
I have hardcoded configuration in deployment.toml file
I want to update this information at the docker runtime for different env - DEV,QA etc
deployment.toml file content -
[server]
offset = 22
How to update .toml file config at the runtime ?
https://ei.docs.wso2.com/en/7.2.0/micro-integrator/setup/dynamic_server_configurations/#environment-variables
Here it says you can mention like-
offset = "${VariableName}"
but what do I mention in my dockerfile to update these variables at runtime ?
I want to update this information at the docker runtime for different env - DEV,QA etc
There are multiple ways to achieve this, here are at least two we commonly use in our deployment
Using a template for the config files
Basically the idea is to mount the deployment.toml (or other config files / folders) as configmap values in Kubernetes or volume in pure docker.
For each environment you can template the configuration using any deployment tool (Maven, Puppet, Ansible, any cloud devops,...). This approach allows you to update the configuration templates without needing a new image.
Template the configuration in the entrypoint
Create an entrypoint script, which templates the configuration based on env variables - e. g. using the sed utility) and then starts the application. Then use that entrypoint in the Dockerfile
This approach doesn't need external configuration (volumes, templates) but if the template needs to be updated, you need a new image.
Edit:
I haven't seen using the env variables in the deployment.toml before as refered in the question, must be something new for wso2. But if it is supported, then it can make your life easier just to specify the env variables in the pod. (oh this is you may be missing)
specify the ENV value in the Dockerfile for the default value
run the docker with your defined value (-e parameter for pure docker or defined environment in the compose or deployment config)
Define the Variable using ARG option in Dockerfile.
Example:
ARG VariableName
Now the value can be given at runtime as below.
docker build --build-arg VariableName=0 .
For more details on how to use ARG in dockerfile, please refer https://docs.docker.com/engine/reference/builder/#arg

Laravel Homestead setup but does not sync hidden files

Hi I've setup Homestead correctly but when I ssh into my Vagrant instance I can see all my files just not the hidden ones (.env, .git, .gitignore are missing). I'm trying to run webpack-dev-server within my instance and it needs my .env file to run. Is it normal that hidden files are not synced?
My .yaml file:
Hidden files don't show when using the ls command (which I guess is what u're doing). They are still there, however. You can try something like nano .env and you'll see that you can edit your env from the console :)
Another command you could use is ls -a, which should show all files regardless of whether they're hidden or not.

Nginx 1.14 can't access System Environment Variables

I have spend past 2 days trying to figure out how to get a System Environment Variable to get passed into Nginx.
I have declared the Environment Variable in /etc/nginx.conf with env FOO.
I have tried perl_set and set_by_lua.
But non works.
I am running this on a Ubuntu "Bionic" 18.04 machine.
Any idea?
Edit:
The issue is, Nginx strips all the environment variables, so they are not available within the web server process. Following some Nginx tutorials, set_by_perl or set_by_lua are the ways recommended for getting System Environment Variables into the web server process, which doesn't seem to work in my case.
Edit: Following is the tutorial I followed: https://web.archive.org/web/20170712003702/https://docs.apitools.com/blog/2014/07/02/using-environment-variables-in-nginx-conf.html
You can't set an environment variable from within nginx.conf. It will be ignored, which is what you've seen.
There are several places you can set the environment variables, I'll show the example for the file where the env vars will be availbale to all user logins and to NGINX.
$sudo nano /etc/environment
Add your defs to the end of the file; Leave everything that is there, there. Just append your new defs. Do this exactly, no extra spaces at the end of lines, no extra lines at the end of the file. Use double quotes. No comments. Very exact.
export COMPILERKEY = "jk8894085349058jjggl"
export FOO="ThisIsFooValue"
export FOO2="ThisIsFooValue2"
Save the file
Reboot server
To show all env vars that are defined
$env
Once defined, then you can access env vars through defined means. Access methods for the environment variables is a question which I think you're not asking about yet. I hope this helps.

Environment variables not found in console and composer

I set environment variables and removed parameters.yml file according to new recommended configuration.
In my local environment (WAMP), I set these in .htaccess file in the root of my project. The app is working, but Symfony console and composer can't find variables. I can't find why. Do you have an idea ?
I think you have to set environment variables on the terminal. PHP CLI mode will not load instruction within .htaccess
export VARNAME="my value"
in your case with windows in cmd it will be:
set NEWVAR=SOMETHING

Resources