生成镜像的三个方法简介
在讨论给image打tag之前,我们需要先有一个镜像来玩。三种方法生成镜像:
1. from Dockfile
# docker build -t <image_name>
2. from local container
# docker commit -m ''[description]" -a "[author" [local_container_name] [image_name]
For example:
[root@k8s-ubuntu-mini ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost:5000/hello-me latest 2cb0d9787c4d 7 weeks ago 1.85 kB
[root@k8s-ubuntu-mini ~]# docker run -d localhost:5000/hello-me
cba2ce46ca021c52f8ffc2d4163a443b9c96dd909c64e41665c06bea27800353
[root@k8s-ubuntu-mini ~]# docker commit -m "test commit command" -a "Josie" cba2ce46ca021c52f8ffc2d4163a443b9c96dd909c64e41665c06bea27800353 hello-me-commit-test
sha256:7d537ed8975ca060c12c8bb6292b8f96d1210cf5a3b8519bcdf75d8f24e61ab6
[root@k8s-ubuntu-mini ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-me-commit-test latest 7d537ed8975c 7 seconds ago 1.85 kB
localhost:5000/hello-me latest 2cb0d9787c4d 7 weeks ago 1.85 kB
[root@k8s-ubuntu-mini ~]#
3. from remote container
## on server1 :
# docker export [server1_container] >[onefile].tar
# scp me@[server1]:[c [server2]/[path]
## on server2:
# cat [onefile].tar | docker import - [image_name]
For example:
[root@k8s-ubuntu-mini ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cba2ce46ca02 localhost:5000/hello-me "/hello" 3 minutes ago Exited (0) 3 minutes ago nifty_booth
[root@k8s-ubuntu-mini ~]# docker export cba2ce46ca02 >hello-me-export-test.tar
[root@k8s-ubuntu-mini ~]# ls -lrt
total 16
-rw-------. 1 root root 1261 Aug 27 15:16 anaconda-ks.cfg
-rw-r--r--. 1 root root 10752 Sep 3 16:26 hello-me-export-test.tar
[root@k8s-ubuntu-mini ~]# cat hello-me-export-test.tar | docker import - hello-me-import-test
sha256:0b7dd0e1540d8f68548228044654ae888681e0b7dc1a974526675828cb80db08
[root@k8s-ubuntu-mini ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-me-import-test latest 0b7dd0e1540d 6 seconds ago 1.86 kB
hello-me-commit-test latest 7d537ed8975c 3 minutes ago 1.85 kB
localhost:5000/hello-me latest 2cb0d9787c4d 7 weeks ago 1.85 kB
[root@k8s-ubuntu-mini ~]#
镜像打tag和上载
镜像的完整tag格式:
registry / repository : tag
[registry-server]:[port] / [username] / [image_name]:[tag]
ps: registry表示镜像存放的网络地址; repository表示的是镜像们之间的关联性。也可以说 :[registry-server]:[port] / [username] / [image_name] 是完整的repository命名组成。
目的:
本地使用来说,主要是标记版本;
命令:
# docker tag [image_name] [image_name]:[tag]
# docker tag [image_name]:[tag1] [image_name]:[tag2]
共享或远端存放,是为了指明 镜像在命名空间里的唯一名字
命令:
# docker tag [image_name] [registry-server]:[port] / [username] / [image_name]:[tag]
# docker tag [image_name] [username] / [image_name]:[tag]
然后就可以上载啦~~(需要有docker账户,并先login)
# docker push [registry-server]:[port] / [username] / [image_name]:[tag]
# docker push [username] / [image_name]:[tag]
For example:
[root@k8s-ubuntu-mini ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: josiejiao
Password:
Login Succeeded
[root@k8s-ubuntu-mini ~]# docker tag hello-me-commit-test docker.io/josiejiao/hello-me-commit-test:hahaha
[root@k8s-ubuntu-mini ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/josiejiao/hello-me-commit-test hahaha 7d537ed8975c 27 minutes ago 1.85 kB
......
[root@k8s-ubuntu-mini ~]# docker push docker.io/josiejiao/hello-me-commit-test:hahaha
The push refers to a repository [docker.io/josiejiao/hello-me-commit-test]
3e324f1cd607: Pushed
ee83fc5847cb: Mounted from library/hello-world
hahaha: digest: sha256:dd8d42acabfc76763f6553a156530a81df446fa842d5149fc847c132151964f8 size: 731
镜像和tag的关系:
一个镜像可以有多个标签,类似多个名字。即:在命名空间里,镜像和名字是一对多的关系。
删掉不唯一的tag(docker rmi / untagged),镜像不会删除。
latest镜像标签,总是自动分配个image_name相同的image的最新生成版本。比如:
[root@k8s-ubuntu-mini ~]# cat hello-me-export-test.tar | docker import - hello-me-import-test
sha256:0b7dd0e1540d8f68548228044654ae888681e0b7dc1a974526675828cb80db08
[root@k8s-ubuntu-mini ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-me-import-test latest 0b7dd0e1540d 6 seconds ago 1.86 kB
hello-me-commit-test latest 7d537ed8975c 3 minutes ago 1.85 kB
localhost:5000/hello-me latest 2cb0d9787c4d 7 weeks ago 1.85 kB
## repeat the import action
[root@k8s-ubuntu-mini ~]# cat hello-me-export-test.tar | docker import - hello-me-import-test
sha256:eaddd3d60fb1f74c9325958c6d05de0c31c9821c30a11fa48bac271612499bea
[root@k8s-ubuntu-mini ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-me-import-test latest eaddd3d60fb1 2 seconds ago 1.86 kB
<None> <None> 0b7dd0e1540d 7 minutes ago 1.86 kB
hello-me-commit-test latest 7d537ed8975c 10 minutes ago 1.85 kB
localhost:5000/hello-me latest 2cb0d9787c4d 7 weeks ago 1.85 kB
镜像的查找、拉取
查找 search:
# docker search [image_name]
在docker hub https://hub.docker.com web搜索,能看到更多信息
拉取pull:
对应于push
# docker pull [registry-server]:[port] / [username] / [image_name]:[tag]
# docker pull [username] / [image_name]:[tag]
# docker pull [username] / [image_name]
# docker pull [image_name]
镜像的装载和保存
相对于从container得到image的方法 -- export / import,这里是要从repository / image,得到image的方法 -- save / load。
保存 save:
为什么还要从image得到image呢? 因为docker image是存在DB里的,save得到的是位于FS上。
默认的导出位置是stdout,可以用-o 参数重定向到tar文件
输入可以是一个repository,这时repository所有关联image版本都会被到出;也可以是一个[image:tag]。
[root@k8s-ubuntu-mini ~]# docker save -o /tmp/hello-me-save-test.tar localhost:5000/hello-me
[root@k8s-ubuntu-mini ~]# ls /tmp/hello-me-save-test.tar
/tmp/hello-me-save-test.tar
装载 load:
由tar ball 装载image到DB
[root@k8s-ubuntu-mini tmp]# docker load -i /tmp/hello-me-save-test.tar
open /var/lib/docker/tmp/docker-import-974526576/hello-me-save-test/json: no such file or directory
[root@k8s-ubuntu-mini tmp]#
网友评论