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.

Laravel Forge logo

Laravel Forge

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

Laravel Forge
Tinkerwell logo

Tinkerwell

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

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Laravel Idea for PhpStorm logo

Laravel Idea for PhpStorm

Ultimate PhpStorm plugin for Laravel developers, delivering lightning-fast code completion, intelligent navigation, and powerful generation tools to supercharge productivity.

Laravel Idea for PhpStorm
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
Bacancy logo

Bacancy

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

Bacancy
Lucky Media logo

Lucky Media

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

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
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
Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate logo

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate

Build your SaaS application in hours. Out-of-the-box multi-tenancy and seamless Stripe integration. Supports subscriptions and one-time purchases, allowing you to focus on building and creating without repetitive setup tasks.

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate
JetShip - Laravel Starter Kit logo

JetShip - Laravel Starter Kit

A Laravel SaaS Boilerplate and a starter kit built on the TALL stack. It includes authentication, payments, admin panels, and more. Launch scalable apps fast with clean code, seamless deployment, and custom branding.

JetShip - Laravel Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Permanent Record Deletion with Laravel's forceDestroy image

Permanent Record Deletion with Laravel's forceDestroy

Read article
DigitalOcean API Client for Laravel image

DigitalOcean API Client for Laravel

Read article
Interactive Console Commands in Laravel image

Interactive Console Commands in Laravel

Read article
Simplified Stream Response Handling in Laravel image

Simplified Stream Response Handling in Laravel

Read article
Television is a Multi-purpose Fuzy Finder TUI image

Television is a Multi-purpose Fuzy Finder TUI

Read article
Handling Process Synchronization with Laravel Cache Locks image

Handling Process Synchronization with Laravel Cache Locks

Read article