Docker & Environment Variables
We now have a Postgres database running inside of a Docker container. We can see it by running:
docker-compose ps
This also tells us that if we want to talk to this database, we can connect to port 50739 on our local machine. That will be a different port for you, because it's randomly chosen when we start Docker.
We also learned that we can talk to the database directly via:
docker-compose exec database psql --user symfony --password app
To get our actual application to point to the database that's running on this port, we could go into .env or .env.local and customize DATABASE_URL accordingly: with user symfony password ChangeMe... and with whatever your port currently is. Though... we would need to update that port each time we start and stop Docker.
Symfony Binary & Docker Env Vars
Thankfully, we don't need to do any of that because, surprise, the DATABASE_URL environment variable is already being correctly set! When we set up our project, we started a local dev server using the Symfony binary.
Just as a reminder, I'm going to run:
symfony server:stop
to stop that server. And then restart it with:
symfony serve -d
I'm mentioning this because the symfony binary has a pretty awesome Docker superpower.
Watch: when you refresh now... and hover over the bottom right corner of the web debug toolbar, it says "Env Vars: From Docker".
In short, the Symfony binary noticed that Docker was running and exposed some new environment variables pointing to the database! I'll show you. Open up public/index.php.
| // ... lines 1 - 2 | |
| use App\Kernel; | |
| require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; | |
| return function (array $context) { | |
| return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); | |
| }; |
We don't normally care about this file... but it's a great spot to dump some info right when our app starts booting. Inside the callback, dd() the $_SERVER superglobal. That variable contains a lot of information, including any environment variables.
Tip
If you don't see the DATABASE_URL environment variable, you may be using a
slightly older version of Docker of the symfony binary. If so, you can upgrade
those or rename compose.yaml to docker-compose.yaml and compose.override.yaml
to docker-compose.override.yaml.
Ok, spin over and refresh. Big list! Search for DATABASE_URL and... there it is! But that is not the value that we have in our .env file: the port is not what we have there. Nope, it's the correct port needed to talk to the Docker container!
Yup, the Symfony binary detects that Docker is running and sets a real DATABASE_URL environment variable that points to that container. And remember, since this is a real environment variable, it will win over any value placed in the .env or .env.local files.
The point is: just by starting Docker, everything is already set up: we didn't need to touch any config files. That's pretty cool.
By the way, if you want to see all the environment variables the Symfony binary is setting, you can run:
symfony var:export --multiline
But the most important one by far is DATABASE_URL.
Ok: Doctrine is configured! Next, let's create the database itself via a bin/console command. When we do that, we'll learn a trick for doing this with the environment variables from the Symfony binary.
33 Comments
I don't have this super power of symfony console directly creating the docker friendly ENV vars.
I'm using php FPM 8.2.5 and symfony 6.1.2 (on Mac 12.6.6).
Don't know if it's normal (i.e., a change in symfony or doctrine behaviour with the newer versions), or if it's specific to my computer, but in the meantime i'll just create an .env.local file with DATABASE_URL using the correct docker port, hoping that it will do for the rest of this course.
Here is my compose.json in case it helps:
Same problem
Hey @Lore
I strongly recommend installing Symfony CLI, it will make your life way easier, but if for some reason that's not possible, you'll have to go with ling's approach
Cheers!
Thanks, I've already installed Symfony CLI, launched the 'docker-compose up -d' command, and subsequently, the 'symfony serve -d' command, but in the debug toolbar, 'docker compose' shows 'down,' and 'Env var' shows 'none.'
That's unexpected, Symfony CLI should detect your Docker configuration automatically. Check two things
1) Your Symfony project is using Symfony Flex. You should see
symfony/flexinside yourcomposer.jsonfile2) Your
docker-compose.yamlfile should be at the root of your project, otherwise you need to set up a few env vars as explained here https://symfony.com/doc/current/setup/symfony_server.html#docker-integrationHey @ling
Yeah, you can just use
.envfiles to configure your environment. But why you can't use thesymfonybinary with docker detection?Cheers!
I don't know, it just doesn't hook up naturally. I followed all the steps of the tutorials i believe. Not sure why it's not working. I could follow the two previous courses without any problem, this is the first time the code doesn't seem to work as it does in the video. When i hover on the right-bottom corner of the debug bar, it says that docker-compose is down, and next to ENV variables it shows: NONE. But i have a docker container with psql in it, and i ran symfony serve. Mystic?
I guess something is wrong with docker detection, is it the latest version? Which console is used to run docker? Symfony binary? I'm asking because I had same issues when tried to run docker from the PHPStorm console, and for me, it works if I run docker from the native MacOS console or from the docker desktop app.
Cheers
Not sure how closely your issues are related to mine.
But if it helps, here is my final composer.json (i fought a lot with it because some tools in the whole tutorial had some compatibility issues):
{
}
I'm using Docker desktop version 4.19.0.
I start the project with docker:
docker compose up -d
Then the symfony server:
symfony serve -d
[OK] Web server listening
The opening the website at https://127.0.0.1:8000, the bottom right corner of the debug bar shows:
sf 6.2.12, and opening the sf server hover menu, i still have:
which is easily fix by setting the correct port for the DATABASE_URL variable in an env.local file as mentioned in a video of the tutorial.
So i'm not sure where the problem is exactly, but the workaround is fine by me as i just wanted to complete the tutorial, knowing that in prod env there are always other things we can do to setup env variables...
hm Symfony binary version?
nope, the binary version
symfony version?Sorry for long waiting, I tried to debug, different environments, but I can't reproduce this situation, so probably I don't have any solution for it. Looks like Symfony binary just didn't catch docker instance to get all env vars
Cheers
Hey SymfonyCast-Team,
I am facing the same issue. Not only with the downloaded code of the course, but also with a fresh installation.
symfony new docker_postgresql_test --webappdocker-compose updocker-compose ps--> [postgreSQL-Server running]docker-compose exec database psql app app--> [login successful]symfony serveorsymfony server:start -d--> [https://127.0.0.1:8000/ is working]docker compose: downandenv vars: none###### Details
SOLUTION FOUND
In Docker-Settings / Advanced: "Allow the default Docker socket to be used (requires password)" has to be activated.
Hello,
I thank you immensely for the solution provided. I had the same issue and despite various attempts, I had been stuck on it for several days. I really did not want to resort to using a local database. For your information, I am also using MacOS and the same version of Docker Desktop as you, and we are not the only ones experiencing this issue.
wow, that's tricky one.. that is cool you found how to solve it
Thanks for sharing the solution!
Another solution (worked for me) - change file names:
from: compose.yaml to: docker-compose.yaml
and from: compose.override.yaml to docker-compose.override.yaml
Thanks for sharing! Same instructions were added to the course script!
I had the same issue with docker env vars and this solution works for me. Thank you
Yea, this is a good tip! The latest version of the recipe ships with
compose.yamlinstead ofdocker-compose.yaml. Thecompose.yamlfilename is the new filename you should use with Docker. But if you have a slightly out-of-date version of Docker, then it won't recognize it at all. This is definitely a bit of a gotcha right now!I figured it out - it was "symfony-cli" outdated.
(Because I had it in two versions in different places, I didn't notice that the upgrade didn't work. After removing it from everywhere and reinstalling symfony-cli, the problem with Docker is solved).
Hey!
I faced the same problem with Symfony binary variables and Docker on my Ubuntu machine. And after some searching on the Internet, I realized that there was a problem with the docker command. When I used the "docker compose up -d" command, the container appears in Docker Desktop, but Symfony could not recognize this connection. So, I used "docker-compose" command instead, and it's started to work like a charm (but there wasn't a container visiable in Docker Desktop).
It's important to note, that you should disable the alias option in Docker Desktop settings (Settings -> General -> Enable 'docker-compose' CLI alias), otherwise there will be no effect.
Hey @bam1to ,
Thanks for sharing this tip with our Docker friends! Also, don't forget to execute
symfony consoleinstead ofbin/consolewhen using Docker to avoid connection problems.Cheers!
Hello, having issues making Symfony see my docker.
Symfony is running. Docker is running.
But when I open localhost server does not see my database running and DATABASE_URL port is not changed...
Not sure what is the issue
Docker version 20.10.17, build 100c701
Using Ubuntu
In next tutorial video I tried running: ./bin/console doctrine:database:create
And I got this:
An exception occurred in the driver: could not find driverHey Algirdas,
When you use Docker + Symfony, you got to run any commands through the Symfony CLI. For example
symfony console doctrine:database:createor to spin up the web serversymfony serve -dI hope it helps. Cheers!
Hi, so not sure what fixed it. Today I tried launching and had issues connecting to database not to mention the issue I already had. Reinstalled postgresql on my machine and now it works
Hey Algirdas!
Weird! Database gremlins hiding somewhere :). Anyways, I'm glad it's behaving for you now!
Cheers!
Is there any difference between using
php bin/console and symfony console
? Or is it exactly the same ?
Hey @ling
It has a few subtle differences. If you use Docker, you can execute your commands through Symfony CLI to let it set up all of the env vars. Also, you can configure what PHP version you want to use per project, you can read more about it here: https://symfony.com/doc/current/setup/symfony_server.html#selecting-a-different-php-version
Cheers!
Hi,
are you planning to make a video on how to dockerise the full app ie database + php + web server ?
I am struggling with docker configs
Hey mofogasy
There is no plan to make a tutorial about it soon but thank you for telling us what you would like to learn. We take in consideration all feedback when we select the topic of the next tutorial
Thanks!
"Houston: no signs of life"
Start the conversation!