Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle specific hostname like -h option in Dockerfile

I'm using CentOS 7 as docker daemon host and centos:6 Docker images.

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?

like image 557
dempasong Avatar asked Mar 06 '15 12:03

dempasong


People also ask

How do I specify a container name in Dockerfile?

The Dockerfile is for creating images not containers. You can now give names to your containers using the new --name flag for docker run . If --name is not provided Docker will automatically generate an alphanumeric string for the container name.

What is the hostname in docker?

This flag, --hostname is used to change the host name of you container, its used when you run your container. This does not change the container's DNS outside docker and it also does not provide any network isolation, so you cannot connect to the container using the hostname.

What is the use of hostname in docker compose?

Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name. Your app's network is given a name based on the “project name”, which is based on the name of the directory it lives in.


3 Answers

This isn't generally possible in a Dockerfile.

Depending on the software, you might be able to do some kind of work-around. For example, you could try something like

RUN echo $(grep $(hostname) /etc/hosts | cut -f1) my.host.name >> /etc/hosts && install-software

By setting the hostname within the same RUN command as you install the software, it'll happen inside the same layer of the container. Docker will later overwrite the hostname and you'll have to set it anew when running, but your software might be OK with that.

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.

like image 56
Nathaniel Waisbrot Avatar answered Oct 22 '22 16:10

Nathaniel Waisbrot


As for workaround, you can use docker-compose or docker stack to build your container with specific hostname, e.g.

version: '3'
services:
  all:
    image: testimage
    container_name: myname
    hostname: myhost
    build:
      context: .

Then run as:

docker-compose --build up

or using Docker stacks:

docker stack deploy -c docker-compose.yml mystack
like image 12
kenorb Avatar answered Oct 22 '22 17:10

kenorb


In a case when /etc/hosts does not help, I replace the hostname binary.

RUN mv /usr/bin/hostname{,.bkp}; \
  echo "echo myhost.local" > /usr/bin/hostname; \
  chmod +x /usr/bin/hostname

...

RUN mv /usr/bin/hostname{.bkp,}
like image 8
Christophe Morio Avatar answered Oct 22 '22 16:10

Christophe Morio