如何在Dockerfile中处理特定的主机名,如-h选项 [英] How to handle specific hostname like -h option in Dockerfile

查看:1844
本文介绍了如何在Dockerfile中处理特定的主机名,如-h选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CentOS 7作为docker守护进程主机和centos:6 Docker映像。



我想安装一些需要可解析主机名的软件,作为非环回地址。如果我使用-h选项运行码头图像,如

  docker run -i -t -h myhost centos:6 / bin / bash 

然后我可以安装软件,因为容器中的/ etc / hosts自动配置为

  [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)个字节的数据。
从myhost(172.17.0.7)64字节:icmp_seq = 1 ttl = 64时间= 0.033毫秒

但是,如果从Dockerfile创建一个图像,我不能使用相同的方法。我测试了使用以下Dockerfile创建一个图像

  FROM centos:6 

ENV HOSTNAME myhost
运行ping myhost

在docker构建过程中,分配的主机名无法解析为动态ip addr,如下所示:

  $ docker build -t testimage。 
将构建上下文发送到Docker守护程序2.048 kB
将构建上下文发送到Docker守护程序
步骤0:FROM centos:6
---> a30bc9f3097e
步骤1:ENV HOSTNAME myhost
--->使用缓存
---> e73bf592389e
步骤2:运行ping myhost
--->运行在ca54c8eac073
ping:未知主机myhost
INFO [0000]命令[/ bin / sh -c ping myhost]返回一个非零代码:2

如何将一些特定的主机名解析为动态容器IP addr?

解决方案

在Docker文件中通常不可能。



根据软件,您可能会做一些解决方法。例如,您可以尝试像

 运行echo $(grep $(hostname)/ etc / hosts | cut -f1) my.host.name>> / etc / hosts&&&安装软件

通过将主机名设置为与安装软件相同的RUN命令,发生在容器的同一层内。 Docker稍后会覆盖主机名称,您必须在运行时重新设置主机名,但是您的软件可能会正常。



如果您必须做很多这样,您可以尝试 Packer 构建容器。它可以构建Docker容器,但不使用多个层。这使得重建速度更慢,下载内置映像的速度更快,并使其在将其冻结到容器中之前对图像执行多个操作变得更加方便。


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?

解决方案

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.

这篇关于如何在Dockerfile中处理特定的主机名,如-h选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆