首页 > 代码库 > 阿里云部署Docker(9)----Dockerfile脚本定制镜像
阿里云部署Docker(9)----Dockerfile脚本定制镜像
本文为原创文章,转载需注明转自:http://blog.csdn.net/minimicall?viewmode=contents
技术爱好者都是比较懒的。而docker又是开发者支持起来的。所以,它肯定是有比较懒的方式供我们定制自己需要的东西。
docker build
docker 用build指令来执行dockerfile脚本。
具体的用法:
- sudo docker build .
当然,你可以指定你建立的镜像的名字。
- 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
- docker images
你会发现多了你下载的镜像。
好接下来我们讲讲Dockerfile本身如何编写。
格式:
- # Comment
- INSTRUCTION arguments
FROM
所有的镜像都应该是基于某一个现有的镜像。
所以,就有了FROM 指令
- FROM <image>
- FROM <Image>:<TAG>
FROM语句必须是第一句“非注释”语句,在Dockerfile中。
我们总是会想在一个脚本里面添加些注释,来说明一些想说的话。注释
那就是注释:#开头的行。
但是#在行中,则却表示是一个参数。
维护
接下来,需要说明维护人。
- MAINTAINER <name>
RUN指令
RUN指令应该是用的最多的指令。
- RUN <command> (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 <port> [<port>...]
ENV
环境变量的设置- ENV <key> <value>
环境变量一旦设定,对整个Dockerfile都是有效的。
当然,key = value这样直接说,其实效果是一样的。
ADD指令
有点像拷贝指令,至少它就是完成文件的拷贝工作的。
- ADD <src> <dst>
- 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)
这里,我给大家贴原文会比较好。
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,等就不想说了。
最后给出一个示例
- # Nginx
- #
- # VERSION 0.0.1
- FROM ubuntu
- MAINTAINER Victor Vieux <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
- # You?ll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with
- # /oink.
阿里云部署Docker(9)----Dockerfile脚本定制镜像