'Pass Environment Variable to Apache PHP Docker Container

I have a PHP script that needs to reference a variable I input at runtime. I have built a docker image (I cannot build the variable into the image as it changes each time):

Here's my minimum working example:

index.php

<?php
$name = $_ENV["name"];
echo $name;
?>

Dockerfile

FROM PHP:apache

COPY index.php /var/www/html
RUN chown -R www-data:www-data /var/www

CMD ["apache2-foreground"]

I'm trying to spin it up with:

docker run -d -p 80:80 myimage:latest -e name=James

However, this fails as apache seems to have a flag called -e which reads the log level.

I'm sure I'm missing something fairly simple here!



Sources

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

Source: Stack Overflow

Solution Source