I am using docker to scale my service. I know there's a command called --add--host for adding host into /etc/host

However, I have a bunch of hosts need to be added. It's just too long to launch a docker in this way

docker run -it \
  --add-host=testing1.example.com: \
  --add-host=testing2.example.com: \
  --add-host=testing3.example.com: \

Therefore, I was thinking to create a function to read a mapping file and solve the problem.

The file format is like the following

host-list A B C D E F G

I am new on shell script and have no idea to start with.

  • get your shell to read one line at a time from your host-list file then have shell echo the needed syntax you show ... this is a couple lines of bash Commented Aug 9, 2019 at 13:59

1 Answer 1


You could try to use docker compose and the docker compose "extra_hosts" tag, as described on the docker compose specification at https://docs.docker.com/compose/compose-file/.


Add hostname mappings. Use the same values as the docker client --add-host parameter.


  • "somehost:"

  • "otherhost:"

An entry with the ip address and hostname is created in /etc/hosts for this service, e.g: somehost otherhostinside containers

So, this will mean, that you need to use docker compose in addition to docker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.