Tinkerwell - The PHP Scratchpad

Flexible Docker Images with PHP INI Environment Variables

Last updated on by

Flexible Docker Images with PHP INI Environment Variables image

I recently posted about Get Xdebug Working With Docker and PHP 8.4 to show you how easy it is to get an Xdebug connection working. In that tutorial, I hard-coded INI settings to keep the tutorial focused on setting up Xdebug. In a real application, you want your Docker images to be flexible so each developer can configure their own settings without affecting the image configuration.

In this post, I'll show you how to use environment variables to make your images super flexible. As of v8.3, PHP supports fallback values with the INI environment variable syntax, so we can really make our INI configuration clean.

If you reference the previous tutorial, our xdebug.ini file looked like the following snippet:

; build/php/conf.d/xdebug.ini file
[xdebug]
xdebug.mode = debug
 
xdebug.client_host = host.docker.internal
 
; Or use the host machine IP address:
; xdebug.client_host = 192.168.86.203
 
xdebug.start_with_request = yes

If another developer on your team doesn't want to start Xdebug by default, they'd have to update the INI file, and either commit the change or just revert it. Gross.

That is no way to live, especially since PHP now supports default fallback values in INI files. Before we get to that, we could also configure Xdebug directly with environment variables. The following is equivalent to our xdebug.ini file:

services:
app:
build:
context: .
dockerfile: build/Dockerfile
target: development
ports:
- "8080:80"
volumes:
- .:/srv/app
environment:
XDEBUG_CONFIG: "client_host=0.0.0.0 start_with_request=yes"
XDEBUG_MODE: "debug,develop"

If we moved the environment values to an unversioned ENV file, each developer could manage Xdebug settings locally. Another way to configure Xdebug is through INI like we initially did, but using envrionment variables:

; build/php/conf.d/xdebug.ini file
[xdebug]
xdebug.mode = ${PHP_XDEBUG_MODE:-debug,develop}
 
xdebug.client_host = ${PHP_XDEBUG_CLIENT_HOST:-host.docker.internal}
 
xdebug.start_with_request = ${PHP_XDEBUG_START_WITH_REQUEST:-trigger}

I've prefixed these variables with PHP_ to avoid conflicts and quickly recognize which ENV values were meant to be used with INI configuration. For example, XDEBUG_MODE is reserved for Xdebug configuration directly, so if we want to configure it via our INI ENV variable we need a unique name.

If you restart the Docker image, you can verify these settings by adding phpinfo(); exit; to the top of public/index.php or connect to the container:

$ docker compose up --build -d
$ docker compose exec app bash
 
# In the container
$ php -i | grep xdebug\.start_with_request
xdebug.start_with_request => trigger => trigger

If you want to try customizing these values locally, add the following to your compose.yaml file:

services:
app:
build:
context: .
dockerfile: build/Dockerfile
target: development
ports:
- "8080:80"
volumes:
- .:/srv/app
+ env_file:
+ - .docker.env

Then, create a .docker.env and .docker.env.example file in the root of your project. Add sensible defaults to the example file and add .docker.env to your .gitignore file. Here's an example of the contents:

PHP_XDEBUG_MODE=debug
 
PHP_XDEBUG_CLIENT_HOST=host.docker.internal
# Or use your computer's local network IP
# PHP_XDEBUG_CLIENT_HOST=192.168.86.250
 
PHP_XDEBUG_START_WITH_REQUEST=trigger

You will need to recreate the container to see the updated ENV values, but once you do, you should see your ENV settings take effect:

And that's it, your INI settings are easy to modify without affecting the image build files.

You can now make your Docker images flexible using environment variables with INI settings. I also showed you how to use Xdebug's built-in environment variables to configure things if you prefer simplicity. However, not all settings can be set via the XDEBUG_CONFIG environment, so use the method of configuration that works best for you!

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Speeding Up Laravel News With Cloudflare image

Speeding Up Laravel News With Cloudflare

Read article
Livewire 4 Support in Laravel VS Code Extension v1.4.3 image

Livewire 4 Support in Laravel VS Code Extension v1.4.3

Read article
Fair Queue Distribution with Laravel Balanced Queue image

Fair Queue Distribution with Laravel Balanced Queue

Read article
Migrating Laravel News from Laravel Forge to Cloud image

Migrating Laravel News from Laravel Forge to Cloud

Read article
Laravel News Is the Live Stream Partner for Laracon EU 2026 image

Laravel News Is the Live Stream Partner for Laracon EU 2026

Read article
Query Builder Expression Aliases in Laravel 12.48 image

Query Builder Expression Aliases in Laravel 12.48

Read article