gstreamer0.10-ffmpeg
gstreamer0.10-plugins-good
packages.
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.
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.
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.
Hey Algirdas,
When you use Docker + Symfony, you got to run any commands through the Symfony CLI. For example symfony console doctrine:database:create
or to spin up the web server symfony serve -d
I 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!
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!
// composer.json
{
"require": {
"php": ">=8.1",
"ext-ctype": "*",
"ext-iconv": "*",
"babdev/pagerfanta-bundle": "^3.7", // v3.7.0
"doctrine/doctrine-bundle": "^2.7", // 2.7.0
"doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.2
"doctrine/orm": "^2.12", // 2.12.3
"knplabs/knp-time-bundle": "^1.18", // v1.19.0
"pagerfanta/doctrine-orm-adapter": "^3.6", // v3.6.1
"pagerfanta/twig": "^3.6", // v3.6.1
"sensio/framework-extra-bundle": "^6.2", // v6.2.6
"stof/doctrine-extensions-bundle": "^1.7", // v1.7.0
"symfony/asset": "6.1.*", // v6.1.0
"symfony/console": "6.1.*", // v6.1.2
"symfony/dotenv": "6.1.*", // v6.1.0
"symfony/flex": "^2", // v2.2.2
"symfony/framework-bundle": "6.1.*", // v6.1.2
"symfony/http-client": "6.1.*", // v6.1.2
"symfony/monolog-bundle": "^3.0", // v3.8.0
"symfony/proxy-manager-bridge": "6.1.*", // v6.1.0
"symfony/runtime": "6.1.*", // v6.1.1
"symfony/twig-bundle": "6.1.*", // v6.1.1
"symfony/ux-turbo": "^2.0", // v2.3.0
"symfony/webpack-encore-bundle": "^1.13", // v1.15.1
"symfony/yaml": "6.1.*", // v6.1.2
"twig/extra-bundle": "^2.12|^3.0", // v3.4.0
"twig/twig": "^2.12|^3.0" // v3.4.1
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.2
"symfony/debug-bundle": "6.1.*", // v6.1.0
"symfony/maker-bundle": "^1.41", // v1.44.0
"symfony/stopwatch": "6.1.*", // v6.1.0
"symfony/web-profiler-bundle": "6.1.*", // v6.1.2
"zenstruck/foundry": "^1.21" // v1.21.0
}
}
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 driver