Reputation: 69
I tried to use a .env file to make environment variable, but it doesn't work.
These are my steps:
version "3"
services:
web:
image: php-fpm:5.6.30
env_file:
- .env
This is .env file
TEST_ENV="HELLO WORLD"
It doesn't work when I start the container:
var_dump(getenv("TEST_ENV")); // output NULL
Upvotes: 0
Views: 626
Reputation: 30723
For me it seems to work. Maybe this can help you:
├── docker-compose.yaml
├── .env
└── myphp
├── Dockerfile
└── script.php
My .env file
TEST_ENV="HELLO WORLD"
My docker-compose.yaml
:
version: "3"
services:
web:
build: ./myphp
env_file: .env
So my docker-compose.yaml
will build the image myphp. The dockerfile looks like this:
FROM php:5.6.30-fpm
COPY script.php /var/script.php
My script.php
<?php
var_dump(getenv('TEST_ENV'));
exit;
Than I perform docker-compose up -d --build
. This will build my image and add the php script in it and it will run a container instance of that image.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
15f0289bfbe8 test_web "docker-php-entryp..." 3 seconds ago Up 1 second 9000/tcp test_web_1
I'm accessing my container
$ docker exec -it 15f0289bfbe8 bash
And I'm going the the /var
folder where I've put my script (check dockerfile) and I'm exexcuting it + also just printing env var:
root@15f0289bfbe8:/var/www/html# cd /var/
root@15f0289bfbe8:/var# ls
backups cache lib local lock log mail opt run script.php spool tmp www
root@15f0289bfbe8:/var# php -f script.php
string(13) ""HELLO WORLD""
root@15f0289bfbe8:/var# echo $TEST_ENV
"HELLO WORLD"
root@15f0289bfbe8:/var#
Upvotes: 1