天天看點

Docker跨伺服器遷移

Docker的備份方式有export和save兩種。

export是目前的狀态,針對的是容器,docker save 是針對鏡像images。

一、鏡像的遷移—save 1.鏡像儲存 登陸到已經部署好鏡像的伺服器上面,執行以下指令進行導出

[[email protected] local]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
shiyu/centos        tomcat-centos       b61b207a5809        28 minutes ago      1.263 GB
docker.io/centos    latest              2d194b392dd1        7 hours ago         195.4 MB
docker.io/centos    7.3.1611            66ee80d59a68        4 months ago        191.8 MB
[[email protected] local]# docker save b61b >mytomcat.tar
           

2.将鏡像導入

将剛才導出的鏡像上傳到你要導入的那台伺服器上

[[email protected] local]# scp  mytomcat.tar 192.168.0.4:/usr/local/
[email protected]'s password: 
mytomcat.tar                                                                                                100% 1228MB  49.1MB/s   00:25
           

執行以下指令鏡像導入

[[email protected] local]# ll mytomcat.tar 
-rw-r--r-- 1 root root 1287580160 Mar  6 16:36 mytomcat.tar
[[email protected] local]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[[email protected] local]# docker load < mytomcat.tar
b03095563b79: Loading layer [==================================================>]   204 MB/204 MB
1d88d689ca13: Loading layer [==================================================>] 3.584 kB/3.584 kB
a997e4440673: Loading layer [==================================================>]  2.56 kB/2.56 kB
0a3b6c7be500: Loading layer [==================================================>] 309.5 MB/309.5 MB
cee5bf38dfe0: Loading layer [==================================================>] 774.1 MB/774.1 MB
Loaded image ID: sha256:b61b207a5809e56150df90e9c419f838c9a0988828773ef33aa2768e589c19c2kB/774.1 MB
[[email protected] local]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              b61b207a5809        About an hour ago   1.263 GB
[[email protected] local]# docker tag b61b mytomcat:2.0
[[email protected] local]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mytomcat            2.0                 b61b207a5809        About an hour ago   1.263 G
           

二、容器的遷移—export 1.導出容器

[[email protected] local]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
8578122af714        b61b                "/bin/sh -c '/usr/loc"   56 minutes ago      Up 56 minutes       0.0.0.0:8090->8088/tcp   naughty_bartik
[[email protected] local]# docker export 8578122af714 > mytomcat_export.tar
           

2.導入到新的伺服器上

同樣需要将剛才的導出的容器備份上傳到目标伺服器上,執行下面的指令

[[email protected] local]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[[email protected] local]# cat mytomcat_export.tar |docker import - centos:tomcat
sha256:b3d5c7409cf020ea3f1ce57865e8e476e878e347963b3da02cab23b1d7464ce2
[[email protected] local]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              tomcat              b3d5c7409cf0        11 seconds ago      1.263 GB
           

注意:運作導入的鏡像的時候必須帶command,否則啟動報如下錯誤

[[email protected] ~]# docker run -d -p 8090:8088 b3d5
/usr/bin/docker-current: Error response from daemon: No command specified.
See '/usr/bin/docker-current run --help'.
           

繼續閱讀