Docke 1 12 基础篇 48条命令 3
Posted firsttry
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docke 1 12 基础篇 48条命令 3相关的知识,希望对你有一定的参考价值。
本文针对Docker1.12的48条命令,找出了Image相关的9条,划分未必精确,大家也不会纠结。接下来看看这9条命令都是怎么用的。
镜像相关的docker命令
项番 | 命令 | 详解 |
---|---|---|
No.2 | build | Build an image from a Dockerfile |
No.17 | load | Load an image from a tar archive or STDIN |
No.18 | login | Log in to a Docker registry. |
No.19 | logout | Log out from a Docker registry. |
No.27 | pull | Pull an image or a repository from a registry |
No.28 | push | Push an image or a repository to a registry |
No.32 | rmi | Remove one or more images |
No.34 | save | Save one or more images to a tar archive (streamed to STDOUT by default) |
No.42 | tag | Tag an image into a repository |
build
build是使用Dockerfile方式创建image时所必需的命令,比如,
[root@liumiaocn ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@liumiaocn ~]# cat Dockerfile
FROM busybox
MAINTAINER liumiaocn@outlook.com
[root@liumiaocn ~]#
- 1
- 2
- 3
- 4
- 5
- 6
使用这个2行的Dockerfile创建出一个busybox的镜像
[[email protected] ~]# docker build -t busybox .
Sending build context to Docker daemon 86.19 MB
Step 1 : FROM busybox
Trying to pull repository docker.io/library/busybox ...
latest: Pulling from docker.io/library/busybox
8ddc19f16526: Pull complete
Digest: sha256:a59906e33509d14c036c8678d687bd4eec81ed7c4b8ce907b888c607f6a1e0e6
Status: Downloaded newer image for docker.io/busybox:latest
---> 2b8fd9751c4c
Step 2 : MAINTAINER [email protected]
---> Running in 3289de364e7f
---> 51cb1d751e30
Removing intermediate container 3289de364e7f
Successfully built 51cb1d751e30
[[email protected] ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
因为事前没有busybox的镜像,所以在build的时候会自动下载,build之后再次确认信息如下
[root@liumiaocn ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 51cb1d751e30 8 seconds ago 1.093 MB
docker.io/busybox latest 2b8fd9751c4c 3 months ago 1.093 MB
[root@liumiaocn ~]#
- 1
- 2
- 3
- 4
- 5
IMAGE ID为51cb1d751e30的是刚刚build的镜像,从CREATED的时间可以看出,以及REPOSITORY,最关键的是build最后会提示出该IMAGEID“Successfully built 51cb1d751e30”。build的时候有很多option可以使用,基础教程教程只负责入门,详细使用方法自行研究,其他命令也是如此。
tag
有很多用法,主要用来建立TAG和REPOSITORY之间的关联。从直观地感受上来说是用来修改docker images出来的REPOSITORY和tag的信息的。比如:
[root@liumiaocn ~]# docker tag docker.io/busybox:latest hello:world
[root@liumiaocn ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 51cb1d751e30 8 minutes ago 1.093 MB
docker.io/busybox latest 2b8fd9751c4c 3 months ago 1.093 MB
hello world 2b8fd9751c4c 3 months ago 1.093 MB
[root@liumiaocn ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
请实际执行的时候REPOSITORY和TAG使用实际意义,例子只是找个乐子而已。但是请注意到修改前后的IMAGE ID都是同一个,说明实际docker在处理这个操作的时候应该只是做了一个alias类似的动作。
rmi
rmi即为remove image的意思,等我们以后研究的东西全部都用汉语拼音的缩写,scjx,累死他们不会汉语的。
[root@liumiaocn ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/busybox latest 2b8fd9751c4c 3 months ago 1.093 MB
hello world 2b8fd9751c4c 3 months ago 1.093 MB
[root@liumiaocn ~]# docker rmi hello:world
Untagged: hello:world
[root@liumiaocn ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/busybox latest 2b8fd9751c4c 3 months ago 1.093 MB
[root@liumiaocn ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
删除tag的内容其实只是untag一下,如果实际删除还会有实际的删除信息提示出来
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/busybox latest 2b8fd9751c4c 3 months ago 1.093 MB
[[email protected] ~]#
[[email protected] ~]# docker rmi docker.io/busybox
Untagged: docker.io/busybox:latest
Deleted: sha256:2b8fd9751c4c0f5dd266fcae00707e67a2545ef34f9a29354585f93dac906749
Deleted: sha256:8ac8bfaff55af948c796026ee867448c5b5b5d9dd3549f4006d9759b25d4a893
[[email protected] ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
pull
pull命令用于从dockerhub或者自建的repository上pull下相关的image,一般使用search先确认,比如我们pull以下刚才下载的busybox
docker build的时候其根据依赖关系,如果本地没有该镜像的话,就会自动地到dockerhub上去拖。
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[[email protected] ~]# docker pull busybox
Using default tag: latest
Trying to pull repository docker.io/library/busybox ...
latest: Pulling from docker.io/library/busybox
8ddc19f16526: Pull complete
Digest: sha256:a59906e33509d14c036c8678d687bd4eec81ed7c4b8ce907b888c607f6a1e0e6
Status: Downloaded newer image for docker.io/busybox:latest
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/busybox latest 2b8fd9751c4c 3 months ago 1.093 MB
[[email protected] ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
save
将镜像文件保存为一个archive文件
[root@liumiaocn ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/busybox latest 2b8fd9751c4c 3 months ago 1.093 MB
[root@liumiaocn ~]# docker save busybox >busybox.tar
[root@liumiaocn ~]# ll busybox.tar
-rw-r--r--. 1 root root 1302016 Sep 23 20:18 busybox.tar
[root@liumiaocn ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
load
load是save的反向操作,将save的文件load成为docker管理的镜像。
[root@liumiaocn ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@liumiaocn ~]# docker load < busybox.tar
[root@liumiaocn ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/busybox latest 2b8fd9751c4c 3 months ago 1.093 MB
[root@liumiaocn ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
再确认一下history,还都在
[root@liumiaocn ~]# docker history 2b8fd9751c4c
IMAGE CREATED CREATED BY SIZE COMMENT
2b8fd9751c4c 3 months ago /bin/sh -c #(nop) CMD ["sh"] 0 B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:9ca60502d646bdd815 1.093 MB
[root@liumiaocn ~]#
- 1
- 2
- 3
- 4
- 5
login
login操作是了演示后面的logout和push命令,用户和repository之间建立的关联通过login/logout/pull/push来进行。
既可以和dockerhub进行连携,也可以在企业自己创立的私库进行操作,比如如下是用liumiaocn的个人账户登陆到dockerhub
[[email protected] ~]# docker login
Username: liumiaocn
Password:
Email: [email protected].com
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
[[email protected] ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
注意此处会把login相关的用户信息放到/root/.docker/config.json中,而与之成对的logout命令则是通过这个文件判断是否在login的状态的
logout
logout则是login的反向操作
[root@liumiaocn ~]# docker logout
Remove login credentials for https://index.docker.io/v1/
[root@liumiaocn ~]#
- 1
- 2
- 3
在logout的基础上再次logout的话,会提示如下
[root@liumiaocn ~]# docker logout
Not logged in to https://index.docker.io/v1/
[root@liumiaocn ~]#
- 1
- 2
- 3
确认之后发现,config.json的auth信息已经被清空
[root@liumiaocn ~]# cat /root/.docker/config.json
{
"auths": {}
}[root@liumiaocn ~]#
- 1
- 2
- 3
- 4
push
push命令用于将本地镜像推送到远端的respository中,如果事前没有login的话,会push失败。
[root@liumiaocn ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/liumiaocn/pause-amd64 latest a7403e3eec99 2 weeks ago 350.2 kB
[root@liumiaocn ~]# docker push docker.io/liumiaocn/pause-amd64
The push refers to a repository [docker.io/liumiaocn/pause-amd64]
5f70bf18a086: Layer already exists
38157ef1625c: Layer already exists
unauthorized: authentication required
[root@liumiaocn ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
login之后,成功push。
[[email protected] ~]# docker login
Username: liumiaocn
Password:
Email: [email protected].com
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
[[email protected] ~]# docker push docker.io/liumiaocn/pause-amd64
The push refers to a repository [docker.io/liumiaocn/pause-amd64]
5f70bf18a086: Layer already exists
38157ef1625c: Layer already exists
latest: digest: sha256:c629c9e027a27d9a398e0a2d7179e3d8034a40a5e0c804d65bdaff34372818c0 size: 711
[[email protected] ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
以上是关于Docke 1 12 基础篇 48条命令 3的主要内容,如果未能解决你的问题,请参考以下文章