一起来学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⭐️)

博客(阅读体验较佳)

相关推荐
码事漫谈40 分钟前
以后人人都可以成为导演
后端
Resean02231 小时前
Spring 源码分析(二)配置类的解析与扫描
java·后端·spring
一只爱撸猫的程序猿1 小时前
五周、两个开源仓库、九次拦截之后:AI 时代程序员缺的到底是什么
人工智能·后端·开源
北城笑笑2 小时前
Vue 110 ,若依SSE流式接口限次空500排查与解决
前端·javascript·vue.js·后端·vue
ServBay3 小时前
Local harness 将退役,VS Code 本地开发要不要搬到云端?
后端·ai编程·visual studio code
ashiho3 小时前
【LeetCode hot 100】3. 无重复字符的最长子串 (三种解法)
java·数据结构·后端·算法·leetcode·滑动窗口
张一凡934 小时前
你的 AGENTS.md,真的管到模型写代码了吗?
前端·后端·agent
00后程序员4 小时前
iOS 实时活动怎么开发,Windows 上用 KXBuild 和 ActivityKit 写灵动岛
后端·ios
程序员cxuan4 小时前
推荐 10 个 WorkBuddy 中的邪修用法
人工智能·后端·程序员
Bacon5 小时前
给项目做一次"全身 CT":Agent Skills 生态里的代码体检全链路
前端·人工智能·后端