I am using docker compose to run my application. For this, I need to set up the host inside the container (it depends on the environment I am running in).
Alibaba Cloud Simple Application Server: Anti COVID-19 SME Enablement Program
$300 coupon package for all new SMEs and a $500 coupon for paying customers.
My method is:
Create an environment file and set variables:
#application.env
SERVER_IP=10.10.9.134
My docker compose file looks like:
version: '2'
services:
api:
container_name: myApplication
env_file:
-application.env
build: ./myApplication/
entrypoint: ./docker/api-startup.sh
ports:
-"8080:8080"
depends_on:
-redis
extra_hosts: &extra_hosts
myip: $SERVER_IP But my problem is that the variable SERVER_IP is never replaced.
When I run the docker-compose configuration, I see:
services:
api:
build:
context: /...../myApplication
container_name: myApplication
depends_on:
-redis
entrypoint: ./docker/api-startup.sh
environment:
SERVER_IP: 10.10.9.134
extra_hosts:
myip:''
ports:
-8080:8080 I tried to use $SERVER_IP or $(SERVER_IP) to replace variable references, but it didn't work.
I created a file .env, added a single line HOST = test.example.com, and then did this in docker-compose:
extra_hosts:
-myip:${HOST} docker-compose config then shows
extra_hosts:
myip: test.example.com
For this, I followed the documentation on .env files in Docker-compose environment variables
UPDATE
According to the Docker documentation,
Note: If your service specifies a build option, variables defined in environment files will not be automatically visible during the build.
Use the args sub-option of build to define build-time environment variables.
It basically means that if you put variables in the .env file, you can use them in docker-compose.yml to replace them, but if you use the env_file option for a specific container, you can only see the variables inside the Docker container, not In the build. It is also logical, env_file replaces docker run --env-file = FILE...and nothing else.
Therefore, you can only put your values into .env. Or, as William described, you can use the host's environment variables.