有时候一些必要的软件必须基于内网安装,于是采用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