概念
父镜像 (parent image)
镜像的父镜像是在镜像的 Dockerfile 中的 FROM 指令中指定的镜像。所有后续命令都基于此父镜像。带有 FROM scratch
指令的 Dockerfile 不使用父镜像,并创建基本镜像。
An image’s parent image is the image designated in the FROM directive in the image’s Dockerfile. All subsequent commands are based on this parent image. A Dockerfile with the FROM scratch directive uses no parent image, and creates a base image.
—— docker官方词汇表-parent-image
基础镜像 (base image)
基础镜像的 Dockerfile 中未指定父镜像。它是使用带有 FROM scratch
指令的 Dockerfile 创建的。
A base image has no parent image specified in its Dockerfile. It is created using a Dockerfile with the FROM scratch directive.
—— docker官方词汇表-base-image
————
当使用Dockerfile定制镜像时,通常用FROM
指令指定一个现有的镜像作为父镜像,如FROM centos:7
。
大部分的镜像都有一个父镜像,这使定制镜像变得简单。比如nginx容器使用debian
作为父镜像,tomcat容器使用jdk
作为父镜像。
除了选择现有镜像为父镜像外,还存在一个特殊的镜像,名为 scratch
,它表示一个空白的镜。
FROM scratch
表示从零开始构建一个镜像,那么这个Dockerfile构建出的镜像被称为基础镜像。
大部分的操作系统镜像都是基础镜像,如centos、oracle-linux、debian。
镜像分类
Docker Hub 上有非常多的高质量的官方镜像(docker团队制作的)。
有应用镜像,如 nginx
、redis
、mongo
、mysql
、httpd
、tomcat
等;
有方便运行各种语言代码的编程语言镜像,如 node
、oraclejdk
,openjdk
、python
、ruby
、golang
等。
还有更为基础的操作系统镜像,如 ubuntu
、debian
、centos
、fedora
、alpine
等。
如何选择父镜像
在 Docker Hub 中寻找一个最符合我们最终目标的镜像为基础镜像进行定制,比如要运行一个java程序(jar包),适合用jdk镜像或tomcat镜像作为父镜像,而非使用centos等操作系统镜像。