John Davidson

How to Export Environment Variable From Script in PHP Docker Entrypoint


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 /opt/
COPY src/ /var/www/html/

This is the docker-php-entrypoint content. It runs a script

set -e


# first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then
set -- php "$@"

exec "$@"

And this is the script itself

EXPORT_STATEMENT='export MY_VARIABLE=FooBar' #Will be loaded from AWS SSM Parameter Store


I have a PHP web script that shows the environment variable.


$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:

# Run the script to load environment variables

# Start Apache

__And this is the content of script that loads the variables and export it as environment variables._#!/bin/sh

# Load variables from somewhere

# 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

# Print out the environment variables
echo "VAR1: $VAR1"
echo "VAR2: $VAR2"

# Start Apache

__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

4 of 5 based on 8971 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us