Skip to content

Change common php settings via environment variable #765

Closed

Description

@J0WI

There was a request to change memory_limit via environment variable:
nextcloud/docker#568

There are other settings that are frequently requested to change, like the upload limit.
Is it worth to allow some common settings to be set via environment variables?

https://secure.php.net/manual/en/configuration.file.php#example-36

Activity

  1. rcdailey commented on Dec 12, 2018

    @rcdailey

    You could go for the kitchen-sink solution and just map all environment variables with a specific prefix to their corresponding php.ini settings, such as a prefix like PHP_INI_. So if I specified PHP_INI_MEMORY_LIMIT=2G, you'd have some generic code that removed the prefix, did lower case conversion of what's left, and inserted it into a php.ini, so you'd end up with memory_limit=2G.

    Something like that would be pretty sophisticated I think, and given my merge request for specifically just memory limit, it would be really nice to get that in first (since it's a quick stopgap at the very least) and then replace it later with a more flushed-out and generic system (assuming that's the direction you're trying to go)

  2. added
    RequestRequest for image modification or feature
    on Dec 12, 2018
  3. J0WI commented on Dec 14, 2018

    @J0WI
    ContributorAuthor

    @yosifkit would you accept a PR implementing this?

  4. TimWolla commented on Dec 19, 2018

    @TimWolla
    Contributor
  5. tianon commented on Dec 24, 2018

    @tianon
    Member

    This is definitely not something I'm very keen on seeing explicitly implemented here in the php image itself -- we've already got a fair amount of "Docker-specific" behavior, and I really don't feel like it's a good idea for us to add even more (especially behavior which amounts to simply appending environment variable values directly to a .ini file verbatim, which seems pretty trivial to implement via a short script which could even be pretty easily embedded directly in the command: supplied to a PHP-based image, if needed).

  6. tianon commented on Dec 31, 2018

    @tianon
    Member

    Here's an example simple command/script which can be used to accomplish this pretty trivially (explicitly ignoring PHP_INI_DIR which is set in the base):

    $ docker run ... -e PHP_INI_MEMORY_LIMIT=2G php:7.3-apache bash -exc 'for env in "${!PHP_INI_@}"; do [ "$env" != 'PHP_INI_DIR' ] || continue; val="${!env}"; var="${env#PHP_INI_}"; var="${var,,}"; echo "$var = $val"; done > "$PHP_INI_DIR/docker-env.ini"; exec apache2-foreground'

    Note this won't work very well for directives which contain . (which isn't POSIX-compliant in environment variable names, so will sometimes not work appropriately).

    Alternatively (and more supportably long-term IMO), take advantage of php.ini explicitly supporting environment variables (https://secure.php.net/manual/en/configuration.file.php#example-36) with something simpler (and much more explicit) like this:

    FROM php:7.3-apache
    
    ENV PHP_INI_MEMORY_LIMIT 128M
    RUN echo 'memory_limit = ${PHP_INI_MEMORY_LIMIT}' > "$PHP_INI_DIR/docker-env.ini"
  7. Thijs-Riezebeek commented on Mar 6, 2019

    @Thijs-Riezebeek

    For future reference, man php has the following to say:

    -d foo[=bar]   Define INI entry foo with value bar
    

    So you could also use the -d option to increase INI settings, such as the memory limit:

    php -d memory_limit=128M
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    RequestRequest for image modification or feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions