BigJobbies
BigJobbies

Reputation: 4055

Passing docker-compose .env file values into the container

I'm having real issues trying to pass variables into my docker container to dynamically update my xdebug.ini file;

I have the following simple docker-compose.yml

services:
  php:
    container_name: php
    build:
      context: .
    command: ["/usr/local/sbin/php-fpm", "-F", "-R"]
    expose:
      - "9003"
    env_file:
      - .env

My Dockerfile is basic

FROM propietary-image-here-custom-8.3-fpm

RUN apt-get update

RUN pecl install xdebug && docker-php-ext-enable xdebug.so
COPY ./xdebug.ini /usr/local/etc/php/conf.d/

CMD ["/usr/local/sbin/php-fpm", "-F", "-R"]

My .env is just one line;

XDEBUG_MODE=debug

and my xdebug.ini is as follows;

xdebug.mode=${XDEBUG_MODE}

I can boot it all up with docker compose up -d fine and if I run docker compose config i can see the variable.

But when I enter into the container with docker run -i -t php /bin/bash and execute env, its not there, and its not working with the xdebug.ini.

Upvotes: 1

Views: 71

Answers (1)

hakre
hakre

Reputation: 198204

its not working with the xdebug.ini.

Your xdebug.mode=${XDEBUG_MODE} in xdebug.ini is probably redundant: the XDEBUG_MODE parameter in the environment overrides the xdebug.mode ini setting.

Therefore, you can replace ${XDEBUG_MODE} in xdebug.ini with the mode you'd like to have -or- remove the setting completely if the default (= develop) already suits you.

Compare: string xdebug.mode = develop (xdebug.org), and find there as well this warning:

WARNING: Some web servers have a configuration option to prevent environment variables from being propagated to PHP and Xdebug.

For example, PHP-FPM has a clear_env configuration setting that is on by default, which you will need to turn off if you want to use XDEBUG_MODE.

Make sure that your web server does not clean the environment, or specifically allows the XDEBUG_MODE environment variable to be passed on.

For interpreting PHP-FPM phpinfo() output for the Xdebug3 extension, see as well Docker PHP with Xdebug 3 env XDEBUG_MODE doesn't work (Q&A).


The way you test the container environment is wrong. Instead of run, use exec:

$ cat .env
FOO=BAR
$ docker exec -i -t php php -r 'print_r(getenv());'
Array
(
    [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    [HOSTNAME] => f30ed99b9fed
    [TERM] => xterm
    [FOO] => BAR
    [PHPIZE_DEPS] => autoconf       dpkg-dev        file        g++         gcc         libc-dev        make        pkg-config      re2c
    [PHP_INI_DIR] => /usr/local/etc/php
    [PHP_CFLAGS] => -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
    [PHP_CPPFLAGS] => -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
    [PHP_LDFLAGS] => -Wl,-O1 -pie
    [GPG_KEYS] => AFD8691FDAEDF03BDF6E460563F15A9B715376CA 9D7F99A0CB8F05C8A6958D6256A97AF7600A39A6 0616E93D95AF471243E26761770426E17EBBB3DD
    [PHP_VERSION] => 8.4.3
    [PHP_URL] => https://www.php.net/distributions/php-8.4.3.tar.xz
    [PHP_ASC_URL] => https://www.php.net/distributions/php-8.4.3.tar.xz.asc
    [PHP_SHA256] => 5c42173cbde7d0add8249c2e8a0c19ae271f41d8c47d67d72bdf91a88dcc7e4b
    [HOME] => /root
)

This is what you configured docker compose to do. See Use the env_file attribute (docker.com):

Using an .env file lets you use the same file for use by a plain docker run --env-file ... command [bold by me]

If you insist to use docker run you have to add the --env-file switch on the command-line as you are running a new container independent to the configured composition:

$ docker run --env-file .env -i -t php php -r 'print_r(getenv());'
Array
(
    [HOSTNAME] => 7f87b21e6aac
    [PHP_INI_DIR] => /usr/local/etc/php
    ...
    [FOO] => BAR
    [PHPIZE_DEPS] => ...
)

Upvotes: 2

Related Questions