Chapters
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
All right, we have Doctrine installed! But now we need, ya know, to get a database server running.
DATABASE_URL
Environment Variable
Take a look at our .env
file. When we installed Doctrine, the Flex recipe added this doctrine-bundle section. The DATABASE_URL
environment variable is where we tell Doctrine how to connect to our database. It's a special URL-looking string called a DSN, if you want some nerdy terminology.
It holds the type of database we're connecting to - mysql
, postgres
, sqlite
, borgsql
, etc, a username, password, host, port, and the database name. Any query params are extra config.
By default, the DATABASE_URL
is set to connect to a Postgres database and that's what we'll use. We'll get it running super easily with Docker.
If you don't want to use Docker, no problem! Comment out this line and uncomment the sqlite
one. SQLite doesn't require a server: it's just a file on your filesystem. Because Doctrine abstracts the database layer, for the most part, the code we write will work with any database type. Cool!
Remember, don't store any sensitive info in this file: it's committed to your repo. If you have your own database server locally, create a .env.local
file (this is ignored by Git), and set your own DATABASE_URL
there.
Starting a Postgres Container with Docker
Ok, so how can we get a Postgres database server running?
Take a look at compose.yaml
. This was added by a Flex recipe and holds Docker config, including this database
service to spin up a Postgres container. Fantastic! You can do whatever you want, but we are only going to use Docker as a convenient way to run a database server locally. PHP itself is installed normally on my machine.
Open your terminal and run:
docker compose up -d
This start the Docker containers and -d
tells Docker to do it all in the background.
But where is the database server running? Like what port? Don't we need to know so we can update DATABASE_URL
to point to it?
The Symfony CLI is Awesome!
No! The symfony
CLI binary that's running the web server has some Docker magic! Jump over and refresh the app. Down here, hover over "Server". This holds details about the Symfony CLI server. This part means that it automatically detected our docker containers and set up the environment variables for us!
Tip
In order for the automatic detection to work, your local project needs to be a Git repository.
If following along with the downloaded code, you'll need to initialize one with git init
.
I'll show you. Pop over to our terminal and run:
symfony var:export --multiline
This shows us some extra environment variables that the Symfony CLI is setting for us, in addition to the ones in .env
.
Scroll up a bit to see.... Ah! Here it is! DATABASE_URL
! This overrides the one in .env
and points to the Postgres database running in Docker. That port number will randomly change, but the Symfony CLI will always use the correct one.
symfony console
vs bin/console
Now, we're used to running Symfony commands with bin/console
. But when using the Symfony CLI with a Docker database, we need to run the database-specific commands through symfony console
instead. It's the same as bin/console
, but it gives the Symfony CLI a chance to add the environment variables.
Creating the Database
Ok! Database server running in a Docker container and DATABASE_URL
is pointing to it. To create the database, run:
symfony console doctrine:database:create
An error?! No worries! The error is telling us the database already exists: apparently the server comes with one. But this is good, it means we are connecting to our database server!
Ok crew, we have Doctrine and a database. Now we need a table! We'll do that next by jump lifting off into the world of entities and migrations.
30 Comments
Hey Nathan,
Thanks for sharing this edge case with others. Actually, I think I always work with projects that are already with initiated Git repo, so I haven't faced the problem you explained here. Though, for me it's weird that Docker compose does not work properly if you don't have .git/ folder, IMO Docker should not care about Git repo, it should care about the folder in which you're currently cd
-ed to. But I may be wrong, or maybe it depends on some special system settings. Anyway, maybe it will be useful for someone.
Cheers!
Yeah, I only didn't have it initialized because I had just downloaded the code here.
It's actually the Symfony CLI that uses the .git folder. It uses that to find the name of the directory that the project is in, and then looks for a Docker container running with the same name. No .git folder, Symfony CLI didn't detect the project name, and without the project name it didn't know which Docker container to connect to.
Someone opened a PR to the CLI to improve the directory detection just last week, actually 🙂
Hey Nathan,
Oh, I see it now! Yeah, I has probably never faced it because always have Git initialized, but when you download the course code - it makes sense there's no Git there, though it's a good idea to init it first.
Good catch actually! And thanks for sharing this with others, I didn't know about this behavior. It sounds like it should be fixed upstream to be more robust.
Cheers!
Hello! There is no server tab in my profiler. I didn't realize it until now. I already tried to restart the symfony server as Martin-C suggested but it does not work. I'll appreciate your help.

Hey @Octavio-B
Did you download the course code from this page? What web server are you using?
Do you see any errors in the console?
Cheers!
Hey MolloKhan
I'm building my own app while following these tutorials, so most of the packages installed are the most recent; so, in this case the profiler might be a different version from the one of the course. Though, I don't think that's the issue here.
I'm using the Symfony Local Web Server. I would also like to point out that I have another app where this tab is shown and I checked the configuration of the profiler in both apps, and it seems to be the same.
The console doesn't show any errors, until I try to create the database:
octavio@octavio-HP-EliteDesk-800-G5-SFF:~/aca_finanzas$ symfony console doctrine:database:create
Could not create database "app" for connection named default
An exception occurred while executing a query: SQLSTATE[42P04]: Duplicate database: 7 ERROR: database "app" already exists

I'm not sure what's causing the server tab to disappear, but if you open up the request page, do you see the "configuration" tab on the sidebar?
Yes, the "configuration" tab is enabled.

if you open up the phpinfo
page do you see the $_SERVER
and $_ENV
variables? Something in your config is preventing the server tab from showing up
Yes, both variables are visible with distinct values. Actually, everything else works fine. Now I'm thinking it has to do with a new version of the "debug" package.

Are you on the latest version? Try upgrading if you're not. Also, check the app logs, perhaps there's a hint of what's going on
I'm on the latest version of everything except php, which is the version 8.3.23. I'm looking at the app logs but I don't know what to look for. I don't see any log about the profiler, though.
Even if I create a new project, the server tab won't appear.
Hey @Octavio-B,
This is a known issue in the SymfonyCLI. If you upgrade to the latest version (5.12.0+), it should be fixed.
--Kevin


martin@martin-inspiron153530:~/Projects/starshop(master)$ symfony console doctrine:database:create
Could not create database "app" for connection named default
An exception occurred in the driver: could not find driver
martin@martin-inspiron153530:~/Projects/starshop(master)$ php -m | grep pdo
pdo_sqlite
martin@martin-inspiron153530:~/Projects/starshop(master)$ sudo apt install php-pgsql
[sudo] password for martin:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
php-pgsql is already the newest version (2:8.4+96+ubuntu24.04.1+deb.sury.org+1).
0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.

Hey @Martin-C
Is your database server up and running?
Cheers!


okay, I've got it solved ... had to turn off docker compose running my other projects and work just with the startshop one. I haven't realized the other one is running and I had a lot of containers mixed up there. Now it's all fine.
Hey @Martin-C,
Glad you got it sorted. The Symfony CLI is usually smart enough to account for multiple containers but might have been some port duplication issues.
--Kevin


CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b1b2619ddf1b postgres:16-alpine "docker-entrypoint.s…" 3 days ago Up 3 days (healthy) 0.0.0.0:32772->5432/tcp, [::]:32772->5432/tcp starshop-database-1


Hi, yes it is...
sudo systemctl status postgresql
[sudo] password for martin:
postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled)
Active: active (exited) since Thu 2025-05-29 14:04:24 CEST; 3 weeks 3 days ago
Main PID: 1769 (code=exited, status=0/SUCCESS) CPU: 1ms
may 29 14:04:24 martin-inspiron153530 systemd[1]: Starting postgresql.service - PostgreSQL RDBMS...
may 29 14:04:24 martin-inspiron153530 systemd[1]: Finished postgresql.service - PostgreSQL RDBMS.


in my case, there is no server tab in bottow right corner, only symfony

Hey @Martin-C,
That's weird. Are you on mobile, or perhaps the toolbar is not expanded?
Do you see any errors in the console?
Cheers!


okay, got it working ... I just had to restart symfony server


hi, this is the server status
martin@martin-inspiron153530:~/Projects/starshop(master)$ symfony server:status
Local Web Server
Listening on http://127.0.0.1:8001
The Web server is using PHP FPM 8.4.8 (from default version in $PATH)
Local Domains
Workers
PID 75896: symfony console tailwind:build --watch
PID 75887: /usr/bin/php8.3 -S 127.0.0.1:44845 -d variables_order=EGPCS /home/martin/.symfony5/php/8bebc3742d26aca368056064cc39b71a5694968f/8.3.21-router.php
Environment Variables
Exposed from Docker


When I set up everything and run "docker compose up -d" I get the "Container starshop-database-1 Started" response, however the Server tooltip shows docker as down ... which is different to what you are seeing.
Any suggestions?
Hey @John-B, what version of the Symfony CLI are you using? (symfony version
)


Symfony CLI 5.10.5
I see this issue looks similar. Does the workaround posted there help at all?


It's working now, thanks.
It was a permission issue ...
I had run docker compose with a "user" permission while symfony was served with "root" permissions.
I might have to restart the project with "user" permissions rather than "root" permissions to get around permission problems.
Initially I had forgot to modify the /var/www permissions to allow write for local users and just got around the issue using sudo ... silly me..
Awesome, I'm so happy this is resolved for you!

"Houston: no signs of life"
Start the conversation!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"babdev/pagerfanta-bundle": "^4.5", // v4.5.0
"doctrine/dbal": "^3", // 3.9.4
"doctrine/doctrine-bundle": "^2.13", // 2.13.2
"doctrine/doctrine-migrations-bundle": "^3.3", // 3.4.0
"doctrine/orm": "^3.3", // 3.3.1
"knplabs/knp-time-bundle": "^2.2", // v2.4.0
"pagerfanta/doctrine-orm-adapter": "^4.7", // v4.7.1
"php-cs-fixer/shim": "^3.46", // v3.65.0
"phpdocumentor/reflection-docblock": "^5.3", // 5.6.0
"phpstan/phpdoc-parser": "^1.25", // 1.33.0
"stof/doctrine-extensions-bundle": "^1.12", // v1.13.0
"symfony/asset": "7.1.*", // v7.1.6
"symfony/asset-mapper": "7.1.*", // v7.1.9
"symfony/console": "7.1.*", // v7.1.8
"symfony/dotenv": "7.1.*", // v7.1.9
"symfony/flex": "^2", // v2.4.7
"symfony/framework-bundle": "7.1.*", // v7.1.6
"symfony/http-client": "7.1.*", // v7.1.9
"symfony/monolog-bundle": "^3.0", // v3.10.0
"symfony/property-access": "7.1.*", // v7.1.6
"symfony/property-info": "7.1.*", // v7.1.9
"symfony/runtime": "7.1.*", // v7.1.7
"symfony/serializer": "7.1.*", // v7.1.9
"symfony/stimulus-bundle": "^2.13", // v2.22.0
"symfony/twig-bundle": "7.1.*", // v7.1.6
"symfony/ux-turbo": "^2.13", // v2.22.0
"symfony/yaml": "7.1.*", // v7.1.6
"symfonycasts/tailwind-bundle": "^0.7.1", // v0.7.1
"twig/extra-bundle": "^2.12|^3.0", // v3.16.0
"twig/twig": "^2.12|^3.0" // v3.16.0
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.6", // 3.7.1
"symfony/debug-bundle": "7.1.*", // v7.1.6
"symfony/maker-bundle": "^1.52", // v1.61.0
"symfony/stopwatch": "7.1.*", // v7.1.6
"symfony/web-profiler-bundle": "7.1.*", // v7.1.9
"zenstruck/foundry": "^2.2" // v2.3.1
}
}
So, I know that I should have followed the instructions exactly and checked the git diff, but I didn't think it would matter. In this case, it actually really does matter!
The automated detection of Docker was not working for me despite following the ticket mentioned in the other thread here in the comments. When I ran the
symfony var:export
command with the debug flag, it said it couldn't find my compose directory.The actual problem is that the compose directory is located by finding the
.git
directory that your terminal is currently inside of. When there is no git directory, it doesn't default to the directory you ran the command inside of or anything, it just doesn't know what directory to use. Because it doesn't know what directory to use, it can't look inside of Docker to see if there are any containers running.So, make sure you initialize a git repo if you download the source code and want to use the Docker compose project shown here 😅