Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
65
Q:

Docker Compose: Set variable in php.ini from Dockerfile

Hey everyone,

I'm relatively new to Docker Compose and I'm trying to figure out how to set a variable in my `php.ini` file using a Dockerfile. I've been searching online for a solution, but I haven't had any luck so far.

Basically, I have a PHP application that requires a specific value to be set in the `php.ini` file. I know that I can manually edit the `php.ini` file inside the container, but I would like to automate this process using a Dockerfile.

Is there a way to pass a variable from the Dockerfile to the `php.ini` file during image build time? I was thinking of using an environment variable, but I'm not sure how to set the variable inside the `php.ini` file.

Any help or guidance would be greatly appreciated. Thanks in advance!

All Replies

antonetta.abshire

Hey folks,

I've also dealt with a similar situation in my Docker Compose setup, and I found an alternative method to set variables in `php.ini` using environment variables and Docker Compose's `env_file` directive.

Firstly, I created a separate file called `php.env` in my project directory. Inside this file, I listed the environment variables I wanted to set, each with its respective value:


MY_VARIABLE=my_value


In my Docker Compose file, I added the `env_file` directive under the PHP service, referencing the `php.env` file:

yaml
version: '3'
services:
php:
build:
context: .
dockerfile: Dockerfile
env_file:
- ./php.env


By specifying the `env_file` directive, Docker Compose automatically reads the environment variables from the `php.env` file and passes them to the PHP container. These environment variables will be available within the container, including during the image build process.

In my Dockerfile, I could then set the desired variable in `php.ini` by utilizing the environment variable passed from the Docker Compose file. Here's an example:

dockerfile
FROM php:7.4-fpm

ARG MY_VARIABLE
ENV MY_VARIABLE=${MY_VARIABLE}

RUN sed -i "s/;custom_variable =/custom_variable = ${MY_VARIABLE}/" /usr/local/etc/php/php.ini


In the above code, I used `sed` to modify the desired variable in the `php.ini` file, replacing `;custom_variable =` with the value of the environment variable.

This approach allows me to easily configure and modify variables in `php.ini` using environment variables defined in the `php.env` file, providing flexibility and ease of management.

I hope you find this approach helpful! Let me know if you have any further questions or need more assistance.

estelle76

Hey,

I've faced a similar issue before, and I managed to solve it by using a combination of Docker Compose and a custom configuration file for PHP.

First, I created a separate `php.ini` file in my project directory, let's call it `custom.ini`. This file contains the specific variable I wanted to set, with the desired value.

Next, in my Dockerfile, I copied this `custom.ini` file to the appropriate location inside the container. For example, if I'm using the official PHP image, I would copy it to `/usr/local/etc/php/conf.d/` like so:

dockerfile
FROM php:7.4-fpm

COPY custom.ini /usr/local/etc/php/conf.d/


By copying the file to this directory, it will be automatically loaded by PHP during startup, and any configurations specified in `custom.ini` will override the default values.

Finally, in my `docker-compose.yml` file, I linked the PHP service with the custom configuration file:

yaml
version: '3'
services:
php:
build:
context: .
dockerfile: Dockerfile
volumes:
- ./custom.ini:/usr/local/etc/php/conf.d/custom.ini
# ...


With this setup, whenever I build and run my Docker Compose stack, the custom configuration file will be passed into the PHP container, and the specified variable will be set as expected in the `php.ini` file.

I hope this helps! Let me know if you have any further questions or if you need clarification on any part of the process.

kyra.balistreri

Hey there,

I've also encountered a similar requirement in my project, and I found a slightly different approach to set variables in the `php.ini` file using Docker Compose and a Dockerfile.

Instead of creating a separate configuration file, I preferred passing the variable directly as an environment variable to the PHP container and utilizing the `sed` command to modify the `php.ini` file during image build.

Firstly, in my Dockerfile, I installed `sed` and utilized it to replace a specific placeholder value in the `php.ini` file with the environment variable passed from the Docker Compose file. Here's an example snippet:

dockerfile
FROM php:7.4-fpm

RUN apt-get update && apt-get install -y sed

ARG MY_VARIABLE
ENV MY_VARIABLE=${MY_VARIABLE}

RUN sed -i "s|<MY_VARIABLE>|${MY_VARIABLE}|" /usr/local/etc/php/php.ini


In the above code, `<MY_VARIABLE>` is a placeholder in the `php.ini` file that I wanted to replace with the value of the environment variable.

Next, in my Docker Compose file, I defined the environment variable and linked it with the PHP service:

yaml
version: '3'
services:
php:
build:
context: .
dockerfile: Dockerfile
args:
- MY_VARIABLE=my_value


By using this approach, I could easily set the desired variable directly in the `php.ini` file during the image build process without any additional configuration files.

Feel free to give it a try and let me know if you have any questions or need further assistance!

New to LearnPHP.org Community?

Join the community