Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php on docker : Using setLocale

I'm trying to migrate an existing Apache/php site to docker and have problem with site localization. Digging into the code, the problem is that setLocale is returning false on Docker install (and true on the existing site). Here is a php test that runs well on existing site and fails on Docker installation.

<?php
$locale = "fr_FR";
putenv("LC_ALL=$locale");
$ok = setlocale(LC_ALL, $locale);
if ($ok) {
  echo "success";
} else {
  echo "failure";
}
?>

Here is my Docker file:

FROM php:5-apache

RUN apt-get update && apt-get install -y locales && apt-get clean
RUN locale-gen fr_FR && locale-gen zh_TW && locale-gen tr_TR && locale-gen ru_R$
RUN docker-php-ext-install gettext

RUN a2enmod rewrite && a2enmod headers

What am I doing wrong?

like image 455
Jean-Marc Astesana Avatar asked Sep 03 '16 16:09

Jean-Marc Astesana


People also ask

Can I run PHP in Docker?

Creating a PHP commandYou can use docker run to create a container and execute PHP. You just need to add some volumes to the container. These volumes should include the paths to your code.

What is Docker PHP ext configure?

docker-php-ext-enable - enables an already extension by adding a specific entry to php. ini. Extensions installed with pecl or native package managers may not be enabled by default thus require this additional step. As mentioned above, extensions installed with docker-php-ext-install are being enabled automatically.


2 Answers

You need to reconfigure your locales:

RUN locale-gen fr_FR.UTF-8 && dpkg-reconfigure locales

And you might need (but I don't know exactly under which circumstances) to add LC_ALL and LANGUAGE environment variables to /etc/environment:

LC_ALL=...
LANGUAGE=...
like image 114
Patxi Gortázar Avatar answered Sep 30 '22 17:09

Patxi Gortázar


As requested by "That Brazilian Guy", here is an answer with the fixed docker file

FROM php:5-apache

RUN apt-get update && apt-get install -y locales && apt-get clean
RUN sed -i -e 's/# fr_FR ISO-8859-1/fr_FR ISO-8859-1/' /etc/locale.gen && \
    dpkg-reconfigure --frontend=noninteractive locales
RUN docker-php-ext-install gettext

RUN a2enmod rewrite && a2enmod headers
If you need more locales, you'll have to look at /etc.locale.gen in your container and add a line

sed -i -e 's/# locale/locale/' /etc/locale.gen && \

for every locale you need (assuming # locale is the content of a line in /ec tc/locale.gen containing the locale you need).

like image 34
Jean-Marc Astesana Avatar answered Sep 30 '22 18:09

Jean-Marc Astesana