Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set extra host in environment variables

I'm using docker compose to run my application. And for do that I need to set the hosts inside container (it's depends on the environment i'm running).

My approach was:

Create an environment file and set the variable:

#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 docker-compose config 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've tried to replace the variable reference using $SERVER_IP or ${SERVER_IP} but it didn't work.

like image 596
FedericoAlvarez Avatar asked Sep 09 '16 14:09

FedericoAlvarez


People also ask

How do I set environment variables in pod?

When you create a Pod, you can set dependent environment variables for the containers that run in the Pod. To set dependent environment variables, you can use $(VAR_NAME) in the value of env in the configuration file.

How do I set an environment variable in Docker?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.


2 Answers

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.

like image 166
Alex Pakka Avatar answered Oct 25 '22 10:10

Alex Pakka


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.

like image 26
William Greenly Avatar answered Oct 25 '22 10:10

William Greenly