前言
镜像(image)是 docker 中的三大核心要素之一,是我们必须要掌握的内容.
先来瞄一眼跟镜像相关的命令帮助:
docker image --help
# 通常会输出
Usage: docker image COMMAND
Manage images
Options:
--help Print usage
Commands:
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Run 'docker image COMMAND --help' for more information on a command.
Docker 镜像命令实践
搜索镜像
类比:我们想玩一个游戏,我们需要先去搜索下
# 搜索远程仓库中的 nginx 镜像
docker search nginx
# 通常会输出
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 6217 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 1051 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 388 [OK]
webdevops/php-nginx Nginx with PHP-FPM 80 [OK]
million12/nginx-php Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS... 77 [OK]
h3nrik/nginx-ldap NGINX web server with LDAP/AD, SSL and pro... 38 [OK]
bitnami/nginx Bitnami nginx Docker Image 30 [OK]
evild/alpine-nginx Minimalistic Docker image with Nginx 16 [OK]
funkygibbon/nginx-pagespeed nginx + ngx_pagespeed + openssl on docker-... 11 [OK]
webdevops/nginx Nginx container 7 [OK]
webdevops/php-nginx-dev PHP with Nginx for Development (eg. with x... 6 [OK]
# ...
# Tips:这里需要关注的是*OFFICIAL*,代表该镜像是官方的.
拉取镜像
类比:我们搜索到想要的游戏,然后需要把游戏的安装包下载过来
# 拉取名为 nginx 的镜像到本地
docker pull nginx
# 通常会输出
查看镜像
类比:游戏的安装包下载完成/要安装游戏时,我们可以看下本机已经有哪些游戏的安装包.
# 拉取完成/要使用镜像时,我们可以看下本机已经有哪些镜像
docker image list
# 通常会输出
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 958a7ae9e569 2 weeks ago 109MB
# ...
# Tips:这里的镜像名称和镜像ID是我们通常要关注的,因为经常会需要使用它们.
如果需要查看镜像的更多信息,可以试试docker inspect
命令:
docker inspect imageId/imageName
# 通常会输出
删除镜像
类比:我们不需要某个游戏安装包了,我们可以把它删除,以节省磁盘空间,看的也清爽点
# 移除一个镜像
docker image remove imageId/imageName
# 移除多个镜像
# 这里删除2个镜像
docker image remove image1Id/image1Name image2Id/image2Name