'Permission denied when attempting to edit docker container files

I'm working on a Dockerized project which has an Adminer container. I need to increase the value of post_max_size found in /usr/local/etc/php/conf.d/0-upload_large_dumps.ini.

My problem is any attempt to edit the file results in permission denied responses. Usually, this would be a prolem resolved by using sudo but I also get permission denied from that as well.

The following is the output of the directory I'm trying to edit showing the target file is owned by root:

/var/www/html $ cd /usr/local/etc/php/conf.d/
/usr/local/etc/php/conf.d $ ls -l
total 24
-rw-r--r--    1 root     root           113 Nov 18 22:10 0-upload_large_dumps.ini
-rw-r--r--    1 root     root            23 Nov 18 22:11 docker-php-ext-pdo_dblib.ini
-rw-r--r--    1 root     root            23 Nov 18 22:10 docker-php-ext-pdo_mysql.ini
-rw-r--r--    1 root     root            22 Nov 18 22:11 docker-php-ext-pdo_odbc.ini
-rw-r--r--    1 root     root            23 Nov 18 22:11 docker-php-ext-pdo_pgsql.ini
-rw-r--r--    1 root     root            17 Nov 18 17:03 docker-php-ext-sodium.ini

And the adminer section of docker-compose is as follows:

  adminer:
    image: adminer
    restart: always
    labels:
      - traefik.port=8080
      - traefik.frontend.rule=Host:adminer

How can I edit docker-compose so I have permissions to update the files?



Solution 1:[1]

There is nothing to change in your docker-compose.yaml.
If you want to change it, you can just exec in the container as the root user.

So I suppose that, right now, you are doing

docker compose exec adminer ash

And then, you are trying to edit those file.

What you can do, instead, is:

docker compose exec --user root adminer ash

So you will be able to adapt those files owned by root.


This said, mind that the philosophy of Docker is that a container should be short lived, so you would be better having your own Dockerfile to edit that configuration file for good. Another way to do it would be to mount a file over the existing one to change the configurations.

Example of adaptation in a Dockerfile:

FROM adminer 

COPY ./0-upload_large_dumps.ini \
     /usr/local/etc/php/conf.d/0-upload_large_dumps.ini
## ^-- copies a files from the folder where you build
## in order to override the existing configuration

Then in your docker-compose.yml:

  adminer:
    build: .
    image: your-own-namespace/adminer
    restart: always
    labels:
      - traefik.port=8080
      - traefik.frontend.rule=Host:adminer

Example of mounting a file to override the configuration file:

  adminer:
    image: adminer
    restart: always
    labels:
      - traefik.port=8080
      - traefik.frontend.rule=Host:adminer
    volumes:
      - "./0-upload_large_dumps.ini:\
        /usr/local/etc/php/conf.d/0-upload_large_dumps.ini"
## a local file `0-upload_large_dumps.ini` on your host
## will override the container ini file

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1