Mastering docker php ext install intl Commands with Docker

Posted November 15, 2023
Mastering docker php ext install intl Commands with Docker

Ext-intl adds internationalization and localization within PHP apps. If you want to add ext-intl extension to a Dockerized PHP environment using the docker php ext install intl command, you are in the right hands.

This article effectively integrates the PHP ext-intl extension within Docker environments. You will learn how to perfectly run docker php ext install intl command when setting up a Dockerized PHP app.

Related: How to use RUN docker php ext install mysqli MySQL Command with Docker

Step 1: Setting Up Ext-intl PHP Docker Development

You want your PHP app to have multilingual support, character encoding conversions, and locale-aware operations. Ext-intl is indispensable here. However, it might be tricky to set it on Docker.

First, let’s assume your app looks as follows:

php-intl/
│ ├── intl.dockerfile
│ └── docker-compose.yml
└── app/
    └── index.php

Now, Dockerfile allows you to craft PHP extension installations. Therefore, you will update this file and add the ext-intl extension to support internationalization tasks as follows:

# Configuring PHP ext-intl environment
FROM php:7.2-apache

RUN apt-get update \
# Add any needed libraries before Intl
    && apt-get install -y \
        libicu-dev \
    # Add PHP ext-intl to the Docker environment
    && docker-php-ext-install -j$(nproc) intl

Here:

  • The libicu-dev package creates development headers and libraries required to build the ext-intl extension for PHP.
  • The docker-php-ext-install -j$(nproc) intl will compile and install intl.

Step 2: Running Ext-Intl Extension in a Dockerized PHP Application

Dockerfile will package PHP and allow Apache to expose your app on the web while ensuring that intl is installed. Now, you will spin a container to package your whole application environment using Docker Compose.

Go to your docker-compose.yml file and create a PHP container to serve your script as follows:

version: '3.9'

services:
  php-apache:
    build:
    # This should reflect to the name and path of dockerfile
      context: .
      dockerfile: intl.dockerfile
    ports:
      - "8080:80"
      # Copy PHP script to Docker Apache working folder 
    volumes:
      - ./app:/var/www/html 

Remember that you must share your app/index.php file with Docker. volumes: - ./app:/var/www/html will do so. Therefore, you need to script in this file and use the intl extension.

Step 3: Creating a Simple PHP app using Intl Extension

In your app/index.php file, you will create a simple test script to allow you to check if Intl works on Docker using the following sample code:

<?php
// PHP test code to verify ext-intl functionality within the Docker container
// Check if the 'intl' extension is loaded
if (extension_loaded('intl')) {
    echo "The 'intl' extension is installed and loaded." . "<br>";

$date = new DateTime('2023-11-23', new DateTimeZone('America/New_York'));
echo $date->format('Y-m-d H:i:sP'); . "<br>" // Output: 2023-11-23 00:00:00-05:00

$formatter = new NumberFormatter('en_US', NumberFormatter::DECIMAL);
echo $formatter->format(1234567.891); . "<br>" // Output: 1,234,567.891 

} else {
    echo "The 'intl' extension is not installed or loaded.";
}

This will check if the ‘intl’ extension is ready and perform some ext-intl functionalities.

Step 4: Running and Testing PHP Applications with Intl Extension and Docker

Check and Excute the following docker-compose command to package Intl and PHP with Docker:

docker-compose up -d --build

Mastering docker php ext install intl Commands with Docker

Confirm if the created Docker container is running as expected:

Mastering docker php ext install intl Commands with Docker

Access your app on http://localhost:8080:

Mastering docker php ext install intl Commands with Docker

This shows your docker php ext install intl command works perfectly within your Dockerized PHP environment.

Conclusion

You have comprehensively learned how to use the docker php ext install Intl command within a Dockerized PHP environment. Now, unlock the full potential of globally accessible, multilingual content and manipulate Unicode strings, format dates, currencies, and language translations while leveraging Docker.

Mastering docker php ext install intl Commands with Docker

Written By:

Joseph Chege