Set extra host in environment variables

白昼怎懂夜的黑 提交于 2019-12-05 15:24:52

I created a file .env, added single line HOST=test.example.com, then did this in docker-compose:

extra_hosts:
- myip:${HOST}

docker-compose config then shows

  extra_hosts:
      myip: test.example.com

To do this I followed the documentation from Docker-compose environment variables the section about .env file

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 if you place your variables in .env file, you can use them for substitution in docker-compose.yml, but if you use env_file option for the particular container, you can only see the variables inside the Docker container, not during the build. It is also logical, env_file replaces docker run --env-file=FILE ... and nothing else.

So, you can only place your values into .env. Alternatively, as William described, you can use host's environment variables.

EDIT

Try the following:

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:
      - "myip:${SERVER_IP}"

Ensure curly bracers and that the environment variable exists on the host os.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!