一、容器文件系统打包
将容器的文件系统打包成tar文件,也就是把正在运行的容器直接导出为tar包的镜像文件
export
Export a container’s filesystem as a tar archive
有两种方式:
第一种:
[root@yixuan ~]# docker ps #运行一个容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96e2b7265d93 daocloud.io/library/centos:6 "/bin/bash" About an hour ago Up About an hour c6
[root@yixuan ~]# docker exec -it c6 /bin/bash
[root@96e2b7265d93 /]# vi a.txt #编辑一个文件
123
[root@96e2b7265d93 /]# yum install -y vim wget #安装一个软件
[root@yixuan ~]# docker export -o centos6-1.tar 96e2b726
-o, --output
[root@yixuan ~]# ls #保存到当前目录下
anaconda-ks.cfg centos6-1.tar
第二种:
[root@yixuan ~]# docker export 容器名称 > 镜像.tar
导入镜像归档文件到其他宿主机:
import
[root@yixuan ~]# docker import centos6-1.tar centos6-1:v1
sha256:b8414448c913259a28d623643a7b67a6c13da9b3259e70ee5febb16c2b0b1095
[root@yixuan ~]# docker images
[root@yixuan ~]# docker run -it --name c6.1 centos6-1:v1 /bin/bash
[root@4a29d58d3bd2 /]# ls
a.txt bin dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var
[root@4a29d58d3bd2 /]# cat a.txt
123123
二、通过容器创建本地镜像
背景:容器运行起来后,又在里面做了一些操作,并且要把操作结果保存到镜像里
方案:使用 docker commit 指令,把一个正在运行的容器,直接提交为一个镜像。
commit 是提交的意思,类似告诉svn服务器我要生成一个新的版本。
例子:
在容器内部新建了一个文件
[root@yixuan ~]# docker run -it --name c7 daocloud.io/library/centos:7 /bin/bash
[root@2e8f79cb5922 /]# touch test.txt
# 将这个新建的文件提交到镜像中保存
[root@yixuan ~]# docker commit 2e8f79cb5922 soso/test:v2
sha256:2214bad66e9b1c2079dc89a2e14e997604237cd49a6dc6c29d84e915fbbeb5bd
[root@yixuan ~]# docker images
也可以这样例子:
# docker commit -m "my images version1" -a "soso" 108a85b1ed99 daocloud.io/ubuntu:v2
sha256:ffa8a185ee526a9b0d8772740231448a25855031f25c61c1b63077220469b057
-m 添加注释
-a 作者
108a85b1ed99 容器环境id
daocloud.io/ubuntu:v2 镜像名称:hub的名称/镜像名称:tag
-p,–pause=true 提交时暂停容器运行
镜像迁移
保存一台宿主机上的镜像为tar文件,然后可以导入到其他的宿主机上:
save
将镜像打包,与下面的load命令相对应
[root@yixuan ~]# docker save -o nginx.tar daocloud.io/library/nginx
load
与上面的save命令相对应,将上面sava命令打包的镜像通过load命令导入,(实验环境中原来机器上面有镜像可以先删除掉。)
[root@yixuan ~]# docker load < nginx.tar
[root@yixuan ~]# docker images
把容器导出成tar包 export import
把容器做成镜像 commit -a "" -m ""
把镜像保存为tar包 save load
通过Dockerfile创建镜像
Docker 提供了一种更便捷的方式,叫作 Dockerfile
docker build命令用于根据给定的Dockerfile构建Docker镜像。
docker build语法:
# docker build [OPTIONS] <PATH | URL | ->
1. 常用选项说明
--build-arg,设置构建时的变量
--no-cache,默认false。设置该选项,将不使用Build Cache构建镜像
--pull,默认false。设置该选项,总是尝试pull镜像的最新版本
--compress,默认false。设置该选项,将使用gzip压缩构建的上下文
--disable-content-trust,默认true。设置该选项,将对镜像进行验证
--file, -f,Dockerfile的完整路径,默认值为‘PATH/Dockerfile’
--isolation,默认--isolation="default",即Linux命名空间;其他还有process或hyperv
--label,为生成的镜像设置metadata
--squash,默认false。设置该选项,将新构建出的多个层压缩为一个新层,但是将无法在多个镜像之间共享新层;设置该选项,实际上是创建了新image,同时保留原有image。
--tag, -t,镜像的名字及tag,通常name:tag或者name格式;可以在一次构建中为一个镜像设置多个tag
--network,默认default。设置该选项,Set the networking mode for the RUN instructions during build
--quiet, -q ,默认false。设置该选项,Suppress the build output and print image ID on success
--force-rm,默认false。设置该选项,总是删除掉中间环节的容器
--rm,默认--rm=true,即整个构建过程成功后删除中间环节的容器
示例:
docker build -t soso/bbauto:v2.1 .
docker build 是docker创建镜像的命令
-t 是标识新建的镜像属于 soso的 bbauto镜像
:v2 是tag
"."是用来指明 我们的使用的Dockerfile文件当前目录的
2.1、 创建镜像所在的文件夹和Dockerfile文件
[root@yixuan ~]# mkdir sinatra
[root@yixuan ~]# cd sinatra/
[root@yixuan sinatra]# touch Dockerfile
2.2、 在Dockerfile文件中写入指令,每一条指令都会更新镜像的信息例如:
[root@yixuan sinatra]# vim Dockerfile
#This is a comment
FROM daocloud.io/library/centos:7
MAINTAINER soso soso@yixuan
RUN touch a.txt
RUN mkdir /test
格式说明:
命令要大写,"#"是注解。
每一个指令后面需要跟空格,语法。
FROM 命令是告诉docker 我们的镜像什么从哪里下载。
MAINTAINER 是描述 镜像的创建人。
RUN 命令是在镜像内部执行。就是说他后面的命令应该是针对镜像可以运行的命令。
2.3、创建镜像
命令:
# docker build -t soso/centso:7 .
docker build 是docker创建镜像的命令
详细执行过程:
[root@yixuan sinatra]# docker build -t soso/centos:7 .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM daocloud.io/library/centos
latest: Pulling from library/centos
d8d02d457314: Pull complete
Digest: sha256:a36b9e68613d07eec4ef553da84d0012a5ca5ae4a830cf825bb68b929475c869
Status: Downloaded newer image for daocloud.io/library/centos:latest
---> 67fa590cfc1c
Step 2/4 : MAINTAINER soso soso@yixuan
---> Running in aab3d80939d8
Removing intermediate container aab3d80939d8
---> 12bae7d75a23
....