練習1

練習1

1.使用busybox:latest鏡像創建t1容器,並查看其主機名

[root@localhost ~]# docker container run --name t1 -it --rm busybox:latest                 
/ # hostname
965e25d7692f

2.t1容器的主機名就是'CONTAINER ID'

[root@localhost ~]# docker container ls
CONTAINER ID   IMAGE            COMMAND   CREATED              STATUS              PORTS     NAMES
965e25d7692f   busybox:latest   "sh"      About a minute ago   Up About a minute             t1

3.使用busybox:latest鏡像創建t1容器,並同時指明其主機名為t1

[root@localhost ~]# docker container run --name t1 -it --rm -h t1 busybox:latest  #加-h或--hostname選項可以設定主機名
/ # hostname
t1
/ # cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      t1  #如果容器的網路為bridge,會自動有名稱解析
/ # cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 192.168.128.2  #如果容器的網路為bridge,會自動指向宿主機使用的DNS SERVER

4. 使用busybox:latest鏡像創建t1容器,並同時指明DNS SERVER以及搜索域

[root@localhost ~]# docker container run --name t1 -it --rm --dns 1.1.1.1 --dns-search ilinux.io busybox:latest  #加--dns選項可以設定DNS SERVER;加--dns-search可以設定搜索域
/ # cat /etc/resolv.conf 
search ilinux.io
nameserver 1.1.1.1
[root@localhost ~]# docker container run --name t1 -it --rm --dns 1.1.1.1 --dns 8.8.8.8 busybox:latest  #設定2個DNS SERVER
/ # cat /etc/resolv.conf 
search localdomain
nameserver 1.1.1.1
nameserver 8.8.8.8

5. 使用busybox:latest鏡像創建t1容器,並同時設定'www.ilinux.io'主機名稱解析的ip地址為192.168.0.1

[root@localhost ~]# docker container run --name t1 -it --rm --add-host www.ilinux.io:192.168.0.1 busybox:latest  #加上--add-host HOSTNAME:IP
/ # cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.0.1     www.ilinux.io
172.17.0.2      1ceac1507fc3