阿里云部署Docker(9)----Dockerfile脚本定制镜像

本文为原创文章,转载需注明转自:http://blog.csdn.net/minimicall?viewmode=contents

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

docker build

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

具体的用法:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. sudo docker build .  
小心后面那个点,表示当前目录。当前目录有一个Dockerfile的文件。

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

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. sudo docker build -t shykes/myapp .  

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

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. sudo docker build -t SvenDowideit/ambassador .  
  2. Uploading context 10.24 kB  
  3. Uploading context  
  4. Step 1 : FROM docker-ut  
  5.  ---> cbba202fe96b  
  6. Step 2 : MAINTAINER SvenDowideit@home.org.au  
  7.  ---> Using cache  
  8.  ---> 51182097be13  
  9. Step 3 : CMD env | grep _TCP= | sed 's/.*_PORT_\([0-9]*\)_TCP=tcp:\/\/\(.*\):\(.*\)/socat TCP4-LISTEN:\1,fork,reuseaddr TCP4:\2:\3 \&/'  | sh && top  
  10.  ---> Using cache  
  11.  ---> 1a5ffc17324d  
  12. Successfully built 1a5ffc17324d  
当你下完之后,你可以用:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. docker images  

你会发现多了你下载的镜像。

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

格式:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. # Comment  
  2. INSTRUCTION arguments  
命令是大写的。

FROM

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

所以,就有了FROM 指令

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. FROM <image>  
或者,更加具体点说明它的Tag。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. FROM <Image>:<TAG>  

FROM语句必须是第一句“非注释”语句,在Dockerfile中。

我们总是会想在一个脚本里面添加些注释,来说明一些想说的话。


注释

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

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

维护

接下来,需要说明维护人。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. MAINTAINER <name>  
填上你的NICK NAME。表示你做的。

RUN指令

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

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. RUN <command> (the command is run in a shell - /bin/sh -c - shell form)  

另一种方式是:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. RUN ["executable""param1""param2"] (exec form)  

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

CMD指令

格式:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. CMD ["executable","param1","param2"] (exec form, this is the preferred form)  
  2. CMD ["param1","param2"] (as default parameters to ENTRYPOINT)  
  3. 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 ,还记得吧。端口映射。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. EXPOSE <port> [<port>...]  

ENV

环境变量的设置

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. ENV <key> <value>  

环境变量一旦设定,对整个Dockerfile都是有效的。

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

ADD指令

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

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. ADD <src> <dst>  

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. ADD hom* /mydir/        # adds all files starting with "hom"  
  2. ADD hom?.txt /mydir/    # ? is replaced with any single character  

COPY指令

和ADD一样,是拷贝


ENTRYPOINT

入口点

真正的MAIN函数

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. ENTRYPOINT ["executable""param1""param2"] (exec form, the preferred form)  
  2. 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 <image> -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,等就不想说了。

最后给出一个示例

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. # Nginx  
  2. #  
  3. # VERSION               0.0.1  
  4.   
  5. FROM      ubuntu  
  6. MAINTAINER Victor Vieux <victor@docker.com>  
  7.   
  8. RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server  
  9.   
  10. # Firefox over VNC  
  11. #  
  12. # VERSION               0.3  
  13.   
  14. FROM ubuntu  
  15.   
  16. # Install vnc, xvfb in order to create a 'fake' display and firefox  
  17. RUN apt-get update && apt-get install -y x11vnc xvfb firefox  
  18. RUN mkdir /.vnc  
  19. # Setup a password  
  20. RUN x11vnc -storepasswd 1234 ~/.vnc/passwd  
  21. # Autostart firefox (might not be the best way, but it does the trick)  
  22. RUN bash -c 'echo "firefox" >> /.bashrc'  
  23.   
  24. EXPOSE 5900  
  25. CMD    ["x11vnc""-forever""-usepw""-create"]  
  26.   
  27. # Multiple images example  
  28. #  
  29. # VERSION               0.1  
  30.   
  31. FROM ubuntu  
  32. RUN echo foo > bar  
  33. # Will output something like ===> 907ad6c2736f  
  34.   
  35. FROM ubuntu  
  36. RUN echo moo > oink  
  37. # Will output something like ===> 695d7793cbe4  
  38.   
  39. # You᾿ll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with  
  40. # /oink.  





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值