鏡像是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的鏡像
- 先到遠程倉庫進行搜索 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倉庫,並且阿里雲還提供了加速功能,因此,我們可以通過設置使用阿里雲倉庫,便可以得到加速的效果。
配置阿里雲鏡像加速步驟:
- 註冊阿里雲,進入控制檯 https://www.aliyun.com
- 在產品與服務菜單中,選擇“容器鏡像服務”,便可以找到菜單“鏡像加速器”
- 根據文檔說明,鏡像加速器配置
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