1. 程式人生 > 其它 >|NO.Z.00001|——————————|NavigationLog|——|Docker&製作映象&批量啟動容器|

|NO.Z.00001|——————————|NavigationLog|——|Docker&製作映象&批量啟動容器|



[NavigationLog:docker]                                                                                                  [NavigationIng.了不起]








一、docker的安裝配置
### --- dockercentos7.x下安裝教程:
### --- yum方式安裝docker並配置橋接網路

[root@localhost ~]#  yum install -y docker                				// yum方式安裝docker
[root@localhost ~]#  systemctl start docker                   
[root@localhost ~]#  ipaddr                               				// 檢視是否有docker0網絡卡
[root@localhost ~]#  systemctl stop                       				// 停止docker
[root@localhost ~]#  ip link set dev docker0 down         				// down掉docker0網絡卡         
[root@localhost ~]#  brctl delbr docker0                  				// 刪除docker0網絡卡
### --- 配置橋接網路:

[root@localhost ~]#  brctl addbr br0		               				// 新建橋接網絡卡br0
[root@localhost ~]#  ip link set dev br0 up                				// 設定為啟動狀態
[root@localhost ~]#  ip addr add 192.168.1.61/24 dev br0   				// 為br0設定IP,這一步會中斷網路。
### --- 為br0分配物理網路中的IP地址

[root@localhost ~]#   ip addr  del 192.168.1.61/24 dev ens33    		// 將宿主機網絡卡上的IP清空
[root@localhost ~]#   brctl addif br0 ens33                     		// 將宿主機IP掛到br0上
[root@localhost ~]#   ip route del default                      		// 刪除原路由
[root@localhost ~]#   ip route add default via 192.168.1.1 dev br0    	// 為br0設定路由。
[root@localhost ~]#   route -n                       					// 檢視閘道器
### --- 設定docker服務啟動引數:

[root@localhost ~]#  vim /etc/sysconfig/docker-network 					 // 其他系統可能在:/etc/sysconfig/docker下。
DOCKER_NETWORK_OPTIONS="-b=br0"                         				 // 設定為此內容即可
[root@localhost ~]#  systemctl restart docker           				 // 啟動docker服務
### --- 安裝pipework

[root@localhost ~]#     git clone https://github.com/jpetazzo/pipework  // 下載pipework服務
[root@localhost ~]#     cp ~/pipework/pipework /usr/local/bin/          // 複製到/usr/local/sbin        
### --- 啟動容器

[root@localhost ~]# docker run -itd --net=none  --name=centos7-ssh \
 docker.io/yumin9822/centos7-sshd:latest /bin/bash  						 // 啟動一個容器
[root@localhost ~]#   pipework  br0 centos7-ssh 192.168.1.70/[email protected]  // 為容器指定ip地址
[root@localhost ~]#   docker attach centos7-ssh                				 // 進入centos7-ssh容器內部
[root@c6915d22954d ~]# cp -r /etc/skel/.bash* /root/        				 // 拷貝環境變數到root下。
### --- 啟動容器並操作

[root@localhost ~]# docker attach centos7-ssh                				// 容器未啟動,啟動容器
You cannot attach to a stopped container, start it first
[root@localhost ~]# docker start centos7-ssh                 				// 啟動容器
[root@localhost ~]# docker ps                                				// 檢視容器起來了沒
[ root@localhost ~]#  docker attach centos7-ssh              				// 進去後發現docker沒有ip地址,重啟後ip地址失效。
[root@localhost ~]#  docker start centos7-ssh
[root@localhost ~]#  pipework  br0 centos7-ssh 192.168.1.70/[email protected]  // 指定ip地址
[root@c6915d22954d /]#  route -n     										// 檢視它的閘道器
[root@c6915d22954d /]#  service sshd restart                 				// 啟動ssh服務。
### --- 將.tar檔案上傳到docker倉庫用來做映象。
### --- 如何把封裝好的.tar 檔案上傳到docker倉庫:兩種方案。

[root@localhost ~]# docker  import <centos7.tar
[root@localhost ~]# cat centos7.tar  |docker import -centos7
### --- docker常用操作

[root@localhost ~]# docker vison        									// 檢視docker版本
[root@localhost ~]# docker  search centos   								// 搜尋可用的映象
[root@localhost ~]#  docker images 											// 檢視當前系統可用的映象
[root@localhost ~]#  docker pull centos 									// 下載映象
[root@localhost ~]#  cat centos.tar |docker  import - centos6    			// docker 匯入映象
[root@localhost ~]#  docker export id > centos6.tar  						// 匯出映象
[root@localhost ~]#  docker run centos echo “hello word” 					// 在容器中執行hello word!
[root@localhost ~]# docker run cnntos yum install ntpdate 					// 在容器中安裝ntpdate程式
[root@localhost ~]#  docker ps -l 											// 命令獲得最後一個容器的ID,
[root@localhost ~]#  docker ps -a 											// 檢視所有的容器
[root@localhost ~]# docker commit 2313132 centos:v1 					   // 例如執行docker commit 提交剛修改的容器
[root@localhost ~]# docker run centos:latest cat /etc/passwd  				  // 檢視容器裡面的密碼檔案
[root@localhost ~]#  docker run -i -t centos:latest /bin/bash  				  // 在容器裡啟動一個/bin/bash shell 環境,可以登入進入操作,其中-t表示開啟一個終端的意思,-i表示可以互動輸入
[root@5ca6ca0c184c /]# ip addr list  										   // 檢視容器的ip地址
[root@localhost ~]# docker run -d -p 80:80 -p 8022:22 centos:v2 /bin/bash -D   // -p制定容器啟動後docker上執行的埠號對映及容器裡執行的埠,80:80 第一個80 表示docker系統上的80 ,第二個80表示docker虛擬機器裡面的埠,使用者預設訪問本機80 埠,自動對映到容器裡面的80 埠。
[root@localhost ~]# docker rmi 映象ID   										 // 刪除映象
### --- 安裝ssh服務
### --- 安裝常用的基礎軟體包:

[root@localhost ~]# yum install  vim iotop bc gcc gcc-c++ glibc \
glibc-devel pcre pcre-devel openssl  openssl-devel zip unzip zlib-devel \
net-tools lrzsz tree ntpdate telnet lsof tcpdump wget libevent \
libevent-devel bc  systemd-devel bash-completion traceroute -y
### --- 安裝ssh:
 
[root@6cbc9ebfbc5f /]# yum -y install openssl openssh-server openssh-client  openssh-devel

[root@6cbc9ebfbc5f ~]# vim /etc/ssh/sshd_config   						// 修改配置檔案
PermitRootLogin yes   													// 把no改為yes
UsePAM no               												// 改為no
Port 22

[root@6cbc9ebfbc5f ~]# systemctl restart sshd 							// 重啟ssh服務
[root@6cbc9ebfbc5f ~]# passwd  											// 更改root密碼  //此時,就可以使用xshell去連結了。
### --- 將容器打包成映象並匯入匯出:

~~~		# 將容器打包成映象
[root@localhost ~]# docker commit -a "yanqi" -m "centos7-ssh" 6cbc9ebfbc5f  centos7-ssh:zabbix-agent 
~~~		# 從容器匯出映象。
[root@localhost ~]# docker save > centos7-ssh.tar   centos7-ssh:zabbix-agent
~~~		# 匯入映象
[root@localhost ~]# docker import centos7-ssh.tar centos7-ssh1:zabbix-agent
~~~		# 匯入映象
[root@localhost ~]# cat centos7-ssh.tar | docker import - centos7-ssh2:zabbix-agent
### --- 建立容器並執行
### --- 使用該命令去建立一個容器:具備systemctl啟動許可權
### --- 建立zabbix-docker-client10臺裝置推薦使用命令

~~~		# 建立容器並執行     
[root@localhost ~]# docker run -tid --net=none --name=centos7-ssh2 --privileged=true centos7-ssh:zabbix-agent /sbin/init
~~~		# 寫入IP地址
[root@localhost ~]# pipework  br0 centos7-ssh 192.168.1.70/[email protected]
~~~		# 使用該命令進入容器
[root@localhost ~]# docker exec -it centos7-ssh1 /bin/bash  

附錄一:下載docker源並安裝測試
### --- 下載docker源並安裝測試

[root@localhost ~]# wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# yum install -y docker-ce*
[root@localhost ~]# rpm -qa |grep -E "docker"
[root@localhost ~]#  systemctl start docker.service 
[root@localhost ~]#  systemctl enable docker.service 
~~~ 執行測試程式
[root@localhost ~]# docker run hello-world  
附錄二:新增本地源
### ---- 新增本地源

[root@localhost ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": [
"https://kfwkfulq.mirror.aliyuncs.com",
"https://2lqq34jg.mirror.aliyuncs.com",
"https://pee6w651.mirror.aliyuncs.com",
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com"
],
"dns": ["8.8.8.8","8.8.4.4"]
}
[root@localhost docker]# systemctl restart docker.service
附錄三:批量建立10臺主機
### --- 批量建立10臺主機:批量建立10臺centos-ssh伺服器用來做zabbix客戶端:

[root@localhost ~]# docker ps -aq    									// 檢視docker安裝的程式
[root@localhost ~]# docker ps -aq |xargs docker rm -f  					// 刪除docker下的所有程式

~~~ 建立10臺主機用來做client
[root@localhost ~]# for i in $(seq 1 10);do docker run -itd --privileged centos:centos7 ;done
[root@localhost ~]# docker ps        									// 檢視建立的十臺主機
[root@localhost ~]# docker exec -it aa9b5d69de24 /bin/bash  			// 進入到這個容器之下。
附錄四:docker下建立自定義網路地址
### --- docker下建立自定義網路地址

[root@localhost ~]# docker network ls        								// 檢視網路模式
[root@localhost ~]# docker network create --subnet=10.0.1.1/24 extnetwork   // 建立自定義的網路型別:extnetwork形的bridge網路。
b92097ff8046        extnetwork          bridge              local
[root@localhost network-scripts]# docker network rm b92097ff8046    		// 刪除docker網路配置
附錄五:檢視容器Ip地址
### --- 檢視容器Ip地址

~~~ 檢視docker容器的IP地址
[root@localhost ~]# docker inspect 792dad250145
~~~ 檢視容器的IP地址
[root@localhost ~]#   docker inspect --format='{{.NetworkSettings.IPAddress}}' 792dad250145
附錄六:新增路由規則
### --- 新增路由規則:

br-b92097ff8046
ip route add 10.0.1.1/24 via 192.168.1.59 dev br-b92097ff8046
附錄七:安裝linux容器基礎軟體包
### --- 安裝常用的基礎軟體包:

### --- 安裝ssh:
[root@6cbc9ebfbc5f /]# yum -y install openssl openssh-server openssh-client  openssh-devel
[root@6cbc9ebfbc5f ~]# vim /etc/ssh/sshd_config   					//修改配置檔案
PermitRootLogin yes   												//把no改為yes
UsePAM no                											//改為no
Port 22
[root@6cbc9ebfbc5f ~]# systemctl restart sshd 					    //重啟ssh服務
[root@6cbc9ebfbc5f ~]# passwd  										//更改root密碼  //此時,就可以使用xshell去連結了。
附錄八:學習地址
### --- 學習地址:

https://ke.qq.com/course/348394?taid=2789714403086570








===============================END===============================


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart                                                                                                                                                    ——W.S.Landor



來自為知筆記(Wiz)