Docker容器导入导出
生活随笔
收集整理的這篇文章主要介紹了
Docker容器导入导出
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 概述
- 步驟
- 198主機上的操作:
- 1.停止容器(也可以不停止,只要下一步commit成功即可)
- 2.將容器commit為鏡像
- 3.save鏡像為tar文件
- 4.將tar文件scp到 目標docker主機
- 197主機上的操作
- 5.在目標主機docker load導入
- 6.目標主機啟動容器
- 7 為宿主機新增一個網橋bridge0,并且配置iptables
- 8 調用腳本手工分配IP
- manual_config_static_ip.sh
- 198主機上的操作:
- 第二種方式 export
概述
需要重新部署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 crm2.將容器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 GB3.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.tar4.將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:23197主機上的操作
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 GBload成功
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來啟動
這種方式創建的 ,默認分配的IP,我們想手動分配IP ,所以
先停止crm,然后 移除crm容器
再重新創建容器,使用如下命令:
[root@entel1 ~]# docker run -it -d --ipc=host -h=crm --name crm --net=none entel_crm_image /usr/sbin/sshd -D f936f206d2ed9c9e1a7773a5be9b5d9a634ddb8ac46034d78df9a1a7d417625e7 為宿主機新增一個網橋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.0inet6 addr: fe80::884a:45ff:fede:17a9/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:5058725 errors:0 dropped:0 overruns:0 frame:0TX packets:8036183 errors:0 dropped:0 overruns:0 carrier:0collisions: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 crmmanual_config_static_ip.sh
#!/bin/bash if [ -z $1 ] || [ -z $2 ] || [ -z $3 ] || [ -z $4 ] || [ -z $5 ]; thenecho "*****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 fiCONTAINERID=$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 ]; thenread -p "$ETHNAME exist,do you want delelte it? y/n " delif [[ $del == 'y' ]]; thenip link del $ETHNAMEelseexitfi fi # pid=`docker inspect -f '{{.State.Pid}}' $CONTAINERID` mkdir -p /var/run/netns find -L /var/run/netns -type l -deleteif [ -f /var/run/netns/$pid ]; thenrm -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為鏡像,最后使用鏡像啟動容器
這個方法,啟動容器時,默認并不會啟動程序,需啟動容器時加啟動參數。
經常會出現問題,不建議使用該方法。
總結
以上是生活随笔為你收集整理的Docker容器导入导出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux-SNAT和DNAT
- 下一篇: Docker-tag