亲宝软件园·资讯

展开

docker镜像拉取登陆上传保存等使用命令

Jeff的技术栈 人气:0

docker 中的三大基本概念

镜像

镜像就是启动一个容器的模板。

容器

容器就是对外提供服务的进程。或者容器就是镜像启动起来的一个实例。

仓库

仓库是用来存放镜像的地方。

docker 镜像相关命令

常用镜像仓库

官方仓库:hub.docker.com
自己的私有仓库:Harbor
阿里云私有仓库:registry.cn-hangzhou.aliyuncs.com

搜索镜像

#格式
	docker search [镜像名称]
# 实例

拉取镜像

# 格式
	docker pull [镜像名称]
# 实例
[root@Centos7 ~]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
# 镜像层
a076a628af6f: Already exists 
f40dd07fe7be: Pull complete 
ce21c8a3dbee: Pull complete 
ee99c35818f8: Pull complete 
56b9a72e68ff: Pull complete 
3f703e7f380f: Pull complete 
# 镜像ID号(镜像ID号是全球唯一)
Digest: sha256:0f97c1c9daf5b69b93390ccbe8d3e2971617ec4801fd0882c72bf7cad3a13494
# 镜像下载状态
Status: Downloaded newer image for redis:latest 
# 镜像的全称(镜像的tag)
docker.io/library/redis:latest

查看当前系统上的有哪些镜像

# 格式
	docker images 或者 docker image ls
# 参数
-q : 只显示镜像ID
[root@Centos7 ~]# docker images -q
621ceef7494a
f6d0b4767a6c

获取镜像的详细信息

# 格式
	docker inspect [镜像名称或镜像ID]
# 参数
-f : 格式化输出
[root@Centos7 ~]# docker inspect -f '{{.Id}}' 621ceef7494a
sha256:621ceef7494adfcbe0e523593639f6625795cc0dc91a750629367a8c7b3ccebb
[root@Centos7 ~]# docker inspect -f '{{.ContainerConfig.Hostname}}' redis
16535cfaf84a

登录镜像仓库

# 格式
	docker login 
	注: 默认情况下,docker login登录的是官方仓库,如果登录其他镜像仓库则需要指定镜像仓库的URL连接。
# 实例
	[root@Centos7 ~]# docker login registry.cn-hangzhou.aliyuncs.com
        Username: yangyang091022
        Password: 
        WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
        Configure a credential helper to remove this warning. See
        https://docs.docker.com/engine/reference/commandline/login/#credentials-store
        Login Succeeded
	[root@Centos7 ~]# cat ~/.docker/config.json 
    {
        "auths": {
            "registry.cn-hangzhou.aliyuncs.com": {
                "auth": "eWFuZ3lhbmcwOTEwMjI6Y2hlbjE4NzkwMDcwODMw"
            }
        }
    }
# 参数
--username|-u : 指定用户名
--password|-p : 指定密码

为镜像标签

# 镜像标签的构成
docker.io/library/redis:latest
docker.io  : 镜像仓库的URL
library    :镜像仓库命名空间
redis	   : 镜像名称
latest	   : 镜像版本号

# 打标签
	# 格式
		docker tag [镜像ID]  镜像标签
[root@Centos7 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
redis        latest    621ceef7494a   2 months ago   104MB
nginx        latest    f6d0b4767a6c   2 months ago   133MB
[root@Centos7 ~]# docker tag 621ceef7494a registry.cn-hangzhou.aliyuncs.com/alvinos/redis:v2
[root@Centos7 ~]# docker images
REPOSITORY                                        TAG       IMAGE ID       CREATED        SIZE
redis                                             latest    621ceef7494a   2 months ago   104MB
registry.cn-hangzhou.aliyuncs.com/alvinos/redis   v2        621ceef7494a   2 months ago   104MB
nginx                                             latest    f6d0b4767a6c   2 months ago   133MB

镜像上传

# 格式
	docker push [镜像标签]
# 注:要想上传镜像,首先得登录镜像仓库,其次设置对应镜像仓库的tag
[root@Centos7 ~]# docker push registry.cn-hangzhou.aliyuncs.com/alvinos/redis:v2
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/alvinos/redis]
3480f9cdd491: Pushed 
a24a292d0184: Pushed 
f927192cc30c: Pushed 
1450b8f0019c: Pushed 
8e14cb7841fa: Pushed 
cb42413394c4: Pushed 
v2: digest: sha256:7ef832c720188ac7898dbd8d1e237b0738e94f94fc7e981cb7b8efe84555e892 size: 1572

镜像的删除

# 格式
	docker rmi [镜像名称或者镜像ID]
# 实例
	[root@Centos7 ~]# docker rmi nginx
# 参数
	-f  : 强制删除
	[root@Centos7 ~]# docker rmi -f nginx
    Untagged: nginx:latest
    Untagged: nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
 # 注:当有容器正在使用镜像时,强制删除镜像,只能删除镜像的所有tag, 不会删除镜像。

清空镜像

# 格式
	docker image prune
# 实例
	[root@Centos7 ~]# docker image prune
    WARNING! This will remove all dangling images.
    Are you sure you want to continue? [y/N] y
    Total reclaimed space: 0B
 # 参数
 -a : 删除所有镜像
 [root@Centos7 ~]# docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: redis:latest
untagged: redis@sha256:0f97c1c9daf5b69b93390ccbe8d3e2971617ec4801fd0882c72bf7cad3a13494
untagged: registry.cn-hangzhou.aliyuncs.com/alvinos/redis:v2
untagged: registry.cn-hangzhou.aliyuncs.com/alvinos/redis@sha256:7ef832c720188ac7898dbd8d1e237b0738e94f94fc7e981cb7b8efe84555e892
deleted: sha256:621ceef7494adfcbe0e523593639f6625795cc0dc91a750629367a8c7b3ccebb
deleted: sha256:de66cfbf4712b8ba9ef292e08ef7487be26d9d21b350548e400ae351405d820e
deleted: sha256:79b2381e35429e8fc04d31b3445f069c22d288bf5c4cba7b7c10004ff78ae201
deleted: sha256:1d047d19be363b00139990d4d7f392dabdb0809dbc9d0fbe67c1f15b8caed27a
deleted: sha256:8c41f4e708c37059df28ae1cabc200a6db2fee45bd3a2cadcf70f2765bb68730
deleted: sha256:b51317bef36fe1900be48402c8a41fcd9cdb6b8950c10209f764473cb8323371
Total reclaimed space: 35.04MB
[root@Centos7 ~]# 

查看镜像历史(镜像的构建历史)

# 格式
	docker history [镜像ID或镜像名称]
# 实例
[root@Centos7 ~]# docker history alpine
IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
7731472c3f2a   2 months ago   /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B        
<missing>      2 months ago   /bin/sh -c #(nop) ADD file:edbe213ae0c825a5b…   5.61MB    

保存镜像(commit)

# 保存正在运行的容器直接为镜像
# 格式:
	docker commit [容器ID|容器名称]
	
# 实例
[root@Centos7 ~]# docker commit -a "Alvin" -m "这是一个docker镜像" -p be3b92e2886b  test:v1
sha256:4a06cd2af42877b5e2908073061f7ae1bf9e308a470bdfc0c6f906ef368aaed8
[root@Centos7 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
test         v1        4a06cd2af428   5 seconds ago   104MB

保存镜像(import/export)

# 保存正在运行的容器为镜像压缩包
## 保存容器为镜像
	docker export [容器的ID] > [包名称]
	# 实例
		[root@Centos7 ~]# docker export be3b92e2886b > redis.tar
        [root@Centos7 ~]# ll | grep redis
        -rw-r--r--. 1 root root 104178688 Mar 18 17:30 redis.tar
        
## docker import [包名称] [自定义镜像名称]
	# 实例
	[root@Centos7 ~]# docker import redis.tar test:v3
    sha256:7776db3402fb8d59f6121a3b1977b5e7016f4064cf59218fd1b06637cb0fca87
    [root@Centos7 ~]# docker images
    REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
    test         v3        7776db3402fb   6 seconds ago   101MB

保存镜像(save/load)

# 保存镜像为压缩包
# 保存镜像的格式:
	docker save [镜像名称|镜像ID] > [包名称]
    [root@Centos7 ~]# docker save 7731472c3f2a > apline.tar
    [root@Centos7 ~]# ll	
    -rw-r--r--. 1 root root   5888000 Mar 18 17:36 apline.tar
    [root@Centos7 ~]# docker save -o apline-two.tar 7731472c3f2a
    [root@Centos7 ~]# ll
    total 148692
    -rw-r--r--. 1 root root   5888000 Mar 18 17:36 apline.tar
    -rw-------. 1 root root   5888000 Mar 18 17:37 apline-two.tar
# 导入镜像的格式:
	docker load < [包名称]
	[root@Centos7 ~]# docker load < apline.tar 
    c04d1437198b: Loading layer [========================================>]   5.88MB/5.88MB
    Loaded image ID: sha256:7731472c3f2a25edbb9c085c78f42ec71259f2b83485aa60648276d408865839
    [root@Centos7 ~]# docker images
    REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
    <none>       <none>    7731472c3f2a   2 months ago     5.61MB
# 注:save/load保存镜像无法自定义镜像名称,save保存镜像时如果使用ID保存则load导入镜像无名称,使用名称导入时才有名称。
[root@Centos7 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
busybox      latest    b97242f89c8a   2 months ago     1.23MB
[root@Centos7 ~]# docker save busybox:latest > busybox.tar
[root@Centos7 ~]# ll
total 150120
-rw-r--r--. 1 root root   1459200 Mar 18 17:43 busybox.tar
[root@Centos7 ~]# docker rmi b97242f89c8a
Untagged: busybox:latest
Untagged: busybox@sha256:c5439d7db88ab5423999530349d327b04279ad3161d7596d2126dfb5b02bfd1f
Deleted: sha256:b97242f89c8a29d13aea12843a08441a4bbfc33528f55b60366c1d8f6923d0d4
Deleted: sha256:0064d0478d0060343cb2888ff3e91e718f0bffe9994162e8a4b310adb2a5ff74
[root@Centos7 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
[root@Centos7 ~]# docker load < busybox.tar 
0064d0478d00: Loading layer [==================================================>]   1.45MB/1.45MB
Loaded image: busybox:latest
[root@Centos7 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
busybox      latest    b97242f89c8a   2 months ago     1.23MB

保存镜像三种方式的区别

1、export保存的镜像体积要小于save(save保存更完全,export保存会丢掉一些不必要的数据)

2、export可以重命名镜像名称而save则不行

3、save可以同时保存多个镜像而export则不行

加载全部内容

相关教程
猜你喜欢
用户评论