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

Docker Swarm - dependencies definition
docker-compose volume not mounted
Exposing sentry service in docker-compose file
Yesod and docker-compose logs in development mode
Docker compose to always deploy using latest image
JHipster - Doubts in the development using Microservices Architecture
Gogs + Drone getsockopt: connection refused
Concourse result keeps loading
Docker-compose: AttributeError: 'module' object has no attribute 'ssl'
scaling a service with docker compose
Docker Compose apparently ignores COMPOSE_FILE
Where does Docker install gems on a mac
What is the difference between docker-machine and docker-compose?
Docker Compose port issue. Cannot launch docker project on localhost
Name an image built from running a docker-compose file
Race conditions after changing names in docker-compose.yml

Categories

HOME
ssms
msbuild
ns2
gsm
grpc
liquibase
x86-64
squeak
terminal
revit-api
annyang
subprocess
spring-data-solr
kairosdb
velocity
resharper
ng2-charts
drush
eclipse-orion
luis
multibinding
internet-explorer-7
single-page-application
gnu-screen
matlab-gui
quality-center
akavache
hierarchical-data
bytebuffer
meteor-accounts
pypy
ds-5
edirectory
deferred
blockly
urhosharp
log4net-configuration
rtos
webclient
autodesk-data-management
intervention
smooth-streaming
logan-square
django-cors-headers
import.io
monaca
activesupport
fckeditor
nservicebus6
tsc
mavlink
git-push
qtruby
wizard
apache2-module
bluetooth-lowenergy-4.2
yfiles
github-for-mac
commercetools
gulp-cdnizer
textblob
joomla3.3
eaaccessory
sparkcore
clearquest
ultraedit
pymol
applinks
jsonmodel
frisby.js
fscanf
banana-pi
sendy
azure-caching
flask-mongoengine
randomaccessfile
violation
wpd
dynamic-expresso
backbone-views
microsoft-speech-platform
rfc822
teamsystem
copyfile
parallel-python
xpolog
soapexception
glassfish-embedded
pkcs#7
content-delivery-network
memory-consumption

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