Monday, July 20, 2015

How to handle specific hostname like -h option in Dockerfile

http://stackoverflow.com/questions/28898787/how-to-handle-specific-hostname-like-h-option-in-dockerfile


I want to install some software which requires resolvable hostname as non-loopback address. If I run a docker image with -h option like
docker run -i -t -h myhost centos:6 /bin/bash
Then I can install the software because /etc/hosts in the container automatically configured like
[root@myhost /]# cat /etc/hosts
172.17.0.7  myhost
127.0.0.1   localhost    

[root@myhost /]# ping myhost
PING myhost (172.17.0.7) 56(84) bytes of data.
64 bytes from myhost (172.17.0.7): icmp_seq=1 ttl=64 time=0.033 ms
But I cannot use same way if I create an image from Dockerfile. I tested creating an image using following Dockerfile
FROM centos:6

ENV HOSTNAME myhost
RUN ping myhost
In docker build process, assigned hostname cannot be resolved as dynamic ip addr like following:
$ docker build -t testimage .
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon
Step 0 : FROM centos:6
 ---> a30bc9f3097e
Step 1 : ENV HOSTNAME myhost
 ---> Using cache
 ---> e73bf592389e
Step 2 : RUN ping myhost
 ---> Running in ca54c8eac073
ping: unknown host myhost
INFO[0000] The command [/bin/sh -c ping myhost] returned a non-zero code: 2
How can I use some specific hostname resolved as dynamic container IP addr?

========================
If you have to do a lot of this, you might try Packer for building containers. It can build Docker containers, but doesn't use multiple layers. This makes it slower to rebuild, faster to download the built images, and makes it more convenient to do multiple operations on an image before freezing it into a container.

No comments: