<p>I've tried your case, and it works fine on my side:</p>
<pre><code>version: '2'
services:
  test:
    image: ubuntu:16.04
    command: sleep 9999
    extra_hosts:
      - &quot;${HOSTNAME}:1.1.1.1&quot;
      - &quot;test.com:1.1.1.1&quot;
</code></pre>
<p>and here is what I get in the container</p>
<pre><code>cat /etc/hosts
...
1.1.1.1 a1.test.com
1.1.1.1 test.com
</code></pre>
<p><em>(edit: indentation)</em></p>

CC BY-SA 3.0
2017-10-23T06:49:54.273Z
It was my mistake, I had typo in .env file
Wissam Roujoulah
CC BY-SA 4.0
2023-09-21T15:36:13.240Z
I'm getting this eror: 
ERROR: for cron  Cannot create container for service cron: bad format for add-host: ":1.1.1.1"

with: "${HOSTNAME}:1.1.1.1"
Adán Escobar
CC BY-SA 4.0
2017-10-23T06:33:50.193Z
2023-04-15T14:48:12.050Z
2023-04-15T14:48:12.050Z
oryades
<p>I have a docker compose file with <code>.env</code> file, for example the <code>.env</code> file has this <code>HOSTNAME=a1.test.com</code>, and a <code>docker-compose</code> file has this:</p>

<pre><code>...
extra_hosts:
  - "${HOSTNAME}:1.1.1.1"
  - "test.com:1.1.1.1"
</code></pre>

<p>When I run the container the <code>/etc/hosts</code> file content is: </p>

<pre><code>1.1.1.1          
1.1.1.1    test.com
</code></pre>

<p>So the docker compose not putting the variable data during the run process.</p>

<p>I tried this <code>hostname: "${HOSTNAME}"</code> and it is working fine the host name of the container is correct <code>a1.test.com</code>.</p>

<p>Any idea how can achieve what I want?</p>

<docker><docker-compose>
Docker compose host variable in extra_hosts not working
Wissam Roujoulah
# Docker compose host variable in extra_hosts not working

(Asked by: username_0 on 2017-10-23)

I have a docker compose file with .env file, for example the .env file has this HOSTNAME=a1.test.com, and a docker-compose file has this:
...
extra_hosts:
  - "${HOSTNAME}:1.1.1.1"
  - "test.com:1.1.1.1"

When I run the container the /etc/hosts file content is: 
1.1.1.1          
1.1.1.1    test.com

So the docker compose not putting the variable data during the run process.
I tried this hostname: "${HOSTNAME}" and it is working fine the host name of the container is correct a1.test.com.
Any idea how can achieve what I want?

**Tags:** docker, docker-compose


---

## 1 answers

### Answer by username_1 on 2017-10-23. Score: 4

I've tried your case, and it works fine on my side:
version: '2'
services:
  test:
    image: ubuntu:16.04
    command: sleep 9999
    extra_hosts:
      - "${HOSTNAME}:1.1.1.1"
      - "test.com:1.1.1.1"

and here is what I get in the container
cat /etc/hosts
...
1.1.1.1 a1.test.com
1.1.1.1 test.com

(edit: indentation)

> **Comments:**
> **username_0**: It was my mistake, I had typo in .env file  
> **username_2**: I'm getting this eror:  ERROR: for cron  Cannot create container for service cron: bad format for add-host: ":1.1.1.1"  with: "${HOSTNAME}:1.1.1.1"  

---

