The env_file
optin will only set environment variables in the docker container itself. Not on the host which is used during the compose ‘build’.
To define your port as env var you should use the .env
file as described here
In your case create a .env
file which contains::
PORT=5000
and the docker-compose.yml
:
version: '3'
services:
flask:
build:
context: ./flask
dockerfile: Dockerfile_flask
ports:
#- "5000:5000"
- "${PORT}:${PORT}" # I want to set port defined in the env file
volumes:
- ./logs:/app/flask/log
restart: always
If you want to add environment variable to your container using a env_file you can add it again.
To make it fully clear this example:
A postgres started in compose. The environment variables in the my-env-file
are known inside the container, the env var inside .env
is used during the docker-compose up
process.
a .env
file with:
PORT=5432
a my-env-file
with:
POSTGRES_USER=dev
POSTGRES_PASSWORD=secret
POSTGRES_DB=db
and the docker-compose.yml
:
version: ‘3.3’
services:
postgres:
image: postgres:9.6
container_name: postgres
env_file:
- my-env-file
ports:
- ${PORT}:${PORT}