Answers: <p>You could try to use docker compose and the docker compose "extra_hosts" tag, as described on the docker compose specification at <a href="https://docs.docker.com/compose/compose-file/" rel="nofollow noreferrer">https://docs.docker.com/compose/compose-file/</a>.</p>
  
  <blockquote>
    <p>extra_hosts</p>
    
    <p>Add hostname mappings. Use the same values as the docker client --add-host parameter.</p>
    
    <p>extra_hosts:</p>
    
    <ul>
    <li><p>"somehost:162.242.195.82"</p></li>
    <li><p>"otherhost:50.31.209.229"</p></li>
    </ul>
    
    <p>An entry with the ip address and hostname is created in /etc/hosts 
    for this service, e.g:</p>
    
    <p>162.242.195.82  somehost</p>
    
    <p>50.31.209.229   otherhostinside containers</p>
  </blockquote>
  
  <p>So, this will mean, that you need to use docker compose in addition to docker.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-06-14T08:09:20.477Z
Answers: 2019-06-14T08:09:20.477Z
Answers: triplem
Body: <p>I am using docker to scale my service.
  I know there's a command called <code>--add--host</code> for adding host into /etc/host</p>
  
  <p>However, I have a bunch of hosts need to be added. It's just too long to launch a docker in this way</p>
  
  <pre><code>docker run -it \
    --add-host=testing1.example.com:10.0.0.1 \
    --add-host=testing2.example.com:10.0.0.2 \
    --add-host=testing3.example.com:10.0.0.3 \
    ...
  </code></pre>
  
  <p>Therefore, I was thinking to create a function to read a mapping file and solve the problem.</p>
  
  <p>The file format is like the following</p>
  
  <p><strong>host-list</strong></p>
  
  <pre><code>14.64.65.3 A
  11.64.65.18 B
  12.64.65.23 C
  13.10.200.63 D
  13.10.5.77 E
  14.10.5.72 F
  15.10.5.81 G
  ...
  </code></pre>
  
  <p>I am new on shell script and have no idea to start with.</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-08-09T13:59:42.467Z
Comments: 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
Comments: Scott Stensland
Tags: <docker><shell-script><bash>
Title: Add mapping ip and host into /etc/hosts with docker
UserDisplayName: rj487
ThreadText: # Add mapping ip and host into /etc/hosts with docker
  
  (Asked by: username_0 on 2019-05-31)
  
  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:10.0.0.1 \
    --add-host=testing2.example.com:10.0.0.2 \
    --add-host=testing3.example.com:10.0.0.3 \
    ...
  
  ```
  
  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**
  
  ```
  14.64.65.3 A
  11.64.65.18 B
  12.64.65.23 C
  13.10.200.63 D
  13.10.5.77 E
  14.10.5.72 F
  15.10.5.81 G
  ...
  
  ```
  
  I am new on shell script and have no idea to start with.
  
  **Tags:** docker, shell-script, bash
  
  > **Comments:**
  > **username_1**: 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  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-06-14. Score: 2
  
  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/>.
  
  > extra\_hosts
  >
  > Add hostname mappings. Use the same values as the docker client --add-host parameter.
  >
  > extra\_hosts:
  >
  > * "somehost:162.242.195.82"
  > * "otherhost:50.31.209.229"
  >
  > An entry with the ip address and hostname is created in /etc/hosts
  > for this service, e.g:
  >
  > 162.242.195.82 somehost
  >
  > 50.31.209.229 otherhostinside containers
  
  So, this will mean, that you need to use docker compose in addition to docker.
  
  
  ---
  
  