Docker容器导入导出

网友投稿 247 2022-10-22

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​​

​​第二种方式 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 crm

2.将容器commit为镜像

[root@entel2 docker]# docker commit ec1f968cc0d0 entel_crm_image6efd670bc0797350799425efd28191e53f17b494e44f92879c55dae6bf36c89f[root@entel2 docker]# docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEentel_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:/dockerroot@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 psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[root@entel1 docker]# docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE[root@entel1 docker]# docker load --input entle_crm_image.tar [root@entel1 docker]# docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEentel_crm_image latest 6efd670bc079 Less than a second ago 8.335 GB

load成功

6.目标主机启动容器

[root@entel1 docker]# docker run -d --name crm entle_crm_imageUnable to find image 'entle_crm_image:latest' locallyPulling repository entle_crm_imageGet dial tcp: lookup index.docker.io: no such host

丢,entle_crm_image 名字写错了……

也可以使用imageid来启动

[root@entel1 docker]# docker run -d --name crm 6efd670bc07939dac87560c335f0129a2c6e980b674559cdc157b74e37811e1bb7246109d169[root@entel1 docker]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES39dac87560c3 6efd670bc079 "/usr/sbin/sshd -D" 34 seconds ago Up 33 seconds 8080/tcp crm

这种方式创建的 ,默认分配的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 showbridge name bridge id STP enabled interfacesdocker0 8000.56847afe9799 no [root@entel1 ~]# brctl addbr bridge0[root@entel1 ~]# brctl showbridge name bridge id STP enabled interfacesbridge0 8000.000000000000 no docker0 8000.56847afe9799 no [root@entel1 ~]# ifconfig bridge0 172.25.243.254 netmask 255.255.255.0 up[root@entel1 ~]# ifconfig bridge0bridge0 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 -nLChain 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 psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf936f206d2ed 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/bashif [ -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" exitfiCONTAINERID=$1SETIP=$2SETMASK=$3GATEWAY=$4ETHNAME=$5#verify the network card exist or notifconfig $ETHNAME > /dev/null 2>&1if [ $? -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 fifi#pid=`docker inspect -f '{{.State.Pid}}' $CONTAINERID`mkdir -p /var/run/netnsfind -L /var/run/netns -type l -deleteif [ -f /var/run/netns/$pid ]; then rm -f /var/run/netns/$pidfiln -s /proc/$pid/ns/net /var/run/netns/$pid#ip link add $ETHNAME type veth peer name vethContainerbrctl addif bridge0 $ETHNAMEip link set $ETHNAME upip 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 eth0ip netns exec $pid ip link set eth0 upip netns exec $pid ip addr add $SETIP/$SETMASK dev eth0ip netns exec $pid ip route add default via $GATEWAY

第二种方式 export

将容器export为tar文件,然后目标主机import为镜像,最后使用镜像启动容器

这个方法,启动容器时,默认并不会启动程序,需启动容器时加启动参数。

经常会出现问题,不建议使用该方法。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:关于@Query注解的用法(Spring Data JPA)
下一篇:Mac 下Docker操作SQLServer数据库
相关文章

 发表评论

暂时没有评论,来抢沙发吧~