docker 学习笔记-3

镜像是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/

    获取最新的Redis,则只需要执行docker pull redis,这里我们指定版本号,则我们需要在Tag中找到是否存在对应的镜像。

2.下载镜像 执行命令 docker pull redis:5.0

root@ubuntu:/home/hui# 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/hui# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               5.0                 415381a6cb81        5 days ago          94.9MB

搜索镜像

搜索镜像,命令 docker search <镜像名>

root@ubuntu:/home/hui# 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>

注意:删除镜像时,要先删除所有用到该镜像的容器。

root@ubuntu:/home/hui# 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/hui# 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/hui# docker rmi redis:latest
Untagged: redis:latest
root@ubuntu:/home/hui# 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. 在产品与服务菜单中,选择“容器镜像服务”,便可以找到菜单“镜像加速器”

  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/hui# docker search rabbitmq
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章