Ross
Ross

Reputation: 2417

Adding extra host to dockerfile

I am trying to build and run a docker container of one of my services from the Dockerfile. I need to add some extra host to the container. I know that in the docker compose file you can add:

extra_hosts:
  - "DEV01:172.16.0.10"
  - "DEV02:172.16.0.11"
  - "DEV03:172.16.0.12"

However I need to build the image directly from the docker file. How do I add these extra hosts?

I have tried add --add-host=[] to the docker run which some people have suggested but that doesn't seem to work.

Upvotes: 3

Views: 4714

Answers (1)

Tomasz Swider
Tomasz Swider

Reputation: 2382

Docker run with --add-host works fine:

->docker run --rm \
 --add-host="DEV01:172.16.0.10" \
 --add-host="DEV01:172.16.0.11" \
 --add-host="DEV01:172.16.0.12" \
 alpine cat /etc/hosts

Gives output:

 127.0.0.1  localhost
 ::1    localhost ip6-localhost ip6-loopback
 fe00::0    ip6-localnet
 ff00::0    ip6-mcastprefix
 ff02::1    ip6-allnodes
 ff02::2    ip6-allrouters
 172.16.0.10    DEV01
 172.16.0.11    DEV01
 172.16.0.12    DEV01
 172.17.0.3 e85457b446f1

Upvotes: 1

Related Questions