继《Oracle支持在docker上跑oracle数据库了》和《在MAC上安装docker并部署oracle12.2》 之后,我们再来看看如何将docker镜像进行备份,迁移和克隆。
(一)备份:
我们用docker ps看有几个container,注意如果加-a参数,则没有running的container也会显示出来。
1 2 3 4 |
LoveHousedeiMac:idocker lovehouse$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c9f09116cc83 oracle/database:12.2.0.1-ee "/bin/sh -c 'exec ..." 23 hours ago Exited (137) 4 hours ago oracle LoveHousedeiMac:idocker lovehouse$ |
我们先将该container commit成镜像:
先检查一下已经存在的image:
1 2 3 4 5 |
LoveHousedeiMac:idocker lovehouse$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE oracle/database 12.2.0.1-ee 4f9df5f46a19 23 hours ago 14.8 GB oraclelinux 7-slim 442ebf722584 4 weeks ago 114 MB LoveHousedeiMac:idocker lovehouse$ |
停下container,并进行commit:
1 2 3 4 5 6 |
LoveHousedeiMac:idocker lovehouse$ docker stop oracle oracle LoveHousedeiMac:idocker lovehouse$ LoveHousedeiMac:idocker lovehouse$ docker commit -p c9f09116cc83 container-backup sha256:f58f6143fca7d2e001ce810b2d13b8adac9d64e4cc9f50477f0108bb246db3c0 LoveHousedeiMac:idocker lovehouse$ |
检查现有的image:
1 2 3 4 5 6 |
LoveHousedeiMac:idocker lovehouse$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE container-backup latest f58f6143fca7 4 seconds ago 15.3 GB oracle/database 12.2.0.1-ee 4f9df5f46a19 23 hours ago 14.8 GB oraclelinux 7-slim 442ebf722584 4 weeks ago 114 MB LoveHousedeiMac:idocker lovehouse$ |
将container-backup 这个image做成tar文件:
1 2 3 4 5 6 7 8 9 |
LoveHousedeiMac:idocker lovehouse$ ls docker-images-master LoveHousedeiMac:idocker lovehouse$ docker save -o ./container-backup.tar container-backup LoveHousedeiMac:idocker lovehouse$ LoveHousedeiMac:idocker lovehouse$ ls -l total 29910416 -rw------- 1 lovehouse staff 15314129920 May 21 21:48 container-backup.tar drwxr-xr-x@ 19 lovehouse staff 646 May 20 20:04 docker-images-master LoveHousedeiMac:idocker lovehouse$ |
我们将container-backup.tar备份或者迁移至别的主机,或者路径。实现了docker container的迁移。
注意,由于之前我们建立database采用了分离式的持久化,即数据文件不是在container,是在/Users/[username]/oradata下,所以我们也要备份一份这个文件。
1 2 |
cd /Users/lovehouse tar cvf oradata.tar oradata |
(二)还原或克隆:
我们这里将备份的东西,load进去,并且成为oracle_2
先将数据文件还原,且文件夹命名成oradata_2
1 2 3 4 5 6 7 8 |
LoveHousedeiMac:~ lovehouse$ pwd /Users/lovehouse LoveHousedeiMac:~ lovehouse$ ls -l total 7356176 drwxr-xr-x 4 lovehouse staff 136 May 21 21:48 iDocker drwxr-xr-x@ 6 lovehouse staff 204 May 21 22:21 oradata drwxr-xr-x@ 6 lovehouse staff 204 May 21 22:21 oradata_2 LoveHousedeiMac:~ lovehouse$ |
将备份的tar包 load进去
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
LoveHousedeiMac:~ lovehouse$ cd idocker LoveHousedeiMac:idocker lovehouse$ ls container-backup.tar docker-images-master LoveHousedeiMac:idocker lovehouse$ LoveHousedeiMac:idocker lovehouse$ docker load -i ./container-backup.tar 94fd1a061ee3: Loading layer [==================================================>] 433.9 MB/433.9 MB Loaded image: container-backup:latest LoveHousedeiMac:idocker lovehouse$ LoveHousedeiMac:idocker lovehouse$ LoveHousedeiMac:idocker lovehouse$ LoveHousedeiMac:idocker lovehouse$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE container-backup latest f58f6143fca7 About an hour ago 15.3 GB oracle/database 12.2.0.1-ee 4f9df5f46a19 25 hours ago 14.8 GB oraclelinux 7-slim 442ebf722584 4 weeks ago 114 MB LoveHousedeiMac:idocker lovehouse$ |
运行docker run创建container,注意端口要修改一下,以免占用原来的oracle这个container的端口。如果不修改,那么第二个container启动的时候会报错:port is already allocated
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
LoveHousedeiMac:idocker lovehouse$ docker run --name oracle_2 -p 1522:1521 -p 5501:5500 -v /Users/lovehouse/oradata_2:/opt/oracle/oradata container-backup:latest LSNRCTL for Linux: Version 12.2.0.1.0 - Production on 21-MAY-2017 15:21:22 Copyright (c) 1991, 2016, Oracle. All rights reserved. Starting /opt/oracle/product/12.2.0.1/dbhome_1/bin/tnslsnr: please wait... TNSLSNR for Linux: Version 12.2.0.1.0 - Production System parameter file is /opt/oracle/product/12.2.0.1/dbhome_1/network/admin/listener.ora Log messages written to /opt/oracle/diag/tnslsnr/e12ff2cf32f2/listener/alert/log.xml Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1))) Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=0.0.0.0)(PORT=1521))) Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1))) STATUS of the LISTENER ------------------------ Alias LISTENER Version TNSLSNR for Linux: Version 12.2.0.1.0 - Production Start Date 21-MAY-2017 15:21:26 Uptime 0 days 0 hr. 0 min. 2 sec Trace Level off Security ON: Local OS Authentication SNMP OFF Listener Parameter File /opt/oracle/product/12.2.0.1/dbhome_1/network/admin/listener.ora Listener Log File /opt/oracle/diag/tnslsnr/e12ff2cf32f2/listener/alert/log.xml Listening Endpoints Summary... (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1))) (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=0.0.0.0)(PORT=1521))) The listener supports no services The command completed successfully SQL*Plus: Release 12.2.0.1.0 Production on Sun May 21 15:21:26 2017 Copyright (c) 1982, 2016, Oracle. All rights reserved. Connected to an idle instance. SQL> ORACLE instance started. Total System Global Area 1610612736 bytes Fixed Size 8793304 bytes Variable Size 520094504 bytes Database Buffers 1073741824 bytes Redo Buffers 7983104 bytes Database mounted. Database opened. SQL> Disconnected from Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production ######################### DATABASE IS READY TO USE! ######################### The following output is now a tail of the alert.log: Shared IO Pool defaulting to 64MB. Trying to get it from Buffer Cache for process 83. =========================================================== Dumping current patch information =========================================================== No patches have been applied =========================================================== Starting background process CJQ0 2017-05-21T15:21:43.968340+00:00 CJQ0 started with pid=40, OS id=295 Completed: ALTER DATABASE OPEN 2017-05-21T15:21:44.275108+00:00 db_recovery_file_dest_size of 12780 MB is 0.00% used. This is a user-specified limit on the amount of space that will be used by this database for recovery-related files, and does not reflect the amount of space available in the underlying filesystem or ASM diskgroup. 2017-05-21T15:21:47.119178+00:00 Setting Resource Manager plan SCHEDULER[0x4AC9]:DEFAULT_MAINTENANCE_PLAN via scheduler window Setting Resource Manager CDB plan DEFAULT_MAINTENANCE_PLAN via parameter ORCLPDB1(3):Setting Resource Manager plan SCHEDULER[0x4AC3]:DEFAULT_MAINTENANCE_PLAN via scheduler window ORCLPDB1(3):Setting Resource Manager plan DEFAULT_MAINTENANCE_PLAN via parameter |
至此,2个docker container已经同时跑起来。container-backup:latest这个container是利用第一个克隆出来的,并且做了端口映射,将1522映射到内部的1521,将5501映射到内部的5500。
1 2 3 4 5 6 |
LoveHousedeiMac:idocker lovehouse$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e12ff2cf32f2 container-backup:latest "/bin/sh -c 'exec ..." 3 minutes ago Up 2 minutes 0.0.0.0:1522->1521/tcp, 0.0.0.0:5501->5500/tcp oracle_2 c9f09116cc83 oracle/database:12.2.0.1-ee "/bin/sh -c 'exec ..." 24 hours ago Up 6 minutes 0.0.0.0:1521->1521/tcp, 0.0.0.0:5500->5500/tcp oracle LoveHousedeiMac:idocker lovehouse$ LoveHousedeiMac:idocker lovehouse$ |