Login to bookmark this video
Buy Access to Course
03.

Docker & Environment Variables

|

Share this awesome video!

|

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.

10 lines | 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.