概述
需要重新部署197主机环境,copy198的镜像 进行迁移。
将容器comit成镜像,使用save和load进行镜像迁移,最后根据镜像启动容器。
步骤
198主机上的操作:
[root@entel2 docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
………………………..
ec1f968cc0d0 entel_base_image:withtt_oracle “/usr/sbin/sshd -D” 12 weeks ago Up 4 weeks crm
………………………..
1.停止容器(也可以不停止,只要下一步commit成功即可)
[root@entel2 docker]# docker stop crm crm
2.将容器commit为镜像
[root@entel2 docker]# docker commit ec1f968cc0d0 entel_crm_image 6efd670bc0797350799425efd28191e53f17b494e44f92879c55dae6bf36c89f [root@entel2 docker]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE entel_crm_image latest 6efd670bc079 17 seconds ago 8.335 GB
3.save镜像为tar文件
[root@entel2 docker]# docker save -o /docker/entle_crm_image.tar entel_crm_image [root@entel2 docker]# cd /docker [root@entel2 docker]# du -sh entle_crm_image.tar 7.8G entle_crm_image.tar
4.将tar文件scp到 目标docker主机
[root@entel2 docker]# scp entle_crm_image.tar root@10.45.7.197:/docker root@10.45.7.197's password: entle_crm_image.tar 100% 7980MB 96.2MB/s 01:23
197主机上的操作
5.在目标主机docker load导入
[root@entel1 docker]# cd /docker [root@entel1 docker]# du -sh entle_crm_image.tar 7.8G entle_crm_image.tar [root@entel1 docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@entel1 docker]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE [root@entel1 docker]# docker load --input entle_crm_image.tar [root@entel1 docker]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE entel_crm_image latest 6efd670bc079 Less than a second ago 8.335 GB
load成功
6.目标主机启动容器
[root@entel1 docker]# docker run -d --name crm entle_crm_image Unable to find image 'entle_crm_image:latest' locally Pulling repository entle_crm_image Get https://index.docker.io/v1/repositories/library/entle_crm_image/images: dial tcp: lookup index.docker.io: no such host
丢,entle_crm_image 名字写错了……
也可以使用imageid来启动
[root@entel1 docker]# docker run -d --name crm 6efd670bc079 39dac87560c335f0129a2c6e980b674559cdc157b74e37811e1bb7246109d169 [root@entel1 docker]# docker ps
这种方式创建的 ,默认分配的IP,我们想手动分配IP ,所以
先停止crm,然后 移除crm容器
[root@entel1 ~]# docker stop crm [root@entel1 ~]# docker rm crm
再重新创建容器,使用如下命令:
[root@entel1 ~]# docker run -it -d --ipc=host -h=crm --name crm --net=none entel_crm_image /usr/sbin/sshd -D f936f206d2ed9c9e1a7773a5be9b5d9a634ddb8ac46034d78df9a1a7d417625e
7 为宿主机新增一个网桥bridge0,并且配置iptables
[root@entel1 ~]# brctl show bridge name bridge id STP enabled interfaces docker0 8000.56847afe9799 no [root@entel1 ~]# brctl addbr bridge0 [root@entel1 ~]# brctl show bridge name bridge id STP enabled interfaces bridge0 8000.000000000000 no docker0 8000.56847afe9799 no [root@entel1 ~]# ifconfig bridge0 172.25.243.254 netmask 255.255.255.0 up [root@entel1 ~]# ifconfig bridge0 bridge0 Link encap:Ethernet HWaddr 1E:0D:4A:6A:C8:82 inet addr:172.25.243.254 Bcast:172.25.243.255 Mask:255.255.255.0 inet6 addr: fe80::884a:45ff:fede:17a9/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:5058725 errors:0 dropped:0 overruns:0 frame:0 TX packets:8036183 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:3775394468 (3.5 GiB) TX bytes:7018918104 (6.5 GiB) [root@entel1 ~]# iptables -t nat -A PREROUTING -p tcp -d 10.45.7.197 --dport 21205 -j DNAT --to-destination 172.25.243.103 :22 [root@entel1 ~]# service iptables save [root@entel1 ~]# iptables -t nat -nL Chain PREROUTING (policy ACCEPT) target prot opt source destination DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL DNAT tcp -- 0.0.0.0/0 10.45.7.197 tcp dpt:21205 to:192.168.123.205:22 Chain INPUT (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination DOCKER all -- 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL Chain POSTROUTING (policy ACCEPT) target prot opt source destination MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0 MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0 MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0 Chain DOCKER (2 references) target prot opt source destination [root@entel1 ~]#
8 调用脚本手工分配IP
[root@entel1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f936f206d2ed entel_crm_image "/usr/sbin/sshd -D" 20 minutes ago Up 20 minutes crm [root@entel1 ~]# ./manual_config_static_ip.sh f936f206d2ed 172.25.243.103 24 172.25.243.254 crm
manual_config_static_ip.sh
#!/bin/bash if [ -z $1 ] || [ -z $2 ] || [ -z $3 ] || [ -z $4 ] || [ -z $5 ]; then echo "*****Input the necessary parameters: CONTAINERID IP MASK GATEWAY ETHNAME" echo "*****Call the script like: sh manual_con_static_ip.sh b0e18b6a4432 192.168.5.123 24 192.168.5.1 deth0" exit fi CONTAINERID=$1 SETIP=$2 SETMASK=$3 GATEWAY=$4 ETHNAME=$5 #verify the network card exist or not ifconfig $ETHNAME > /dev/null 2>&1 if [ $? -eq 0 ]; then read -p "$ETHNAME exist,do you want delelte it? y/n " del if [[ $del == 'y' ]]; then ip link del $ETHNAME else exit fi fi # pid=`docker inspect -f '{{.State.Pid}}' $CONTAINERID` mkdir -p /var/run/netns find -L /var/run/netns -type l -delete if [ -f /var/run/netns/$pid ]; then rm -f /var/run/netns/$pid fi ln -s /proc/$pid/ns/net /var/run/netns/$pid # ip link add $ETHNAME type veth peer name vethContainer brctl addif bridge0 $ETHNAME ip link set $ETHNAME up ip link set vethContainer netns $pid # ip netns exec $pid ip link del eth0 > /dev/null 2>&1 # ip netns exec $pid ip link set dev vethContainer name eth0 ip netns exec $pid ip link set eth0 up ip netns exec $pid ip addr add $SETIP/$SETMASK dev eth0 ip netns exec $pid ip route add default via $GATEWAY
第二种方式 export
将容器export为tar文件,然后目标主机import为镜像,最后使用镜像启动容器
这个方法,启动容器时,默认并不会启动程序,需启动容器时加启动参数。
经常会出现问题,不建议使用该方法。