基本结构
Dockerfile 是一个文本格式的配置文件,用户可以使用 Dockerfile 快速创建自定义镜像。
Dockerfile 由一行行命令语句组成,并且支持以 # 开头的注释行。
Docker分为四部分:
- 基础镜像信息
- 维护者信息
- 镜像操作指令
- 容器启动时默认要执行的指令
例如:
# This dockerfile uses the ubuntu image
# VERSION 2 - EDITION 1
# Author: seancheng
# Command format: Instruction [arguments / command] ...
# 第一行必须指定基于的基础镜像
FROM ubuntu
# 维护者信息
LABEL MAINTAINER='seancheng xianshangxian@126.com'
# 镜像操作指令
RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
RUN apt-get update && apt-get install -y nginx
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
# 容器启动时默认要执行的指令
CMD /usr/sbin/nginx
其中,一开始必须指明所基于的镜像名称,接下来一般会说明维护者信息。
后面则是镜像操作指令,例如RUN指令,RUN指令将对镜像执行跟随的命令。每运行一条RUN指令,镜像添加新的一层,并提交。
最后是CMD指令来指定运行容器时的操作指令。
指令
指令的一般格式为INSTRUCTION arguments,指令包括:
- FROM
- LABEL MAINTAINER
- RUN
- CMD
- EXPOSE
- ENV
- ADD
- COPY
- ENTRYPOINT
- VOLUME
- USER
- WORKDIR
- ONBUILD
FROM
格式为FROM <image>或FROM <image>:<tag>。
第一条指令必须为FROM指令。并且,如果在同一个Dockerfile中创建多个镜像时,可以使用多个FROM指令(每个镜像一次)。
LABEL MAINTAINER
格式为LABEL MAINTAINER <name email_address>,指定维护者信息
RUN
格式为RUN <command>或RUN [“executable”,“param1”,“param2”]。
前者将在shell终端中运行命令,即/bin/sh -c;后者则使用exec执行。指定使用其他终端可以通过第二种方式实现,例如:
RUN ["/bin/bash","-c","echo hello"]
每条RUN指令将在当前镜像基础上执行指定命令,并提交为新的镜像。当命令较长时可以使用 \ 来换行,例如:
RUN echo "hello world\nhello tom" > /tmp/abc && \
cat /tmp/abc
CMD
CMD支持三种格式:
- CMD [“executable”,“param1”,“param2”]使用exec执行,推荐方式
- CMD command param1 param2在/bin/sh中执行,提供给需要交互的应用
- CMD [“param1”,“param2”]提供给ENTRYPOINT的默认参数
CMD用于指定启动容器时默认要执行的命令,每个Dockerfile只能有一条CMD命令。如果指定了多条命令,只有最后一条会被执行。
如果用户启动容器时指定了运行的命令,则会覆盖掉CMD指定的命令。
EXPOSE
格式为EXPOSE <port> [<port>…]。
例如:
EXPOSE 22 80 8443
EXPOSE用于告诉Docker服务器容器暴露的端口号,供互联系统使用。
在启动容器时通过-P,Docker主机会自动分配一个端口转发到指定的端口;
使用-p则可以具体指定哪个本地端口映射过来。
ENV
格式为ENV <key> <value>。指定一个环境变量,会被后续RUN指令使用,并在容器运行时保持。例如:
ENV PG_MAJOR 9.3
ENV PG_VERSION 9.3.4
RUN curl -SL http://example.com/postgres-$PG_VERSION.tar.xz | tar -xJC /usr/src/postgress && ...
ENV PATH /usr/local/postgres-$PG_MAJOR/bin:$PATH
ADD
格式为ADD\ <dest>。
该命令将复制指定的<src>到容器中的<dest>。其中<src>可以是Dockerfile所在目录的一个相对路径(文件或目录);也可以是一个URL;还可以是一个tar文件(会自动解压为目录)。
COPY
格式为COPY <src> <dest>。
复制本地主机的<src>(为Dockerfile所在目录的相对路径,文件或目录)为容器中的<dest>。目标路径不存在时会自动创建。
当使用本地目录为源目录时,推荐使用COPY。
ENTRYPOINT
ENTRYPOINT有两种格式:
- ENTRYPOINT [“executable”,“param1”,“param2”]
- ENTRYPOINT command param1 param2(在shell中执行)
配置容器启动后执行的命令,并且不可被docker run提供的参数覆盖。而且,如果在docker run的后面提供了参数,这些命令行参数会被当作参数传递给ENTRYPOINT指定的程序。
每个Dockerfile中只能有一个ENTRYPOINT,当指定多个ENTRYPOINT时,只有最后一个生效。
创建镜像
编写完成Dockerfile后,可以通过docker build命令来创建镜像。
基本的格式为docker build [选项] 路径,该命令将读取指定路径下(包括子目录)的Dockerfile,并将该路径下所有内容发送给Docker服务端,由服务端来创建镜像。因此一般建议放置Dockerfile的目录为空目录。
另外,可以通过 .dockerignore 文件(每一行添加一条匹配模式)来让Docker忽略路径下的目录和文件。
要指定镜像的标签信息,可以通过-t选项。
例如,指定Dockerfile所在路径为/tmp/docker_builder/,并且希望生成镜像标签为build_repo/first_image,可以使用下面的命令:
docker build -t build_repo/first_image /tmp/docker_builder/
编写源码apache的Dockerfile文件
//结构
[root@localhost apache]# tree
.
├── Dockerfile
└── files
├── apr-1.7.0.tar.gz
├── apr-util-1.6.1.tar.gz
└── httpd-2.4.49.tar.gz
[root@localhost apache]# cat Dockerfile
#基础镜像
FROM centos
#变量
ENV APR_VERSION 1.7.0
ENV APR_UTIL_VERSION 1.6.1
ENV APACHE_VERSION 2.4.49
#上传本机上的apache压缩包,会自动解压
ADD files/* /usr/src/
#切换当前工作目录
WORKDIR /usr/src/
#执行命令进行安装操作
RUN yum -y install openssl-devel pcre-devel pcre expat-devel libtool gcc gcc-c++ make && \
cd apr-$APR_VERSION && sed -i '/$RM "$cfgfile"/d' configure && \
./configure --prefix=/usr/local/apr && make && make install && \
cd ../apr-util-$APR_UTIL_VERSION && \
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
make && make install && \
cd ../httpd-$APACHE_VERSION && \
./configure --prefix=/usr/local/apache \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork && \
make && make install && \
echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh && \
source /etc/profile.d/httpd.sh && \
ln -s /usr/local/apache/include/ /usr/include/httpd && \
sed -i 's/#ServerName www.example.com:80/ServerName 172.17.0.2:80/g' /usr/local/apache/conf/httpd.conf
#开放端口
EXPOSE 80
#启动时执行的命令
CMD ["/usr/local/apache/bin/apachectl","-D","FOREGROUND"]
创建镜像
[root@localhost apache]# docker build -t apache:0.2 /apache/
Sending build context to Docker daemon 11.08MB
Step 1/9 : FROM centos
---> 5d0da3dc9764
Step 2/9 : ENV APR_VERSION 1.7.0
---> Using cache
---> fbd88f0c3db2
Step 3/9 : ENV APR_UTIL_VERSION 1.6.1
---> Using cache
---> 9f093c47bc92
Step 4/9 : ENV APACHE_VERSION 2.4.49
---> Using cache
---> 61a2ea481cb3
Step 5/9 : ADD files/* /usr/src/
---> Using cache
---> e0e7f7de899d
Step 6/9 : WORKDIR /usr/src/
---> Using cache
---> 0fecb490df90
Step 7/9 : RUN yum -y install openssl-devel pcre-devel pcre expat-devel libtool gcc gcc-c++ make && cd apr-$APR_VERSION && sed -i '/$RM "$cfgfile"/d' configure && ./configure --prefix=/usr/local/apr && make && make install && cd ../apr-util-$APR_UTIL_VERSION && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install && cd ../httpd-$APACHE_VERSION && ./configure --prefix=/usr/local/apache --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork && make && make install && echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh && source /etc/profile.d/httpd.sh && ln -s /usr/local/apache/include/ /usr/include/httpd && sed -i 's/#ServerName www.example.com:80/ServerName 172.17.0.2:80/g' /usr/local/apache/conf/httpd.conf
---> Using cache
---> 14b037aabdb9
Step 8/9 : EXPOSE 80
---> Using cache
---> 64988ed5c4b7
Step 9/9 : CMD ["/usr/local/apache/bin/apachectl","-D","FOREGROUND"]
---> Running in 72688171f5a3
Removing intermediate container 72688171f5a3
---> 8dcedd1337ad
Successfully built 8dcedd1337ad
Successfully tagged apache:0.2
[root@localhost apache]# docker run --name httpd55 -dit -p 80:80 apache:0.2
7e9ce1e5362a1623d93371f051a761b5305d46051f57f0e443d70064cb33f4be
[root@localhost apache]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7e9ce1e5362a apache:0.1 "/usr/local/apache/b…" 9 seconds ago Up 7 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp httpd55
[root@localhost apache]# docker exec -it httpd55 /bin/bash
[root@7e9ce1e5362a src]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*