Dockerfile 简版大全,附赠编写实例

云计算
Dockerfiles是由一系列命令和参数构成的脚本,这些命令应用于基础镜像并最终创建一个新的镜像。镜像可以非常基础,仅仅包含操作系统;也可以非常丰富,包含灵巧的应用栈,随时可以发布。当你在使用Docker构建镜像的时候,每一个命令都会在前一个命令的基础上形成一个新层。这些基础镜像可以用于创建新的容器。本篇文章将手把手教您如何从基础镜像,一步一步,一层一层的从Dockerfile构建容器的过程。

Dockerfile 简版大全,附赠编写实例

基础镜像可以用于创建Docker容器。镜像可以非常基础,仅仅包含操作系统;也可以非常丰富,包含灵巧的应用栈,随时可以发布。当你在使用Docker构建镜像的时候,每一个命令都会在前一个命令的基础上形成一个新层。这些基础镜像可以用于创建新的容器。本篇文章将手把手教您如何从基础镜像,一步一步,一层一层的从Dockerfile构建容器的过程。

Docker简介

Docker项目提供了构建在Linux内核功能之上,协同在一起的的高级工具。其目标是帮助开发和运维人员更容易地跨系统跨主机交付应用程序和他们的依赖。Docker通过Docker容器,一个安全的,基于轻量级容器的环境,来实现这个目标。这些容器由镜像创建,而镜像可以通过命令行手工创建或者通过Dockerfile自动创建。

Dockerfiles

Dockerfiles是由一系列命令和参数构成的脚本,这些命令应用于基础镜像并最终创建一个新的镜像。它们简化了从头到尾的流程并极大的简化了部署工作。Dockerfile从FROM命令开始,紧接着跟随者各种方法,命令和参数。其产出为一个新的可以用于创建容器的镜像。

Dockerfile 语法

在我们深入讨论Dockerfile之前,让我们快速过一下Dockerfile的语法和它们的意义。

什么是语法?

非常简单,在编程中,语法意味着一个调用命令,输入参数去让应用执行程序的文法结构。这些语法被规则或明或暗的约束。程序员遵循语法规范以和计算机交互。如果一段程序语法不正确,计算机将无法识别。Dockerfile使用简单的,清楚的和干净的语法结构,极为易于使用。这些语法可以自我释义,支持注释。

Dockerfile 语法示例

Dockerfile语法由两部分构成,注释和命令+参数

 

  1. # Line blocks used for commenting 
  2. command argument argument .. 

 

一个简单的例子:

 

  1. # Print "Hello docker!" 
  2. RUN echo "Hello docker!" 

 

#p#

Dockerfile 命令

Dockerfile有十几条命令可用于构建镜像,下文将简略介绍这些命令。

ADD

ADD命令有两个参数,源和目标。它的基本作用是从源系统的文件系统上复制文件到目标容器的文件系统。如果源是一个URL,那该URL的内容将被下载并复制到容器中。

 

  1. # Usage: ADD [source directory or URL] [destination directory] 
  2. ADD /my_app_folder /my_app_folder 

 

CMD

和RUN命令相似,CMD可以用于执行特定的命令。和RUN不同的是,这些命令不是在镜像构建的过程中执行的,而是在用镜像构建容器后被调用。

 

  1. # Usage 1: CMD application "argument", "argument", .. 
  2. CMD "echo" "Hello docker!" 

 

ENTRYPOINT

ENTRYPOINT 帮助你配置一个容器使之可执行化,如果你结合CMD命令和ENTRYPOINT命令,你可以从CMD命令中移除“application”而仅仅保留参数,参数将传递给ENTRYPOINT命令,

 

  1. # Usage: ENTRYPOINT application "argument", "argument", .. 
  2. # Remember: arguments are optional. They can be provided by CMD 
  3. # or during the creation of a container.  
  4. ENTRYPOINT echo 
  5. # Usage example with CMD: 
  6. # Arguments set with CMD can be overridden during *run* 
  7. CMD "Hello docker!" 
  8. ENTRYPOINT echo

ENV

ENV命令用于设置环境变量。这些变量以”key=value”的形式存在,并可以在容器内被脚本或者程序调用。这个机制给在容器中运行应用带来了极大的便利。

 

  1. # Usage: ENV key value 
  2. ENV SERVER_WORKS 4 

 

EXPOSE

EXPOSE用来指定端口,使容器内的应用可以通过端口和外界交互。

 

  1. # Usage: EXPOSE [port] 
  2. EXPOSE 8080 

 

FROM

FROM命令可能是最重要的Dockerfile命令。改命令定义了使用哪个基础镜像启动构建流程。基础镜像可以为任意镜像。如果基础镜像没有被发现,Docker将试图从Docker image index来查找该镜像。FROM命令必须是Dockerfile的首个命令。

 

  1. # Usage: FROM [image name] 
  2. FROM ubuntu 

 

MAINTAINER

我建议这个命令放在Dockerfile的起始部分,虽然理论上它可以放置于Dockerfile的任意位置。这个命令用于声明作者,并应该放在FROM的后面。

 

  1. # Usage: MAINTAINER [name] 
  2. MAINTAINER authors_name 

 

RUN

RUN命令是Dockerfile执行命令的核心部分。它接受命令作为参数并用于创建镜像。不像CMD命令,RUN命令用于创建镜像(在之前commit的层之上形成新的层)。

 

  1. # Usage: RUN [command] 
  2. RUN aptitude install -y riak 

 

USER

USER命令用于设置运行容器的UID。

 

  1. # Usage: USER [UID] 
  2. USER 751 

 

VOLUME

VOLUME命令用于让你的容器访问宿主机上的目录。

 

  1. # Usage: VOLUME ["/dir_1", "/dir_2" ..] 
  2. VOLUME ["/my_files"

 

WORKDIR

WORKDIR命令用于设置CMD指明的命令的运行目录。

 

  1. # Usage: WORKDIR /path 
  2. WORKDIR ~/ 

 

#p#

如何使用Dockerfiles

使用Dockerfiles和手工使用Docker Daemon运行命令一样简单。脚本运行后输出为新的镜像ID

 

  1. # Build an image using the Dockerfile at current location 
  2. # Example: sudo docker build -t [name] . 
  3. sudo docker build -t my_mongodb . 

 

Dockerfile 示例一:创建一个MongoDB的镜像

在这部分中,我们讲一步一步创建一个Dockfile,这个Dockerfile可用于构建MongoDB镜像进而构建MongoDB容器。

创建一个Dockerfile

使用nano文本编辑器,让我们创建Dockerfile

  1. sudo nano Dockerfile 

 

定义文件和它的目的

让阅读者明确Dockerfile的目的永远是必要的。为此,我们通常从注释开始写Dockerfile。

 

  1. ############################################################ 
  2. # Dockerfile to build MongoDB container images 
  3. # Based on Ubuntu 
  4. ############################################################ 

 

设置基础镜像

 

  1. # Set the base image to Ubuntu 
  2. FROM ubuntu

定义作者

 

  1. # File Author / Maintainer 
  2. MAINTAINER Example McAuthor 

 

设置命令与参数下载MongoDB

 

  1. ################## BEGIN INSTALLATION ###################### 
  2. # Install MongoDB Following the Instructions at MongoDB Docs 
  3. # Ref: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/ 
  4. # Add the package verification key 
  5. RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 
  6. # Add MongoDB to the repository sources list 
  7. RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list 
  8. # Update the repository sources list once more 
  9. RUN apt-get update 
  10. # Install MongoDB package (.deb) 
  11. RUN apt-get install -y mongodb-10gen 
  12. # Create the default data directory 
  13. RUN mkdir -p /data/db 
  14. ##################### INSTALLATION END ##################### 

 

设置MongoDB端口

 

  1. # Expose the default port 
  2. EXPOSE 27017 
  3. # Default port to execute the entrypoint (MongoDB) 
  4. CMD ["--port 27017"
  5. # Set default container command</span> 
  6. ENTRYPOINT usr/bin/mongod 

 

保存Dockerfile,下面的代码是Dockerfile的完整版本

 

  1. ############################################################ 
  2. # Dockerfile to build MongoDB container images 
  3. # Based on Ubuntu 
  4. ############################################################ 
  5. # Set the base image to Ubuntu 
  6. FROM ubuntu 
  7. # File Author / Maintainer 
  8. MAINTAINER Example McAuthor 
  9. # Update the repository sources list 
  10. RUN apt-get update 
  11. ################## BEGIN INSTALLATION ###################### 
  12. # Install MongoDB Following the Instructions at MongoDB Docs 
  13. # Ref: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/ 
  14. # Add the package verification key 
  15. RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 
  16. # Add MongoDB to the repository sources list 
  17. RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list 
  18. # Update the repository sources list once more 
  19. RUN apt-get update 
  20. # Install MongoDB package (.deb) 
  21. RUN apt-get install -y mongodb-10gen 
  22. # Create the default data directory 
  23. RUN mkdir -p /data/db 
  24. ##################### INSTALLATION END ##################### 
  25. # Expose the default port 
  26. EXPOSE 27017 
  27. # Default port to execute the entrypoint (MongoDB) 
  28. CMD ["--port 27017"
  29. # Set default container command 
  30. ENTRYPOINT usr/bin/mongod 

 

构建镜像

使用上述的Dockerfile,我们已经可以开始构建MongoDB镜像

 

  1. sudo docker build -t my_mongodb . 

 

#p#

Dockerfile 示例二:创建一个Nginx的镜像

Nginx简述

Nginx是一个高性能的 HTTP 和 反向代理 服务器。它因为它的轻量级,易用,易于扩展而流行于业界。基于优良的架构设计,它能够比之前的类似软件处理更多的请求。它也可以用来提供静态文件服务,比如图片,脚本和CSS。

和上个例子一样,我们还是从基础镜像开始,运用FROM命令和MAINTAINER命令

 

  1. ############################################################ 
  2. # Dockerfile to build Nginx Installed Containers 
  3. # Based on Ubuntu 
  4. ############################################################ 
  5. # Set the base image to Ubuntu 
  6. FROM ubuntu 
  7. # File Author / Maintainer 
  8. MAINTAINER Maintaner Name 

 

安装Nginx

 

  1. # Install Nginx 
  2. # Add application repository URL to the default sources 
  3. RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list 
  4. # Update the repository 
  5. RUN apt-get update 
  6. # Install necessary tools 
  7. RUN apt-get install -y nano wget dialog net-tools 
  8. # Download and Install Nginx 
  9. RUN apt-get install -y nginx 

 

Bootstrapping

安装Nginx后,我们需要配置Nginx并且替换掉默认的配置文件

 

  1. # Remove the default Nginx configuration file 
  2. RUN rm -v /etc/nginx/nginx.conf 
  3. # Copy a configuration file from the current directory 
  4. ADD nginx.conf /etc/nginx/ 
  5. # Append "daemon off;" to the beginning of the configuration 
  6. RUN echo "daemon off;" >> /etc/nginx/nginx.conf 
  7. # Expose ports 
  8. EXPOSE 80 
  9. # Set the default command to execute 
  10. # when creating a new container 
  11. CMD service nginx start 

 

最后的Dockerfile

 

  1. ############################################################ 
  2. # Dockerfile to build Nginx Installed Containers 
  3. # Based on Ubuntu 
  4. ############################################################ 
  5. # Set the base image to Ubuntu 
  6. FROM ubuntu 
  7. # File Author / Maintainer 
  8. MAINTAINER Maintaner Name 
  9. # Install Nginx 
  10. # Add application repository URL to the default sources 
  11. RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list 
  12. # Update the repository 
  13. RUN apt-get update 
  14. # Install necessary tools 
  15. RUN apt-get install -y nano wget dialog net-tools 
  16. # Download and Install Nginx 
  17. RUN apt-get install -y nginx 
  18. # Remove the default Nginx configuration file 
  19. RUN rm -v /etc/nginx/nginx.conf 
  20. # Copy a configuration file from the current directory 
  21. ADD nginx.conf /etc/nginx/ 
  22. # Append "daemon off;" to the beginning of the configuration 
  23. RUN echo "daemon off;" >> /etc/nginx/nginx.conf 
  24. # Expose ports 
  25. EXPOSE 80 
  26. # Set the default command to execute 
  27. # when creating a new container 
  28. CMD service nginx start 

 

使用Dockerfile自动构建Nginx容器

因为我们命令Docker用当前目录的Nginx的配置文件替换默认的配置文件,我们要保证这个新的配置文件存在。在Dockerfile存在的目录下,创建nginx.conf:

 

  1. sudo nano nginx.conf 

 

然后用下述内容替换原有内容:

 

  1. worker_processes 1; 
  2. events { worker_connections 1024; } 
  3. http { 
  4.      sendfile on; 
  5.      server { 
  6.          listen 80; 
  7.          location / { 
  8.               proxy_pass http://httpstat.us/; 
  9.               proxy_set_header X-Real-IP $remote_addr; 
  10.          } 
  11.      } 

 

让我们保存nginx.conf。之后我们就可以用Dockerfile和配置文件来构建镜像。

原文链接:http://www.oschina.net/news/64396/dockerfile-instructions
 

责任编辑:Ophira 来源: oschina.net
相关推荐

2009-11-07 11:18:57

2011-04-06 09:39:49

mysql5存储

2015-07-21 12:43:58

Dockerfile命令实例

2022-01-07 06:12:08

RPC框架限流

2020-03-30 17:43:13

开源开源项目编写文档

2019-04-19 08:04:57

程序员Dockerfile容器

2021-06-29 12:10:00

CRC校验码C语言

2018-10-15 10:13:00

网络拓扑结构

2024-03-07 11:39:24

HadolintDockerfile工具

2011-06-16 17:54:30

Qt Mplayer

2009-09-03 10:52:41

C#递归树

2009-07-06 18:16:00

Servlet程序Cookie

2009-08-13 14:36:40

C#结构体构造函数

2009-08-12 16:38:35

C#读取XML节点

2011-07-05 17:54:43

QT Sqlite ARM

2010-03-18 20:00:35

Java socket

2023-10-19 11:53:53

2019-08-14 08:03:49

LinuxShell脚本web服务

2009-08-20 09:58:06

C#操作文本文件

2020-05-16 12:06:32

微信公众平台Python开发
点赞
收藏

51CTO技术栈公众号