title: Dockerfile how to add localhost alias
body: <p>Dockerfile how to add localhost alias ?</p>
  
  <p>Hi i need to add <code>localhost</code> alias in the <code>Dockerfile</code>
  I do the following:</p>
  
  <p><code>RUN echo '127.0.0.1 locdev' &gt;&gt; /etc/hosts</code></p>
  
  <p>But when i then go to image bash console</p>
  
  <pre><code>$ docker exec -it my-image bash
  </code></pre>
  
  <p>and try to ping that host it does throw error</p>
  
  <pre><code>$ ping locdev
  ping: unknown host
  </code></pre>
  
  <p>What <code>/etc/hosts</code> contains ?</p>
  
  <pre><code>$ cat /etc/hosts
  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.17.0.4  62a5e2d10730
  </code></pre>
  
  <p>there is no other records</p>
  
  <p>Does anyone know how can i do this ? I need to do this in <code>Dockerfile</code> as external service run this docker and i do not run it from command line.</p>
  
  <p>In my case it is an <code>PHP</code> application, that is held in <code>Bitbucket</code> repository. There is a new feature called <code>PIPELINES</code>. It run application build on <code>Docker</code> image.</p>
  
  <p>In my case this application connects to <code>mysql</code> server where i can not change config (it will break production). On that docker image i ahve set up <code>mysql</code> server. So <code>localhost</code> works, but i need to add this alias for <code>localhost</code> to not break production server;</p>
  
  <p><code>Dockerfile</code> fragment:</p>
  
  <pre><code>RUN \
   aliases="127.0.0.1 localhost locdev" &amp;&amp;\
   sed "1s/.*/$aliases/" /etc/hosts
  RUN cat /etc/hosts
  </code></pre>
  
  <p>My <code>docker build</code> output example:</p>
  
  <pre><code>Step 10 : RUN aliases="127.0.0.1 localhost locdev" &amp;&amp; sed "1s/.*/$aliases/" /etc/hosts
   ---&gt; Running in 11ac105d632d
  127.0.0.1 localhost locdev
  ::1     localhost ip6-localhost ip6-loopback
  fe00::0 ip6-localnet
  ff00::0 ip6-mcastprefix
  ff02::1 ip6-allnodes
  ff02::2 ip6-allrouters
  172.17.0.2      383850eeb47b
   ---&gt; 0248977e48bd
  Removing intermediate container 11ac105d632d
  Step 11 : RUN cat /etc/hosts
   ---&gt; Running in 20fc2f40b5a9
  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.17.0.2      383850eeb47b
   ---&gt; c9e47ee296c5
  Removing intermediate container 20fc2f40b5a9
  Successfully built c9e47ee296c5
  </code></pre>