Docker
ubuntu20.04 安装docker、docker-compose
docker镜像相关命令
openim
本文档使用 MrDoc 发布
-
+
首页
docker镜像相关命令
https://www.cnblogs.com/caodan01/category/2005170.html ## **docker镜像相关命令** ### **1.常用镜像仓库** #1.官方仓库:hub.docker.com#2.自己的私有仓库:Harbor#3.阿里云私有仓库:registry.cn-hangzhou.aliyuncs.com ### **2).探索镜像** #格式: docker search [镜像ID|名称] 搜索镜像默认在官方镜像仓库中搜索 #实例: [root@localhost ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 11171 [OK] mariadb MariaDB Server is a high performing open sou… 4242 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 832 [OK] NAME : 镜像名称 DESCRIPTION : 镜像描述 STARS : 收藏个数 OFFICIAL : 是否是官方构建镜像 AUTOMATED:是否是自构建镜像 #参数: --limit : 查询出几条 -f : 指定字段 --no-trunc :显示所有的简介 #参数案例 [root@docker ~]# docker search mysql --filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的 NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 10637 [OK] mariadb MariaDB Server is a high performing open sou… 3991 [OK] ### **3).拉取镜像** #格式: docker pull [镜像名称] docker pull [仓库URL]/[仓库命名空间]/[仓库名称]:[版本号] 如:docker pull mysql:5.7 默认的URL:index.docker.io 默认的命名空间:library index.docker.io/library/mysql:5.7 #实例: [root@docker ~]# docker pull redis Using default tag: latest #如果不写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 ### **4).查看当前系统上有哪些镜像** #格式 docker images 或者 docker image ls 解释:查看当前系统中所有的镜像列表 #参数 -q : 只显示镜像ID [root@docker ~]# docker images -q 621ceef7494a f6d0b4767a6c #案例: [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 08b152afcfae 3 days ago 133MB [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 08b152afcfae 3 days ago 133MB REPOSITORY:镜像名称 TAG : 版本号(默认最新版本号为:latest) IMAGE ID :镜像ID CREATED :创建距离现在的时间段 SIZE : 镜像大小 ### **5).查看镜像的详细信息** #格式 docker inspect [镜像名称或镜像ID] docker inspect mysql:5.7 #参数 -f : 格式化输出 [root@docker ~]# docker inspect -f '{{.Id}}' mysql:5.7 #获取某一项信息 sha256:621ceef7494adfcbe0e523593639f6625795cc0dc91a750629367a8c7b3ccebb [root@docker ~]# docker inspect -f '{{.ContainerConfig.Hostname}}' redis 16535cfaf84a#‘.’相当于linux里的根===路径 ### **6).登录镜像仓库** #格式 docker login [镜像的URL] #默认情况下,docker login登录的是官方仓库,如果登录其他镜像仓库则需要指定镜像仓库的URL连接。 #实例#1,先在官网上登录#2,在xshell主机上登录 [root@docker ~]# docker login docker.io 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: caodan999 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 #可以查看dockerhub的密码 [root@docker ~]# cat /root/.docker/config.json { "auths": { "https://index.docker.io/v1/": { "auth": "Y2FvZGFuOTk5OjEzNTU2MTU0N" } } }[root@docker ~]# echo "Y2FvZGFuOTk5OjEzNTU2MTU0N"|base64 -d [root@Centos7 ~]# cat ~/.docker/config.json #保存的登录信息 参数 --username|-u : 指定用户名 --password|-p : 指定密码 ### **7).为镜像添加标签** #镜像标签的构成 docker.io/library/redis:latest docker.io : 镜像仓库的URL library :镜像仓库命名空间 redis : 镜像名称 latest : 镜像版本号 #打标签 #格式 docker tag [镜像ID] 镜像标签 #实例 [root@docker ~]# docker images ls REPOSITORY TAG IMAGE ID CREATED SIZE [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest d4ff818577bc 2 weeks ago 5.6MB hello-world latest d1165f221234 4 months ago 13.3kB alpine 3.10.3 965ea09ff2eb 20 months ago 5.55MB [root@docker ~]# docker tag 965ea09ff2eb docker.io/caodan999/alpine:v3.10.3 [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest d4ff818577bc 2 weeks ago 5.6MB hello-world latest d1165f221234 4 months ago 13.3kB caodan999/alpine v3.10.3 965ea09ff2eb 20 months ago 5.55MB alpine 3.10.3 965ea09ff2eb 20 months ago 5.55MB ### **8).镜像上传(推送镜像)** #格式 docker push [镜像标签] 注:要想上传镜像,首先得登录镜像仓库,其次设置对应镜像仓库的tag #实例 [root@docker ~]# docker push docker.io/caodan999/alpine:v3.10.3 The push refers to repository [docker.io/caodan999/alpine] 77cae8ab23bf: Mounted from library/alpine v3.10.3: digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a size: 528 ##可以改标签再次推送 [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest d4ff818577bc 2 weeks ago 5.6MB hello-world latest d1165f221234 4 months ago 13.3kB caodan999/alpine v3.10.3 965ea09ff2eb 20 months ago 5.55MB alpine 3.10.3 965ea09ff2eb 20 months ago 5.55MB [root@docker ~]# docker tag 965ea09ff2eb caodan999/alpine:latest [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest d4ff818577bc 2 weeks ago 5.6MB hello-world latest d1165f221234 4 months ago 13.3kB alpine 3.10.3 965ea09ff2eb 20 months ago 5.55MB caodan999/alpine latest 965ea09ff2eb 20 months ago 5.55MB caodan999/alpine v3.10.3 965ea09ff2eb 20 months ago 5.55MB [root@docker ~]# docker push docker.io/caodan999/alpine:latest The push refers to repository [docker.io/caodan999/alpine] 77cae8ab23bf: Layer already exists latest: digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a size: 528 ### **9).镜像的删除** #格式 docker rmi [镜像名称或者镜像ID] #实例 [root@Centos7 ~]# docker rmi nginx [root@Centos7 ~]# docker rmi nginx mysql redis #删除多个镜像 [root@Centos7 ~]# docker rmi -f $(docker images -aq) #删除全部的镜像 #参数 -f : 强制删除 [root@Centos7 ~]# docker rmi -f nginx Untagged: nginx:latest Untagged: nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa 注:当有容器正在使用镜像时,强制删除镜像,只能删除镜像的所有tag及名字, 不会删除镜像。 #去我的仓库拉取镜像 [root@docker ~]# docker pull caodan999/alpine ### **10).清空镜像** 将当前系统中所有的没被使用到的镜像删除 #格式 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 : 删除所有未被使用到的镜像 -f : 强制删除 [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:0f97c1c9daf5b69b93390ccbe8d3e2971617ec4... ... ### **11).查看镜像历史(镜像的构建历史)** #格式 docker history [镜像ID或镜像名称] #实例 [root@Centos7 ~]# docker history redis 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 ### **12).构建镜像、保存镜像(commit)** 直接将容器保存为镜像 #格式 docker commit [容器ID|容器名称] docker commit -m="提交的描述信息" -a="作者" [容器ID] 目标镜像名:[TAG] #实例 [root@Centos7 ~]# docker commit -a "Alvin" -m "这是一个docker镜像" -p be3b92e2886b test:v1 sha256:4a06cd2af42877b5e2908073061f7ae1bf9e308a470bdfc0c6f906ef368aaed8 [root@Centos7 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZEtest v1 4a06cd2af428 5 seconds ago 104MB #参数 -a:作者信息 -m:提交信息 -p:提交时,暂停容器运行 #案例 [root@docker ~]# docker run -d --name alpine\_sleep docker.io/mmdghh/alpine:latest /bin/sleep 300s c3d1aa7a1bc2df47f47621f16e420883b334ef8e242349f1f734ad9cb4533968 [root@alice ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c3d1aa7a1bc2 mmdghh/alpine:latest "/bin/sleep 300s" 5 seconds ago Up 4 seconds alpine\_sleep [root@alice ~]# docker exec -it c3d1aa7a1bc2 /bin/sh / # ls bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var / # echo "hello world" >1.txt / # ls 1.txt bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var / # [root@alice ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c3d1aa7a1bc2 mmdghh/alpine:latest "/bin/sleep 300s" 45 seconds ago Up 44 seconds alpine\_sleep [root@alice ~]# docker run -it docker.io/mmdghh/alpine:latest /bin/sh / # ls bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var / # 写入到容器的文件并不会保存在镜像里 -p 保存到执行命令这一时刻的内容 之后更新的不会报错 [root@alice ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c3d1aa7a1bc2 mmdghh/alpine:latest "/bin/sleep 300s" 5 minutes ago Up 4 minutes alpine\_sleep [root@alice ~]# docker commit -p alpine\_sleep docker.io/mmdghh/alpine:v\_1.txt sha256:11be5214792460b1e258a6d6e7dbca5dccfddce0d438c0ad9b04d78b147006e0 [root@alice ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mmdghh/alpine v\_1.txt 11be52147924 8 seconds ago 5.55MB alpine latest 389fef711851 3 weeks ago 5.58MB hello-world latest bf756fb1ae65 12 months ago 13.3kB mmdghh/alpine latest 965ea09ff2eb 14 months ago 5.55MB [root@alice ~]# docker run -it docker.io/mmdghh/alpine:v\_1.txt /bin/sh / # ls 1.txt bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var / # cat 1.txt hello world ### **13).保存镜像(import/export)** 保存正在运行的容器为镜像压缩包 **export: 将容器导出为镜像** #格式 docker export [容器的ID] > [包名称]# 实例 [root@Centos7 ~]# docker export be3b92e2886b > redis.tar #保存镜像(打包) 或者[root@docker ~]# docker export -o redis.tar be3b92e2886b [root@Centos7 ~]# ll | grep redis -rw-r--r--. 1 root root 104178688 Mar 18 17:30 redis.tar [root@Centos7 ~]# gzip redis #给镜像压缩 **导入镜像包(import)** #格式 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 ### **14).将镜像导出为镜像(save/load)** 保存镜像为压缩包 **保存镜像(save)的格式**: docker save [镜像名称|镜像ID] > [包名称] [root@Centos7 ~]# docker save 7731472c3f2a > apline.tar 或者[root@docker ~]# docker save -o apline.tar 7731472c3f2a [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 [root@localhost ~]# docker load < nginx-save.tar #下面是导入镜像的格式 ### **15).**导入镜像(load**的格式:** 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 ### **16).保存镜像三种方式的区别** # 1.export保存的镜像体积要小于save(save保存更完全,export保存会丢掉一些不必要的数据)# 2.export可以重命名镜像名称而save则不行# 3.save可以同时保存多个镜像而export则不行 #4、两种保存镜像的区别 1、dockers export 保存的镜像的体积小于docker save 2、docker save是完全保存,而docker export是仅仅保存镜像 3、docker load不能够自定义镜像名称,docker impprt 可以 4、docker import未保存镜像默认的启动命令,docker load保存了 5、docker save一般情况下,使用的场景是项目迁移,docker export一般情况下是作为基础镜像 ### **17).显示docker的版本信息** [root@docker ~]# docker version Client: Docker Engine - Community Version: 20.10.3 API version: 1.41 Go version: go1.13.15 Git commit: 48d30b5 Built: Fri Jan 29 14:34:14 2021 OS/Arch: linux/amd64 ... ... ... ### **18).显示docker的系统信息(包括镜像和容器的数量)** [root@docker ~]# docker info Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Build with BuildKit (Docker Inc., v0.5.1-docker) ... ... ... ### **19).万能帮助命令** [root@docker ~]# docker --help Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "/root/.docker") -c, --context string Name of the context to use to connect to the daemon (overrides DOCKER\_HOST env var and default context set with "docker context use") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -l, --log-level string Set the logging leve ..........
admin
2024年9月18日 10:43
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码