8.1 基本结构

Dockerfile由一行行命令语句组成,并且支持以#开头的注释行。

一般而言,Dockerfile主体内容分为四部分:

  • 基础镜像信息

  • 维护者信息

  • 镜像操作指令

  • 容器启动时执行指令

下面给出一个简单的示例:

# escape=\ (backslash)
# This dockerfile uses the ubuntu:xeniel image
# VERSION 2- EDITION 1
# Author: docker_user
# Command format: Instruction [arguments / command] ..

# Base image to use, this must be set as the first line
FROM ubuntu:xeniel

# Maintainer: docker_user <docker_user at email.com> (@docker_user)
LABEL maintainer docker_user<docker_user@email.com>

# Commands to update the image
RUN echo "deb http://archive.ubuntu.com/ubuntu/ xeniel main universe" >> /etc/
    apt/sources.list
RUN apt-get update && apt-get install -y nginx
RUN echo "\ndaemon off; " >> /etc/nginx/nginx.conf

# Commands when creating a new container
CMD /usr/sbin/nginx

首行可以通过注释来指定解析器命令,后续通过注释说明镜像的相关信息。主体部分首先使用FROM指令指明所基于的镜像名称,接下来一般是使用LABEL指令说明维护者信息。后面则是镜像操作指令,例如RUN指令将对镜像执行跟随的命令。每运行一条RUN指令,镜像添加新的一层,并提交。最后是CMD指令,来指定运行容器时的操作命令。

Last updated