有時候一些必要的軟件必須基於內網安裝,於是采用docker鏡像遷移的方式進行。
1.鏡像遷移=離線獲取鏡像(save vs load)
1.基於nginx構建一個簡單的鏡像(當然公網上直接拉下來的鏡像也可以)
(1)Dockerfile內容如下:
FROM ubuntu MAINTAINER qlq qlq@163.com RUN apt-get update RUN apt-get install -y nginx COPY index.html /var/www/html ENTRYPOINT ["/usr/sbin/nginx", "-g", "daemon off;"] EXPOSE 80
index.html內容如下:
<Html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <Head> <title>nginx 首頁</title> </Head> <Body> 歡迎你! </Body> </Html>
(2)構建鏡像
docker build -t mynginx .
(3)查看鏡像
$ docker images | grep myn mynginx latest d9d27ba1207a 5 minutes ago 155MB
2.使用docker save命令對鏡像進行打包
(1)docker save 用法如下:
$ docker save --help Usage: docker save [OPTIONS] IMAGE [IMAGE...] Save one or more images to a tar archive (streamed to STDOUT by default) Options: -o, --output string Write to a file, instead of STDOUT
(2)打包
Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest $ docker save -o mynginx.tar mynginx Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest $ ll total 155526 -rw-r--r-- 1 Administrator 197121 180 6月 1 11:39 Dockerfile -rw-r--r-- 1 Administrator 197121 161 6月 1 13:32 index.html -rw-r--r-- 1 Administrator 197121 159256064 6月 1 13:47 mynginx.tar
3.docker load加載打包后的鏡像
(1)先刪除上面的本地鏡像
$ docker rmi -f mynginx
Untagged: mynginx:latest
(2)load用法如下:
$ docker load --help Usage: docker load [OPTIONS] Load an image from a tar archive or STDIN Options: -i, --input string Read from tar archive file, instead of STDIN -q, --quiet Suppress the load output
(3)加載mynginx.tar構建鏡像
Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest $ docker load -i mynginx.tar Loaded image: mynginx:latest Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest $ docker images | grep myng mynginx latest d9d27ba1207a 12 minutes ago 155MB
2. docker export vs import (容器導出為tar VS tar導入為鏡像)
1.docker export:將容器(文件系統)作為一個tar歸檔文件導出到STDOUT。
$ docker export --help Usage: docker export [OPTIONS] CONTAINER Export a container's filesystem as a tar archive Options: -o, --output string Write to a file, instead of STDOUT
從接的參數猜測,直接接container,多半就是dump rootfs了
例如:
Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f6f212ca3017 ubuntu "/bin/bash" 41 minutes ago Up 41 minutes practical_tesla Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest $ docker export -o ubuntu.tar f6f212ca3017 Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest $ ls | grep ubuntu ubuntu.tar
2. docker import 從歸檔文件中創建鏡像。
$ docker import --help Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]] Import the contents from a tarball to create a filesystem image Options: -c, --change list Apply Dockerfile instruction to the created image -m, --message string Set commit message for imported image
注意:docker import后面接的是docker export導出的文件,也就是一個文件系統,所以導入的鏡像是不帶歷史的
使用docker history $image_name 查看鏡像,只有一層
Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest $ docker import ubuntu.tar myubuntu #基於容器導出的tar構建鏡像 sha256:9771bf4f1f7470b949b659894fbca936556b6350ee8c5bd76729a8f831243903 Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE myubuntu latest 9771bf4f1f74 3 seconds ago 162MB mynginx latest d9d27ba1207a 3 hours ago 155MB ubuntu latest 1d622ef86b13 5 weeks ago 73.9MB ecm_tomcat2 latest bb7bd1013a9c 3 months ago 889MB ecm_tomcat latest c62a6bd19ecf 3 months ago 889MB nginx latest 2073e0bcb60e 4 months ago 127MB hub.c.163.com/ray11415/tesseract latest b49db28cb085 23 months ago 454MB hub.c.163.com/library/tomcat latest 72d2be374029 2 years ago 292MB hub.c.163.com/springwen/oracle-xe-11g latest 04851454491b 2 years ago 792MB hub.c.163.com/library/tomcat 7.0.78-jre7-alpine abe56df7b966 2 years ago 151MB hub.c.163.com/library/mysql latest 9e64176cd8a2 3 years ago 407MB hub.c.163.com/library/mysql 5.5.48 7d1caa7ed830 4 years ago 256MB Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest $ docker history myubuntu #查看鏡像歷史 IMAGE CREATED CREATED BY SIZE COMMENT 9771bf4f1f74 15 seconds ago 162MB Imported from - Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest $ docker history ubuntu IMAGE CREATED CREATED BY SIZE COMMENT 1d622ef86b13 5 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 5 weeks ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B <missing> 5 weeks ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 811B <missing> 5 weeks ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 1.01MB <missing> 5 weeks ago /bin/sh -c #(nop) ADD file:a58c8b447951f9e30… 72.8MB
3.docker commit 使用容器創建鏡像
$ docker commit -h Flag shorthand -h has been deprecated, please use --help Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] Create a new image from a container's changes Options: -a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>") -c, --change list Apply Dockerfile instruction to the created image -m, --message string Commit message -p, --pause Pause container during commit (default true)
commit是合並了save、load、export、import這幾個特性的一個綜合性的命令,它主要做了:
(1)將container當前的讀寫層保存下來,保存成一個新層
(2)和鏡像的歷史層一起合並成一個新的鏡像
(3)如果原本的鏡像有3層,commit之后就會有4層,最新的一層為從鏡像運行到commit之間對文件系統的修改
例如:基於ubuntu構建自己的鏡像
Administrator@MicroWin10-1535 MINGW64 ~/Desktop $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e7856417a422 ubuntu "/bin/bash" 4 minutes ago Up 4 minutes inspiring_shirley Administrator@MicroWin10-1535 MINGW64 ~/Desktop $ docker commit e7856417a422 myubuntu2 sha256:e5757f2d6d4b3335a474eb53c7a7ebbe9478db871669657dfc01bc411c76ff7b Administrator@MicroWin10-1535 MINGW64 ~/Desktop $ docker images | grep ubun myubuntu2 latest e5757f2d6d4b 9 seconds ago 73.9MB myubuntu latest 9771bf4f1f74 28 minutes ago 162MB ubuntu latest 1d622ef86b13 5 weeks ago 73.9MB
查看鏡像歷史:
Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest $ docker history ubuntu IMAGE CREATED CREATED BY SIZE COMMENT 1d622ef86b13 5 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 5 weeks ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B <missing> 5 weeks ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 811B <missing> 5 weeks ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 1.01MB <missing> 5 weeks ago /bin/sh -c #(nop) ADD file:a58c8b447951f9e30… 72.8MB Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest $ docker history myubuntu2 IMAGE CREATED CREATED BY SIZE COMMENT e5757f2d6d4b 6 hours ago /bin/bash 20B 1d622ef86b13 5 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 5 weeks ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B <missing> 5 weeks ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 811B <missing> 5 weeks ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 1.01MB <missing> 5 weeks ago /bin/sh -c #(nop) ADD file:a58c8b447951f9e30… 72.8MB