💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] ## 镜像介绍 镜像是Docker的三大核心概念之一。 Docker运行容器前需要本地存在对应的镜像,如果镜像不存在本地,Docker会尝试先从默认镜像仓库下载(默认使用Docker Hub公共注册服务器中仓库),用户也可以通过配置,使用自定义的镜像仓库。 > ## 获取镜像 命令:`docker pull <registry>/<name>:<tag>` ``` guanfuchang@ubuntu:~$ docker pull --help Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST] Pull an image or a repository from a registry Options: -a, --all-tags Download all tagged images in the repository --disable-content-trust Skip image verification (default true) --platform string Set platform if server is multi-platform capable ``` ### 案例:获取 redis 5.0.0的镜像 1. 先到远程仓库进行搜索 https://hub.docker.com/ ![](https://box.kancloud.cn/6234ba0992ea108e8002ae3197f2afa7_1255x439.png) 获取最新的Redis,则只需要执行`docker pull redis`,这里我们指定版本号,则我们需要在Tag中找到是否存在对应的镜像。 ![](https://box.kancloud.cn/e73557076784651852abd82919d8e016_869x431.png) ![](https://box.kancloud.cn/2c77833a468b1f3f01edc7380cf8fea4_838x61.png) 2.下载镜像 执行命令 `docker pull redis:5.0` ``` root@ubuntu:/home/guanfuchang# docker pull redis:5.0 5.0: Pulling from library/redis f17d81b4b692: Downloading [=======> ] 3.226MB/22.49MB b32474098757: Download complete 8980cabe8bc2: Download complete e614c66c2b9c: Downloading [==========> ] 2.485MB/11.76MB 6eb43ec9256b: Download complete 394ecf5f46d4: Download complete ``` ## 查询本地镜像 查询本地镜像,命令`docker images` ``` root@ubuntu:/home/guanfuchang# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis 5.0 415381a6cb81 5 days ago 94.9MB ``` ## 搜索镜像 搜索镜像,命令 `docker search <镜像名>` ``` root@ubuntu:/home/guanfuchang# docker search redis NAME DESCRIPTION STARS OFFICIAL AUTOMATED redis Redis is an open source key-value store that… 6062 [OK] bitnami/redis Bitnami Redis Docker Image 94 [OK] google/guestbook-python-redis A simple guestbook example written in Python… 1 tiredofit/redis Redis Server w/ Zabbix monitoring and S6 Ove… 1 [OK] ``` ## 删除镜像 删除镜像命令 `docker rmi <镜像ID>` >[warning]注意:删除镜像时,要先删除所有用到该镜像的容器。 ``` root@ubuntu:/home/guanfuchang# docker rmi --help Usage: docker rmi [OPTIONS] IMAGE [IMAGE...] Remove one or more images Options: -f, --force Force removal of the image --no-prune Do not delete untagged parents ``` ### 案例:删除redis:latest镜像 ``` root@ubuntu:/home/guanfuchang# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis 5.0 415381a6cb81 5 days ago 94.9MB redis latest 415381a6cb81 5 days ago 94.9MB root@ubuntu:/home/guanfuchang# root@ubuntu:/home/guanfuchang# docker rmi redis:latest Untagged: redis:latest root@ubuntu:/home/guanfuchang# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis 5.0 415381a6cb81 5 days ago 94.9MB ``` ## 镜像加速 通过上面的实操例子,我们有可能会发现下载镜像的速度非常慢,那是因为Docker默认直接到Docker Hub中下载,Docker Hub是国外的网站,访问自然会慢一些,甚至会出现下载失败。在国内,阿里云,163都提供了docker仓库,并且阿里云还提供了加速功能,因此,我们可以通过设置使用阿里云仓库,便可以得到加速的效果。 配置阿里云镜像加速步骤: 1. 注册阿里云,进入控制台 https://www.aliyun.com 2. 在产品与服务菜单中,选择“容器镜像服务”,便可以找到菜单“镜像加速器” ![](https://box.kancloud.cn/8096e22752b8bfe1e3a99c1f80fc1121_501x513.png) ![](https://box.kancloud.cn/c0add3d1b5acc3263286da8217ac4678_690x719.png) 3. 根据文档说明,镜像加速器配置 ``` sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://6z3kxtoq.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker ``` 配置后镜像加速器后,不妨可以测试下载一个rabbitmq试一下,这个时候会发现下载速度会非常快。 ``` root@ubuntu:/home/guanfuchang# docker search rabbitmq ``` --- :-: ![](https://box.kancloud.cn/331f659e8e6cddb0d9f182e00e32803f_258x258.jpg) <span style="color: #993366;"><em>***<span style="text-decoration: underline;"><span style="text-decoration: underline;">微信扫一扫,关注&ldquo;python测试开发圈&rdquo;,了解更多测试教程!!</span></span>***</em></span>