docker-compose


Can .env file make environment variables?


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
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#

Related Links

Why doesn't docker-compose 'down' take an optional [SERVICE…] argument?
why does docker-compose build trigger a rebuild when nothing changed
Disable autostart of docker-compose project
Docker-compose Error-Mean Stack Application won't get loaded with the data from mongodb container
docker-compose Windows volume
Trying to figure out why eslint-watch doesn't work with docker-compose correctly
docker-compose the latest stable version on ubuntu
docker compose environment variable for command
java.net.UnknownHostException: failed to resolve 'inventory-microservice'. Exceeded max queries per resolve 3
What does working_dir tag mean in a docker-compose yml file
If I upgrade an image in docker-compose, will its volumes be lost?
How to solve this docker-compose upgrading error?
Fabric Network - what happens when a downed peer connects back to the network?
Docker-compose and load balancing
Run arguments in Docker Compose
Docker compose file config

Categories

HOME
max
primefaces
azure-eventhub
microcontroller
blob
cryengine
assign
symfony-sonata
psexec
liquidsoap
country-codes
clip-path
software-collections
urllib2
websharper
captcha
simd
openam
beacon
webgl-earth
android-navigation-drawer
chartist.js
pixels
expressionengine
utc
mex
r-factor
apm
selenium3
multilanguage
android-scrollview
voyager
powerbi-embedded
iterm2
uitextview
inner-join
django-cors-headers
google-chrome-storage
react.rb
kendo-scheduler
reloaddata
bipartite
strtol
networkstream
altova
jira-agile
syslog4j
cherry-pick
diameter-protocol
factorization
sqldataadapter
dday
computability
ruby-1.8.7
recode
transbase
jsoncpp
joomla3.3
tinymce-plugins
gcc-warning
valdr-bean-validation
autopep8
asp.net-2.0
shunting-yard
agent-based-modeling
selenium-grid2
step
elixir-framework
imaplib
code-complete
violation
azure-pack
wpd
architectural-patterns
auctex
rbm
boost.build
cryptarithmetic-puzzle
ios6-maps
flatten
django-admin-tools
eeprom
stl-algorithm
youtube.net-api
zipstream
response-time
onunload
azure-appfabric
google-local-search
miniport
web-widget

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App