阿里云部署Docker(9)----Dockerfile脚本定制镜像
时间:2014-11-22 来源:服务器之家 投稿:root

阿里云部署Docker系列文章目录:
阿里云部署Docker(1)----阿里云Ubuntu环境搭建Docker服务
阿里云部署Docker(2)
阿里云部署Docker(3)----指令学习
阿里云部署Docker(4)----容器的使用
阿里云部署Docker(5)----管理和发布您的镜像
阿里云部署Docker(6)----解决删除镜像问题
阿里云部署Docker(7)----将容器连接起来
阿里云部署Docker(8)----安装和使用redmine
阿里云部署Docker(9)----Dockerfile脚本定制镜像

技术爱好者都是比较懒的。而docker又是开发者支持起来的。所以,它肯定是有比较懒的方式供我们定制自己需要的东西。
docker build

docker 用build指令来执行dockerfile脚本。

具体的用法:

sudo docker build .
小心后面那个点,表示当前目录。当前目录有一个Dockerfile的文件。

当然,你可以指定你建立的镜像的名字。

sudo docker build -t shykes/myapp .

然后你就可以看到执行过程,或许,会非常的漫长,取决于要下的东西的大小和你的网速。

sudo docker build -t SvenDowideit/ambassador .

Uploading context 10.24 kB

Uploading context

Step 1 : FROM docker-ut

---> cbba202fe96b

Step 2 : MAINTAINER SvenDowideit@home.org.au

---> Using cache

---> 51182097be13

Step 3 : CMD env | grep _TCP= | sed 's/._PORT_([0-9])_TCP=tcp://(.):(.)/socat TCP4-LISTEN:1,fork,reuseaddr TCP4:2:3 &/' | sh && top

---> Using cache

---> 1a5ffc17324d

Successfully built 1a5ffc17324d
当你下完之后,你可以用:
你会发现多了你下载的镜像。

好接下来我们讲讲Dockerfile本身如何编写。
格式:

Comment

INSTRUCTION arguments
命令是大写的。 FROM

所有的镜像都应该是基于某一个现有的镜像。

所以,就有了FROM 指令
或者,更加具体点说明它的Tag。

FROM语句必须是第一句“非注释”语句,在Dockerfile中。
我们总是会想在一个脚本里面添加些注释,来说明一些想说的话。 注释

那就是注释:#开头的行。

但是#在行中,则却表示是一个参数。
维护

接下来,需要说明维护人。
填上你的NICK NAME。表示你做的。 RUN指令

RUN指令应该是用的最多的指令。

RUN (the command is run in a shell - /bin/sh -c - shell form)

另一种方式是:

RUN ["executable", "param1", "param2"] (exec form)

RUN语句会在当前镜像的基础上执行该条指令,同时执行完就成了一个新的镜像一样,即数据和影响都是会保存的,然后用这个新的镜像去执行下一条指令,这样上一条的结果镜像是下一条指令的基础,如此不断推进。

CMD指令

格式:

CMD ["executable","param1","param2"] (exec form, this is the preferred form)

CMD ["param1","param2"] (as default parameters to ENTRYPOINT)

CMD command param1 param2 (shell form)

有三种形式。

CMD在DOckerfile里面只能用一次,如果你写了很多条,那么只有最后一条是有效的。

CMD有什么用呢,可以理解为Main函数一样吧,作为一个入口。具体见英文

The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.
EXPOSE 这个单词的中文叫什么,暴露。对,他就是暴露镜像的某个接口出来。例如,我的镜像是用来做http服务的,那么我就理应暴露我镜像的80端口。然后-p 主机端口:80 ,还记得吧。端口映射。

EXPOSE [...]
ENV 环境变量的设置
环境变量一旦设定,对整个Dockerfile都是有效的。

当然,key = value这样直接说,其实效果是一样的。
ADD指令

有点像拷贝指令,至少它就是完成文件的拷贝工作的。

ADD hom* /mydir/ # adds all files starting with "hom"

ADD hom?.txt /mydir/ # ? is replaced with any single character

COPY指令

和ADD一样,是拷贝
ENTRYPOINT 入口点

真正的MAIN函数

ENTRYPOINT ["executable", "param1", "param2"] (exec form, the preferred form)

ENTRYPOINT command param1 param2 (shell form)
CMD和这个指令的区别,是应用的场景不一样。

这里,我给大家贴原文会比较好。

An ENTRYPOINT helps you to configure a container that you can run as an executable. That is, when you specify an ENTRYPOINT, then the whole container runs as if it was just that executable.

Unlike the behavior of the CMD instruction, The ENTRYPOINT instruction adds an entry command that will not be overwritten when arguments are passed to docker run. This allows arguments to be passed to the entry point, i.e. docker run -d will pass the -d argument to the entry point.

You can specify parameters either in the ENTRYPOINT JSON array (as in "like an exec" above), or by using a CMD instruction. Parameters in the ENTRYPOINT instruction will not be overridden by the docker run arguments, but parameters specified via a CMD instruction will be overridden by docker run arguments.

Like a CMD, you can specify a plain string for the ENTRYPOINT and it will execute in /bin/sh -c:

FROM ubuntu
ENTRYPOINT ls -l
For example, that Dockerfile's image will always take a directory as an input and return a directory listing. If you wanted to make this optional but default, you could use a CMD instruction:

FROM ubuntu
CMD ["-l"]
ENTRYPOINT ["ls"]
WORKDIR 工作目录

RUN ENTERPOINT带的指令在哪里执行的设置。

此外,还有一些指令,例如

USER ,ONBUILD,等就不想说了。
最后给出一个示例

Nginx

VERSION 0.0.1

FROM ubuntu

MAINTAINER Victor Vieux mailto:victor@docker.com

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

Multiple images example

VERSION 0.1

FROM ubuntu

RUN echo foo > bar

Will output something like ===> 907ad6c2736f

FROM ubuntu

RUN echo moo > oink

Will output something like ===> 695d7793cbe4

Youll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with

/oink.

转载请注明原文地址:http://www.server110.com/docker/201411/11143.html