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
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
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 $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
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
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
Join the Mastering Laravel community logo

Join the Mastering Laravel community

Connect with experienced developers in a friendly, noise-free environment. Get insights, share ideas, and find support for your coding challenges. Join us today and elevate your Laravel skills!

Join the Mastering Laravel community
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
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

The latest

View all →
Auto-translate Application Strings with Laratext image

Auto-translate Application Strings with Laratext

Read article
Simplify Factory Associations with Laravel's UseFactory Attribute image

Simplify Factory Associations with Laravel's UseFactory Attribute

Read article
Improved Installation and Frontend Hooks in Laravel Echo 2.1 image

Improved Installation and Frontend Hooks in Laravel Echo 2.1

Read article
Filter Model Attributes with Laravel's New except() Method image

Filter Model Attributes with Laravel's New except() Method

Read article
Arr::from() Method in Laravel 12.14 image

Arr::from() Method in Laravel 12.14

Read article
Streamline API Resources with Laravel's Fluent Methods image

Streamline API Resources with Laravel's Fluent Methods

Read article