debugdockerMinor
Docker compose host variable in extra_hosts not working
Viewed 0 times
dockerworkinghostcomposeextra_hostsvariablenot
Problem
I have a docker compose file with
When I run the container the
So the docker compose not putting the variable data during the run process.
I tried this
Any idea how can achieve what I want?
.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.comSo 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?
Solution
I've tried your case, and it works fine on my side:
and here is what I get in the container
(edit: indentation)
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)
Code Snippets
version: '2'
services:
test:
image: ubuntu:16.04
command: sleep 9999
extra_hosts:
- "${HOSTNAME}:1.1.1.1"
- "test.com:1.1.1.1"cat /etc/hosts
...
1.1.1.1 a1.test.com
1.1.1.1 test.comContext
StackExchange DevOps Q#2392, answer score: 4
Revisions (0)
No revisions yet.