I have a script that grabs variables and export it as environment variable. And I also have PHP-based docker for my web application. I tried to execute the script in the docker-php-entrypoint
but the environment variable is not loaded.
The Dockerfile is like below.
FROM php:7.2-apache
COPY docker-php-entrypoint /usr/local/bin/
COPY load_parameter_store.sh /opt/
COPY src/ /var/www/html/
This is the docker-php-entrypoint
content. It runs a script load_parameter_store.sh
.
#!/bin/sh
set -e
/opt/load_parameter_store.sh
# first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then
set -- php "$@"
fi
exec "$@"
And this is the script itself
EXPORT_STATEMENT='export MY_VARIABLE=FooBar' #Will be loaded from AWS SSM Parameter Store
eval $EXPORT_STATEMENT
I have a PHP web script that shows the environment variable.
<?php
$var = $_ENV['MY_VARIABLE'];
echo "Hello World {$var}";
The end goal is that I would like the PHP shows Hello World FooBar
but what I get is just Hello World
. It means that the variable is not loaded.
What should I do to fix this?
My Answer:
#!/bin/sh
# Run the script to load environment variables
/opt/load_parameter_store.sh
# Start Apache
apache2-foreground
__And this is the content of load_parameter_store.sh script that loads the variables and export it as environment variables._#!/bin/sh
# Load variables from somewhere
VAR1="value1"
VAR2="value2"
# Export variables
export VAR1
export VAR2
__To make sure that the environment variables are loaded correctly, you can modify the docker-php-entrypoint to print out the environment variables before starting Apache._#!/bin/sh
# Run the script to load environment variables
/opt/load_parameter_store.sh
# Print out the environment variables
echo "VAR1: $VAR1"
echo "VAR2: $VAR2"
# Start Apache
apache2-foreground
__After building and running the docker container, you should see the environment variables printed out in the console before Apache starts. This confirms that the variables are loaded correctly and exported as environment variables.
Rate this post
3 of 5 based on 3083 votesComments