本文为原创文章,转载需注明转自:http://blog.csdn.net/minimicall?viewmode=contents
技术爱好者都是比较懒的。而docker又是开发者支持起来的。所以,它肯定是有比较懒的方式供我们定制自己需要的东西。
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当你下完之后,你可以用:
docker images
好接下来我们讲讲Dockerfile本身如何编写。
# Comment INSTRUCTION arguments命令是大写的。
所有的镜像都应该是基于某一个现有的镜像。
所以,就有了FROM 指令
FROM <image>或者,更加具体点说明它的Tag。
FROM <Image>:<TAG>
FROM语句必须是第一句“非注释”语句,在Dockerfile中。
我们总是会想在一个脚本里面添加些注释,来说明一些想说的话。
那就是注释:#开头的行。
但是#在行中,则却表示是一个参数。
接下来,需要说明维护人。
MAINTAINER <name>填上你的NICK NAME。表示你做的。
RUN指令应该是用的最多的指令。
RUN <command> (the command is run in a shell - /bin/sh -c - shell form)
RUN ["executable", "param1", "param2"] (exec form)
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 <port> [<port>...]
ENV <key> <value>
当然,key = value这样直接说,其实效果是一样的。
有点像拷贝指令,至少它就是完成文件的拷贝工作的。
ADD <src> <dst>
ADD hom* /mydir/ # adds all files starting with "hom" ADD hom?.txt /mydir/ # ? is replaced with any single character
和ADD一样,是拷贝
真正的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 <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"]
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脚本定制你的镜像
原文地址:http://blog.csdn.net/minimicall/article/details/40431849