docker如何构建Container的host信息
1 hostname和hosts文件的作用
hostname:在网络上标示一台机器的名称
hosts: 将主机名映射到IP上
2 构建hostname和hosts文件
在建立Container时,docker需要为Container准备hosts和hostname文件,在inux系统中,hostname和hosts文件一般都放在系统的etc目录下。那么这个两个文件是如何建立起来的呢?下面详细分析一下他们的由来。
2.1 构建hostname文件
docker使用buildHostnameFile
方法构建Container的hostname文件。构建Container的hostname文件共分两个步骤:
1. 获取hostname
文件路径
2. 将hostname
文件路径记录在Container对象的HostnamePath
中
3. 将Container对象中记录的Hostname和Domainname写入hostname
文件中
实际的操作简而言之是将Container.Config.hostname和container.Config.Domainname写入hostname文件中。
2.1.1获取hostname
文件路径
docker使用Container方法getRootResourcePath("hostname")
来获取hos