您的位置:首页 > 运维架构 > Docker

Dockerfile详解

2016-07-19 11:37 731 查看
Docker可以通过获取Dockerfile编写的命令自动Build出一个新的镜像,里面的Docker内建命令会帮助我们在已有的image下创建一个新的定制image.这里我们先给大家介绍一些常用Dockerfile编写规范.
1.FROM FROM <image>:<tag>FROM会使用当前本地或者远程Docker仓库的image, 这个要首先写到该脚本的第一行.例:
FROM centos/latest
这里代表我们使用本地的centos67base/apache镜像.2.MAINTAINERMAINTAINER <name>这个会给生成的镜像创建一个作者名
3.RUNRUN <command> (命令行格式, 执行一段shell命令)RUN ["executable", "param1", "param2"] (列表格式, "executable"可以更改不同shell格式, 例如/bin/sh或/bin/bash, "param1"代表shell参数)这个可以理解为执行shell命令到image, 一般来说用来给镜像安装package, 或者更改系统配置等.例:
RUN /bin/bash -c echo 123
RUN ["/bin/bash" "-c" "echo 123"]

4.CMDCMD ["executable","param1","param2"] (同RUN)CMD ["param1","param2"] (使用系统默认shell)CMD command param1 param2 (命令行格式)这个CMD基本使用格式与RUN类似, 区别在于如果存在多个CMD行, 则只有最后一行生效, 这个我们常用执行默认的shell命令给该容器.
5.LABELLABEL <key>=<value> <key>=<value> <key>=<value> ...例:
LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates \
that label-values can span multiple lines."
这个可以理解为给image传送一个键值对.可以使用如下命令查看已Build好的image LABEL# docker inspect
6.EXPOSEEXPOSE <port> [<port>...]这个用来通知容器在某一具体端口进行监听, 但不会打开该端口, 如需打开要在创建容器容器的时候配合-P参数使用.
7.ENVENV <key> <value>ENV <key>=<value> ...例如:
ENV myName="John Doe" myDog=Rex\ The\ Dog \
myCat=fluffy
相当于给镜像添加环境变量.
8.ADDADD <src>... <dest>ADD ["<src>",... "<dest>"] (这种写法会避免路径名含有空格而报错)ADD命令会copy本地目录, 文件或远程URL到容器目的路径, <src>, <dest>支持通配符和绝对相对路径.例如:
ADD hom* /mydir/        # adds all files starting with "hom"
ADD hom?.txt /mydir/    # ? is replaced with any single character, e.g., "home.txt"
ADD test relativeDir/   # adds "test" to `WORKDIR`/relativeDir/
ADD test /absoluteDir   # adds "test" to /absoluteDir

9.COPYCOPY <src>... <dest>COPY ["<src>",... "<dest>"] (这种写法会避免路径名含有空格而报错)本身用法和ADD类似, 唯独不能使用远程URL
10.VOLUMEVOLUME ["/data"]
VOLUME会给该image创建一个挂载点.
11.WORKDIRWORKDIR /path/to/workdir顾名思义, 指定一个当前的工作目录.
12.Dockerfile实例
# Nginx
#
# VERSION               0.0.1

FROM      ubuntu
MAINTAINER Victor Vieux <victor@docker.com>

LABEL Description="This image is used to start the foobar executable" Vendor="ACME Products" Version="1.0"
RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server

# Firefox over VNC
#
# VERSION               0.3
FROM ubuntu

# Install vnc, xvfb in order to create a 'fake' display and firefox
RUN apt-get update && apt-get install -y x11vnc xvfb firefox
RUN mkdir ~/.vnc
# Setup a password
RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
# Autostart firefox (might not be the best way, but it does the trick)
RUN bash -c 'echo "firefox" >> /.bashrc'

EXPOSE 5900
CMD    ["x11vnc", "-forever", "-usepw", "-create"]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: