Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile how to add localhost alias

Tags:

docker

Dockerfile how to add localhost alias ?

Hi i need to add localhost alias in the Dockerfile I do the following:

RUN echo '127.0.0.1 locdev' >> /etc/hosts

But when i then go to image bash console

$ docker exec -it my-image bash

and try to ping that host it does throw error

$ ping locdev
ping: unknown host

What /etc/hosts contains ?

$ 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

there is no other records

Does anyone know how can i do this ? I need to do this in Dockerfile as external service run this docker and i do not run it from command line.

In my case it is an PHP application, that is held in Bitbucket repository. There is a new feature called PIPELINES. It run application build on Docker image.

In my case this application connects to mysql server where i can not change config (it will break production). On that docker image i ahve set up mysql server. So localhost works, but i need to add this alias for localhost to not break production server;

Dockerfile fragment:

RUN \
 aliases="127.0.0.1 localhost locdev" &&\
 sed "1s/.*/$aliases/" /etc/hosts
RUN cat /etc/hosts

My docker build output example:

Step 10 : RUN aliases="127.0.0.1 localhost locdev" && sed "1s/.*/$aliases/" /etc/hosts
 ---> 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
 ---> 0248977e48bd
Removing intermediate container 11ac105d632d
Step 11 : RUN cat /etc/hosts
 ---> 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
 ---> c9e47ee296c5
Removing intermediate container 20fc2f40b5a9
Successfully built c9e47ee296c5
like image 527
vardius Avatar asked Oct 12 '16 16:10

vardius


1 Answers

Because Docker manages the /etc/hosts file for you, making changes to /etc/hosts isn't going to work.

You can, however, add additional hosts to Docker's DNS service discovery with the --add-host option when running docker create or docker run.

This is from the output of docker run --help:

  --add-host value              Add a custom host-to-IP mapping (host:ip) (default [])

Keep in mind, however, that 127.0.0.1 from inside a container will not take you to the container host, but will land you on the container since it has its own 127.0.0.1 interface.

The eth0 IP address of your docker host is routable from inside your container. You could use that in your --add-host argument. For example, if my Docker host is 192.168.1.43 on my local network, I could do docker run --add-host locdev:192.168.1.43 .... At that point, locdev will resolve back to that ip from inside the container.

like image 168
programmerq Avatar answered Oct 11 '22 03:10

programmerq