Skip to content
KARINKARIN
博客主页
项目总览
知识要点
捣鼓折腾
Git笔记
Excel修炼秘籍
github icon
  • 博客主页
    • 项目总览
        • 虚拟化的发展
          • 初识 Docker 与容器
            • 核心概念与安装配置
              • 使用 Docker 镜像
                • 操作 Docker 容器
                  • 访问 Docker 仓库
                    • Docker 数据管理
                      • 端口映射与容器互联
                        • 使用 Dockerfile 创建镜像
                          • 基本结构
                            • 指令说明

                          使用 Dockerfile 创建镜像

                          author iconkarincalendar icon2021年8月23日category icon
                          • 笔记
                          tag icon
                          • docker
                          timer icon大约 3 分钟

                          此页内容
                          • 基本结构
                          • 指令说明

                          Dockerfile 是一个文本格式的配置文件,用户可以使用 Dockerfile 来快速创建自定义的镜像。

                          下面首先将介绍 Dockerfile 典型的基本结构及其支持的众多指令,并具体讲解通过这些指令来编写定制镜像的 Dockerfile,以及如何生成镜像。最后,会介绍使用 Dockerfile 的一些最佳实践经验。

                          # 基本结构

                          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/app/sources.list
                          RUN apt-get update && apt-get install -y nginx
                          RUN echo "\ndaernon off;" >> /etc/nginx/nginx.conf
                          
                          # Commands when creating a new container
                          CMD /usr/sbin/nginx
                          
                          1
                          2
                          3
                          4
                          5
                          6
                          7
                          8
                          9
                          10
                          11
                          12
                          13
                          14
                          15
                          16
                          17
                          18
                          19

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

                          下面是 Docker Hub 上两个热门镜像 nginx 和 Go 的 Dockerfile 的例子,通过这两个例子。你可以对 Dockerfile 结构有个基本的感知。

                          第一个是在 debian:jessie 基础镜像基础上安装 Nginx 环境,从而创建一个新的 nginx 镜像:

                          FROM debian:jessie
                          LABEL maintainer docker_user<docker_user@email.com>
                          ENV NGINX_VERSION 1.10.1-1-jessie
                          RUN apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3DBFBC641079A6ABABF5BDB27BD9BF62 \
                          		&& echo "deb http://nginx.org/packages/debian/ jessie nginx" >> /etc/apt/sources.list \
                          		&& apt-get update \
                          		&& apt-get install --no-install-recommends --no-install-suggests -y \
                          		ca-certificates \
                          		nginx=${NGINX_VERSION} \
                          		nginx-module-xslt \
                          		nginx-module-geoip \
                          		nginx-module-image-filter \
                          		nginx-module-perl \
                          		nginx-module-njs \
                          		gettext-base \
                          		&& rm -rf /var/lib/apt/lists/*
                          ----------------------------
                          # forward request and error logs to docker log collector
                          RUN ln -sf /dev/stdout /var/log/nginx/access.log \
                          		&& ln -sf /dev/stderr /var/log/nginx/error.log
                          
                          EXPOSE 80 443
                          
                          CMD ["nginx","-g","daemon off;"]
                          
                          1
                          2
                          3
                          4
                          5
                          6
                          7
                          8
                          9
                          10
                          11
                          12
                          13
                          14
                          15
                          16
                          17
                          18
                          19
                          20
                          21
                          22
                          23
                          24

                          第二个是基于 buildpack-deps:jessie-scm 基础镜像,安装 Golang 相关环境,制作一个 Go 语言的运行环境镜像:

                          FROM buildpack-deps:jessie-scm
                          
                          #gcc for cgo
                          
                          RUN apt-get update && apt-get install -y --no-install-recommends \
                          		g++ \
                          		gcc \
                          		libe6-dev \
                          		make \
                          		&& rm -rf /var/lib/apt/lists/*
                          		
                          ENV GOLANG VERSION 1.6.3
                          ENV GOLANG_DOWNLOAD_URL https://golang.org/dl/go$GOLANG_VERSION.linux-amd64.tar.gz
                          ENV GOLANG_DOWNLOAD_SHA256 cdde5e08530c0579255d6153b08fdb3b8e47caabbe717bc7bcd7561275a87aeb
                          
                          RUN curl -fsSL "$GOLANG_DOWNLOAD_URL" -o golang.tar.gz \
                          		&& echo "$GOLANG_DOWNLOAD_SHA256 golang.tar.gz" | sha256sum -c - \
                          		&& tar -C /usr/local -xzf golang.tar.gz \
                          		&& rm golang.tar.gz
                          		
                          ENV GOPATH /go
                          ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH
                          
                          RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"
                          WORKDIR $GOPATH
                          
                          COPY go-wrapper /usr/local/bin/
                          
                          1
                          2
                          3
                          4
                          5
                          6
                          7
                          8
                          9
                          10
                          11
                          12
                          13
                          14
                          15
                          16
                          17
                          18
                          19
                          20
                          21
                          22
                          23
                          24
                          25
                          26
                          27

                          下面,将讲解 Dockerfile 中各种指令的应用。

                          # 指令说明

                          Dockerfile 中指令的一般格式为 INSTRUCTION arguments,包括“配置指令”(配置镜像信息)和“操作指令“(具体执行操作),参见表 8-1。

                          表 8-1 Dockerfile 中的指令及说明

                          分类指令说明
                          edit icon编辑此页open in new window
                          上一页
                          端口映射与容器互联
                          MIT Licensed
                          Copyright © 2022 karin