If you want to learn how to add a custom php.ini file to your apache-php image, install new extensions and edit configuration settings, stay with me for the next few minutes. As often, the best way to learn is to look on the working example so lets install xdebug and lets set some settings in php.ini file.

dockercompose.yml

Change image to build in dockercompose.yml

GitHub – Project

ersion: '3'
services:
  db:
    image: mysql:latest
    environment:
      MYSQL_DATABASE: lamp
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
      MYSQL_ROOT_PASSWORD: root
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
    volumes:
      - "./sql:/docker-entrypoint-initdb.d"
      
  app-8.1:
    # image: php:8.1-apache-buster <<<<<<<<<<<<<<<<
    build: ./apache/php/php8.1/
    volumes:
      - "./app:/var/www/html"
      # - "./apache/php/php8.1/conf.d:/usr/local/etc/php/conf.d"
    ports:
      - 81:80
    links:
      - db
    depends_on:
      - db

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - 8001:80
    environment:
      - PMA_HOST=db
      - PMA_PORT=3306
    depends_on:
      - db

Create new Dockerfile in ./apache/php/php8.1/ directory. Inside Dockerfile type:

FROM php:8.1-apache-buster

Dockerfile

Create your custom Dockerfile from Apache / PHP image and copy .ini file to with php.ini file with the fallowing command:

Create php.ini file

RUN cp /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini

Install new extension in docker apache-php image

Install new PHP extension using pecl and docker-php-ext-enable

# Install php extensions and creates aditional ini file in conf.d directory example: /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN pecl install xdebug \
	&& docker-php-ext-enable xdebug

RUN docker-php-ext-install pdo_mysql \
	&& docker-php-ext-enable pdo_mysql

Change existing settings in docker image – php.ini file.

Modify existing settings in php.ini.

sed -E -i 's/(;?)(post_max_size[[:space:]]=[[:space:]])[0-9]+M/\2328M/g' /usr/local/etc/php/php.ini

Add new / non-existing settings to docker image – in to php.ini file

# You can use this form of configuration when adding new settings to a configuration file
RUN echo '[Xdebug]\n\
pdo_mysql.debug = On\n\
xdebug.remote_enable = 1\n\
xdebug.remote_connect_back = On\n\
xdebug.remote_port = "9000"\n\
xdebug.profiler_enable = 0\n\
xdebug.remote_handler = dbgp\n\
xdebug.remote_mode = req\n\
xdebug.var_display_max_depth = 5\n\
xdebug.remote_autostart = true' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

Create bash script …

Now you know how to change setting in docker image for apache-php. You might think that it would be nice to create a bash script that will check if the php.ini setting is set, is commented out or should it add a new line to config file and use it instead. This is also what I think. Perhaps in next blogpost we will create such a script, so stay keep reading. Hope this post was helpful, if so leave a comment or let me know if we could improve this post in any way.

0
Would love your thoughts, please comment.x
()
x