Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Docker & Environment Variables

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

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.

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.

Leave a comment!

2
Login or Register to join the conversation
mofogasy Avatar
mofogasy Avatar mofogasy | posted 3 months ago

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

Reply

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!

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// 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
    }
}