Docker Uses Environment Variables to Set Up Extra Hosts

Source: Internet
Author: User
Keywords extra hosts docker hosts of extra docker environment variables

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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.