Docker Community Forums

Share and learn in the Docker community.

How to edit a file in a running container?

I have a container and in it is a php.ini file. It is located at /opt/bitnami/php/lib/php.ini . I’d like to modify this file and then restart the container. Two questions: 1) if I restart the container (docker-compose down/up) will my mods to the php.ini remain? 2) what tool can I use to “live edit” the file in the container? I saw somewhere that sed and awk are both available. I don’t know how to use either. Which is easier (or is there another one?)

use docker cp to copy the file from a running container to the host. Edit it one the host. Add a volume to your docker-compose.yml that mapps the host file on /opt/bitnami/php/lib/php.in in the container. Container are ephemeral/disposable. Whenever a container is recreated, it will start from the scratch. Ony data written in volume will survive.

Thank you. I managed to get phpinfo() to run by just inserting the line phpinfo(); into my php application. That was pretty stupid of me. Anyway, I can’t seem to get the postgresql driver working in my php.ini and thus is my container Now I am going to try inserting “RUN apt-get update && apt-get install -y libpq-dev && docker-php-ext-install pdo pdo_pgsql” into the Dockerfile. But honestly, I am not sure how the docker-compose.yml is getting the Dockerfile to recompile the image.

Does the service has a “build:” setting in you docker-compose.yml? If it doesn’t, then there is no way it could know how to build the image, would it?

Yes, I was thinking that same thing. Here is the Dockerfile. I think it should be being re-built, but I am surely not a docker expert!
FROM bitnami/php-fpm:7.2 as builder
RUN install_packages git autoconf build-essential
WORKDIR /app
RUN wget https://github.com/xdebug/xdebug/archive/2.6.0.tar.gz &&
tar xzf 2.6.0.tar.gz &&
cd xdebug-2.6.0 &&
phpize &&
./configure --enable-xdebug &&
make && make install
RUN apt-get update && apt-get install -y libpq-dev && docker-php-ext-install pdo pdo_pgsql

FROM bitnami/php-fpm:7.2
COPY --from=builder /opt/bitnami/php/lib/php/extensions/xdebug.so /opt/bitnami/php/lib/php/extensions/
RUN echo ‘zend_extension="/opt/bitnami/php/lib/php/extensions/xdebug.so"’ >> /opt/bitnami/php/etc/php.ini

RUN echo “xdebug.remote_port=9000” >> /opt/bitnami/php/etc/php.ini
&& echo “xdebug.remote_enable=1” >> /opt/bitnami/php/etc/php.ini
&& echo “xdebug.remote_connect_back=0” >> /opt/bitnami/php/etc/php.ini
&& echo “xdebug.remote_host=192.168.122.1” >> /opt/bitnami/php/etc/php.ini
&& echo “xdebug.idekey=docker” >> /opt/bitnami/php/etc/php.ini
&& echo “xdebug.remote_autostart=1” >> /opt/bitnami/php/etc/php.ini
&& echo “xdebug.remote_log=/tmp/xdebug.log” >> /opt/bitnami/php/etc/php.ini
&& echo “extension=pgp_pdo_pgsql.so” >> /opt/bitnami/php/etc/php.ini
&& echo “extension=pgp_pgsql.so” >> /opt/bitnami/php/etc/php.ini

I “run” this by taking the containers down and up by docker-compose down/up. But the phpinfo doesn’t show the pgsql as being enabled!

And here is the docker-compose.yml too:
version: ‘3’

services:

apache:
image: bitnami/apache:latest
restart: unless-stopped
ports:
- 80:8080
volumes:
- ./apache/app.conf:/vhosts/app.conf:ro
- ./app:/app
networks:
- net

mysql:
container_name: “mysql”
restart: unless-stopped
image: mysql:5.6
environment:
- MYSQL_DATABASE
- MYSQL_PASSWORD
- MYSQL_ROOT_PASSWORD
- MYSQL_USER
volumes:
- data:/var/lib/mysql
- ./mysql/mysql.sql:/docker-entrypoint-initdb.d/mysql.sql
ports:
- “3306:3306”
networks:
- net

phpmyadmin:
container_name: phpmyadmin
restart: unless-stopped
image: phpmyadmin/phpmyadmin
environment:
- PMA_HOST=mysql
- PMA_PORT=3306
ports:
- “8081:80”
networks:
- net

php-fpm:
build: ./php
restart: unless-stopped
image: bitnami/php-fpm
volumes:
- ./app:/app
environment:
- XDEBUG_CONFIG=“remote_host=192.168.122.1”
networks:
- net

jasperreports:
image: ‘bitnami/jasperreports:7’
restart: unless-stopped
environment:
- MARIADB_HOST=mysql
- MARIADB_PORT_NUMBER=3306
- JASPERREPORTS_USERNAME=admin
- JASPERREPORTS_PASSWORD=bitnami
- JASPERREPORTS_DATABASE_USER=admin
- JASPERREPORTS_DATABASE_PASSWORD=xxx
- JASPERREPORTS_DATABASE_NAME=jasper
- ALLOW_EMPTY_PASSWORD=yes
ports:
- ‘8080:8080’
volumes:
- jasperreports_data:/bitnami
depends_on:
- mysql
networks:
- net

volumes:
data:
driver: local
jasperreports_data:
driver: local

Metin, the problem is that I don’t have the php.ini that is being used in the /bitnami/php-fpm container. It was built by bitnami, not me. I have managed to get xdebug working on it by using appending some commands onto the bottom of it. I then tried to do the same thing with the pgsql commands, but it didn;t work. Then I added : RUN apt-get update && apt-get install -y libpq-dev && docker-php-ext-install pdo pdo_pgsql
to load the pgsql but it didn’t work. You can see the Dockerfile I am using below.

Seems I should have quoted docker cp like i did now in my first response. This litteraly is the command to copy files from a running container to the host and the other way arround. Read the docs, apply what you understood, if you fail share what you did and we will see.

I won’t be able to help you with your php issues. I am a java enterprise guy… when it commes to docker, the painpoints couldn’t be any more different.

well, thanks for your help! Have a good night

There are two ways in which we can get inside the running container to edit the configuration file:
Open a shell and edit the file from the command line.
Remote into the container and make the changes with our editor.

This is what I use to insert the MySQL driver:
RUN apk update;
apk upgrade;
RUN docker-php-ext-install pdo_mysql
You should be able to do the same for Postgres (from a post on another forum):
RUN apt-get update && apt-get install -y libpq-dev && docker-php-ext-install pdo pdo_pgsql