欢迎访问www.showerlee.com, 您的支持就是我前进的动力.

Dockerfile详解

showerlee 2015-12-31 17:15 DevTools, Docker 阅读 (12,342) 抢沙发

Docker可以通过获取Dockerfile编写的命令自动Build出一个新的镜像,里面的Docker内建命令会帮助我们在已有的image下创建一个新的定制image.

这里我们先给大家介绍一些常用Dockerfile编写规范.

Docker配置传送门: http://www.showerlee.com/archives/1758

1.FROM 

FROM <image>:<tag>

FROM会使用当前本地或者远程Docker仓库的image, 这个要首先写到该脚本的第一行.

例:

FROM centos67base/apache:apache_base

这里代表我们使用本地的centos67base/apache镜像.

查看本地缓存镜像可以使用

# docker images

2.MAINTAINER

MAINTAINER <name>

这个会给生成的镜像创建一个作者名

3.RUN

RUN <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.CMD

CMD ["executable","param1","param2"] (同RUN)

CMD ["param1","param2"] (使用系统默认shell)

CMD command param1 param2 (命令行格式)

这个CMD基本使用格式与RUN类似, 区别在于如果存在多个CMD行, 则只有最后一行生效, 这个我们常用执行默认的shell命令给该容器.

5.LABEL

LABEL <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.EXPOSE

EXPOSE <port> [<port>...]

这个用来通知容器在某一具体端口进行监听, 但不会打开该端口, 如需打开要在创建容器容器的时候配合-P参数使用.

7.ENV

ENV <key> <value>

ENV <key>=<value> ...

例如:

ENV myName="John Doe" myDog=Rex\ The\ Dog \
       myCat=fluffy

相当于给镜像添加环境变量.

8.ADD

ADD <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.COPY

COPY <src>... <dest>

COPY ["<src>",... "<dest>"] (这种写法会避免路径名含有空格而报错)

本身用法和ADD类似, 唯独不能使用远程URL

10.VOLUME

VOLUME ["/data"]
VOLUME会给该image创建一个挂载点.

11.WORKDIR

WORKDIR /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"]

更多详细配置请参考: http://www.showerlee.com/archives/1758 第10项

正文部分到此结束
版权声明:除非注明,本文由(showerlee)原创,转载请保留文章出处!
本文链接:http://www.showerlee.com/archives/1837

继续浏览:DocerfileDocker

还没有评论,快来抢沙发!

发表评论

icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif