How can i edit php.ini file in docker container? please give good explanation
Good evening,
there are different approaches to fulfill this task.
The following ones come to my mind - sorted from worst to best.
You can either open a shell into your running container and then edit the file. docker exec -it <containername> bash
or docker exec -it <containername> ash
and then use vim
to edit the file. Maybe you need to install vim
or the editor of your choice. This is not a good idea because the most common use of php (the php-fpm-image) reads the file on startup of the container and if you create a new instance of the image (= a new container) you have the same issue that the file contains content you do want to change.
You can copy a file from the running container to your docker-host docker cp .....
, edit the file on your host and copy it back into the container. Advantage over the idea described above: you don’t need to install additional software into the container. But you still have the other disadvantages as above.
Another idea is to create the file on your host (or copy it from a running container to your host) and edit it according to your needs. Then you can create a Dockerfile
which creates a new image based on your php-image which ADD
s or COPY
s the modified php.ini
into the image. Then every instance of this image (aka a container) contains the modified php.ini
.
Best idea (at least in my eyes) is to create the file on your host (or copy it from a running container to your host) and edit it according to your needs. Then you stop your PHP-container and remove it. Now you create your PHP-container again, but this time you mount the php.ini-file located on your host into the container docker run --volume ...
. Of course the same thing is possible with docker-compose
.
Hello, the last idea is good, but still a disadvantage. We have to reconfigure the OS of the container. In my case, every change in the php.ini I have to remove container and run it again which means I’ll end up with new virgin setup of linux image then I have to install nano and reconfigure /etc/apache/sites-available/xxxxx.conf again and again.
Hello parazitenew,
maybe I don’t get the whole picture?
For me I have this setup:
- I have a
Dockerfile
to create an image based onphp:8.1-fpm
, install some packages and build some PHP-modules. I rebuild this image every time there is a new version of the base-image is available. - The
php.ini
is located next to the mydocker-compose.yml
where it is mounted into the container upon start. - After re-building this image I run
docker-compose up -d
which re-creates the container with the new image and re-reading thephp.ini
. - If I have to modify a setting within the
php.ini
(which does not need a new package/module not available in the image up to now) I simply change thisphp.ini
and then stop and start the container again so that the PHP-service within the container re-reads thephp.ini
.
Best regards
Matthias
Sounds like an unpleasant and cumbersome approach. If you have to perform manual tasks inside a container for anything other than for troubleshooting or debugging purposes, then it’s not unlikely that you are doing something undesirable.
A combination of the last two suggestions of @matthiasradde is the proper way to prevent unnecessary manual actions inside the container. Especially, since containers are ephemeral by design. Create a Dockerfile to automate the creation of your image. Use a compose file to start a container of the image. During development just map host folders or files as volumes into container folders or files, once everything is as you need, use a COPY instruction in the Dockerfile to copy the files into the image during build time.