一起来学Docker(二)

前言

目前正在出一个Docker系列教程, 篇幅会较多, 喜欢的话,给个关注❤️ ~

Docker大家应该都听说过,特别是在当今云原生爆火的时代,更值得我们去学习,下面会带大家系统性的认识一下Docker,并结合一些例子,让大家快速上手~

好了, 废话不多说直接开整吧~

配置国内镜像源

我们使用Docker构建应用的时候往往会有一些镜像依赖,配置国内的镜像源开发体验会好一点,可以大大缩减镜像的构建时间,那如何配置呢?

  • linux环境下找到/etc/docker/daemon.json,添加如下配置
json 复制代码
{
    "registry-mirrors": [
        "http://hub-mirror.c.163.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://registry.docker-cn.com"
    ]
}

接着重启docker

shell 复制代码
service docker restart
  • Mac或者Win的小伙伴可以直接到客户端的软件中点击设置项,也是如上的配置,修改完成点击保存,它会自动进行重启

Docker常用命令

以下的命令都可以通过docker -h查找到,所以大家也可以试着看看有哪些命令

登录远程仓库

这个命令也是比较常用的,有时候我们需要从私有仓库拉取构建好的镜像

shell 复制代码
docker login -u username -p passwd registry

搜索镜像

比如我们搜索nginx的镜像

shell 复制代码
docker search nginx

输出:

shell 复制代码
[root@iZ2ze5vrnucj8nu52fq932Z ~]# docker search nginx
INDEX       NAME                                                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/nginx                                             Official build of Nginx.                        19105     [OK]       
docker.io   docker.io/bitnami/nginx                                     Bitnami nginx Docker Image                      176                  [OK]
docker.io   docker.io/nginxinc/nginx-unprivileged                       Unprivileged NGINX Dockerfiles                  125                  
docker.io   docker.io/nginxproxy/acme-companion                         Automated ACME SSL certificate generation ...   125                  
docker.io   docker.io/nginxproxy/nginx-proxy                            Automated Nginx reverse proxy for docker c...   110                  
docker.io   docker.io/ubuntu/nginx                                      Nginx, a high-performance reverse proxy & ...   100                  
docker.io   docker.io/nginx/nginx-ingress                               NGINX and  NGINX Plus Ingress Controllers ...   81                   
docker.io   docker.io/nginx/unit                                        NGINX Unit is a dynamic web and applicatio...   64                   
docker.io   docker.io/nginx/nginx-prometheus-exporter                   NGINX Prometheus Exporter for NGINX and NG...   33                   
docker.io   docker.io/bitnami/nginx-ingress-controller                  Bitnami Docker Image for NGINX Ingress Con...   30                   [OK]
docker.io   docker.io/unit                                              Official build of NGINX Unit: Universal We...   15        [OK]       
docker.io   docker.io/nginxproxy/docker-gen                             Generate files from docker container meta-...   12                   
docker.io   docker.io/rancher/nginx-ingress-controller                                                                  11                   
docker.io   docker.io/kasmweb/nginx                                     An Nginx image based off nginx:alpine and ...   6                    
docker.io   docker.io/nginxinc/ingress-demo                             Ingress Demo                                    4                    
docker.io   docker.io/nginxinc/nginx-s3-gateway                         Authenticating and caching gateway based o...   2                    
docker.io   docker.io/rancher/nginx-ingress-controller-defaultbackend                                                   2                    
docker.io   docker.io/nginx/nginx-ingress-operator                      NGINX Ingress Operator for NGINX and NGINX...   1                    
docker.io   docker.io/nginx/nginx-quic-qns                              NGINX QUIC interop                              1                    
docker.io   docker.io/nginxinc/amplify-agent                            NGINX Amplify Agent docker repository           1                    
docker.io   docker.io/nginxinc/mra-fakes3                                                                               0                    
docker.io   docker.io/nginxinc/mra_python_base                                                                          0                    
docker.io   docker.io/nginxinc/nginmesh_proxy_debug                                                                     0                    
docker.io   docker.io/nginxinc/nginmesh_proxy_init                                                                      0                    
docker.io   docker.io/nginxinc/ngx-rust-tool                                                                            0                    
[root@iZ2ze5vrnucj8nu52fq932Z ~]# 

它还有一些可选参数,下面给大家举几个例子:

  • -f过滤条件

搜索nginx镜像并查找stars数大于10000

shell 复制代码
docker search nginx -f stars=10000
  • --limit限制数量
shell 复制代码
[root@iZ2ze5vrnucj8nu52fq932Z ~]# docker search nginx --limit 5
INDEX       NAME                                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/nginx                             Official build of Nginx.                        19105     [OK]       
docker.io   docker.io/nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  125                  
docker.io   docker.io/nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers ...   81                   
docker.io   docker.io/nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NG...   33                   
docker.io   docker.io/unit                              Official build of NGINX Unit: Universal We...   15        [OK]       
[root@iZ2ze5vrnucj8nu52fq932Z ~]# 

推送到远程

可以把本地仓库中的镜像推送到远程仓库,如果拉取的是私有镜像需要先登录远程仓库,如果未登录会默认去公有云仓库拉取,也就是我们前面配置的国内镜像源,如果找不到最终会去docker hub

语法:

shell 复制代码
docker push [OPTIONS] NAME[:TAG]
  • OPTIONS:可选参数
  • NAME:镜像名称
  • TAG:镜像版本号,可省略,默认为latest,即最新版本

比如:

shell 复制代码
docker push my-image:1.1.0

查看本地仓库的镜像可以通过docker images进行查看,上边的NAME其实就对标本地仓库的REPOSITORY,我们也可以使用IMAGE_ID

拉取或更新镜像

有时候需要部署最新的应用,就要从仓库拉拉取最新的镜像版本

语法:

shell 复制代码
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
  • DIGEST:镜像的摘要,每个镜像都有对应的名称、id、摘要信息,每个摘要信息能唯一代表一个镜像

比如拉取nginx镜像

shell 复制代码
docker pull nginx

不指定TAG默认会拉取最新的:latest

  • -a 拉取所有镜像
shell 复制代码
docker pull -a nginx

显示本地存在的所有镜像

语法:

shell 复制代码
docker images [OPTIONS] [REPOSITORY[:TAG]]
  • -a: 显示所有镜像,包含中间映像(默认情况下中间映像是隐藏的)
shell 复制代码
[root@iZ2ze5vrnucj8nu52fq932Z ~]# docker images -a
REPOSITORY                                          TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world                               latest              feb5d9fea6a5        2 years ago         13.3 kB
  • -q: 只显示镜像id
shell 复制代码
[root@iZ2ze5vrnucj8nu52fq932Z ~]# docker images -q
feb5d9fea6a5
[root@iZ2ze5vrnucj8nu52fq932Z ~]# 
  • --no-trunc 显示完整的镜像ID
shell 复制代码
[root@iZ2ze5vrnucj8nu52fq932Z ~]# docker images --no-trunc
REPOSITORY                                          TAG                 IMAGE ID                                                                  CREATED             SIZE
docker.io/hello-world                               latest              sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412   2 years ago         13.3 kB
[root@iZ2ze5vrnucj8nu52fq932Z ~]# 
  • -f筛选条件

找出在nginx:1.21.5这个镜像之前构建的镜像,就是昨天我们新拉的那个hello-world镜像,这个就很方便找出近期构建的镜像

shell 复制代码
[root@iZ2ze5vrnucj8nu52fq932Z ~]# docker images -f before=nginx:1.21.5
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              feb5d9fea6a5        2 years ago         13.3 kB
[root@iZ2ze5vrnucj8nu52fq932Z ~]# 

与之相反的是since,下面的例子找出hello-world镜像之后构建的镜像

shell 复制代码
[root@iZ2ze5vrnucj8nu52fq932Z ~]# docker images -f since=hello-world
REPOSITORY                                          TAG                 IMAGE ID            CREATED             SIZE
docker.io/nginx                                     1.21.5              605c77e624dd        21 months ago       141 MB
[root@iZ2ze5vrnucj8nu52fq932Z ~]# 

当然我们也可以结合linux中的grep命令找出具体的镜像

shell 复制代码
[root@iZ2ze5vrnucj8nu52fq932Z ~]# docker images | grep nginx
docker.io/nginx                                     1.21.5              605c77e624dd        21 months ago       141 MB
[root@iZ2ze5vrnucj8nu52fq932Z ~]# 
  • --digests显示详细的DIGEST
shell 复制代码
[root@iZ2ze5vrnucj8nu52fq932Z ~]# docker images --digests
REPOSITORY                                          TAG                 DIGEST                                                                    IMAGE ID            CREATED             SIZE
docker.io/hello-world                               latest              sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f   feb5d9fea6a5        2 years ago         13.3 kB
[root@iZ2ze5vrnucj8nu52fq932Z ~]# 

删除镜像

删除本地镜像的语法:

shell 复制代码
docker rmi [OPTIONS] IMAGE [IMAGE...]
  • OPTIONS:可选参数
  • IMAGE:镜像id或仓库路径名称
  • OPTIONS的常用值
  • -f: 强制删除,如果镜像有对应的容器正在运行,则不允许直接删除镜像,需要强制删除
  • ---no-prune:不删除该镜像的过程镜像,默认是删除的

这里要注意的是-f要慎用,因为正常删除的时候如果有依赖的容器或者镜像在使用会给我们报错提示无法删除,如果强删除可能会导致一些未知的错误

shell 复制代码
[root@iZ2ze5vrnucj8nu52fq932Z ~]# docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 3f2d1dd782f1 is using its referenced image feb5d9fea6a5
[root@iZ2ze5vrnucj8nu52fq932Z ~]# 

可以看到报了一个错误,容器3f2d1dd782f1正在使用该镜像,无法删除

删除nginx镜像

shell 复制代码
[root@iZ2ze5vrnucj8nu52fq932Z ~]# docker rmi nginx
Untagged: nginx:latest
Untagged: docker.io/nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
[root@iZ2ze5vrnucj8nu52fq932Z ~]# 

这个就可以正常删除了,因为我们并没有使用它运行容器,容器后边会给大家想详细讲解,大家在学习的时候一定要分清楚镜像容器这两个概念

结束语

本节到这里就结束了,docker命令很多,大家不要去背,如果忘了可以使用docker -h进行查看,下节接着给大家讲解它的命令~

本着把自己知道的都告诉大家,如果本文对有所帮助,点赞+关注鼓励一下呗~

Docker教程相关文章

往前Shell脚本编程相关文章

往期Linux相关文章

往期面试题相关文章

项目源码(源码已更新 欢迎star⭐️)

往期设计模式相关文章

设计模式项目源码(源码已更新 欢迎star⭐️)

Kafka 专题学习

项目源码(源码已更新 欢迎star⭐️)

ElasticSearch 专题学习

项目源码(源码已更新 欢迎star⭐️)

往期并发编程内容推荐

推荐 SpringBoot & SpringCloud (源码已更新 欢迎star⭐️)

博客(阅读体验较佳)

相关推荐
2401_857622662 小时前
SpringBoot框架下校园资料库的构建与优化
spring boot·后端·php
2402_857589362 小时前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
哎呦没4 小时前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
_.Switch4 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
雪域迷影4 小时前
PostgreSQL Docker Error – 5432: 地址已被占用
数据库·docker·postgresql
莹雨潇潇5 小时前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器
杨哥带你写代码5 小时前
足球青训俱乐部管理:Spring Boot技术驱动
java·spring boot·后端
AskHarries6 小时前
读《show your work》的一点感悟
后端
A尘埃6 小时前
SpringBoot的数据访问
java·spring boot·后端
yang-23076 小时前
端口冲突的解决方案以及SpringBoot自动检测可用端口demo
java·spring boot·后端