天天看點

建立docker容器時設定hosts内容

方式1:

在使用docker run運作一個新的容器的時候,通過參數--add-host來添加域名和IP資訊到容器的/etc/hosts檔案中。例如:

docker run --add-host=hostname:111.111.111.1 --name containerName -it Centos7 

上面中我添加了一個域名hostname,以及對應的IP位址111.111.111.1

方式2:

在docker-compose.yml檔案中,通過配置參數extra_hosts來實作。例如:

extra_hosts:

 - "host1:111.111.111.111"

 - "host2:111.111.111.112"

你就可以在你容器中/etc/hosts中看到下面的内容:

111.111.111.111  host1

111.111.111.112   host2