dockerfile- format and use

A, dockerfile format
Dockerfile is a text file that contains the command for combining images, any command can be invoked from the command line, automatically generating an image by reading Docker dockerfile instructions. docker build for constructing the command image from dockerfile. You can use the -f flag to point dockerfile path of the file system in the docker build command .
 
 
1. the FROM : Specifies which base image based, must be the first order!
Format: the FROM <Image> or FROM <image>: <tag>
such as
FROM centos
FROM centos:latest
 
2. MAINTAINER : Specifies the author information!
Format:  MAINTAIN <name> 
such as
MAINTAINER  test  test@[email protected]
 
3. the RUN : mirroring operation instruction!
Format: the RUN <Command> or RUN [ "executable", "param1 ", "param2"]
such as
RUN  yum install  httpd
RUN ["/bin/bash", "-c", "echo hello"]
 
4. CMD : After building container calls before calling after the vessel started!
三种格式:CMD ["executable", "param1", "param2"]、CMD command param1 param2
CMD ["param1", "param2"],RUN和CMD看起来挺像,但是CMD用来指定容器启动时用到的命令,只能有一条。
比如
CMD ["/bin/bash", "/usr/local/nginx/sbin/nginx", "-c", "/usr/local/nginx/conf/nginx.conf"]
 
5. EXPOSE:指定于外界交互的端口!
格式: EXPOSE <port> [<port>...]
比如
EXPOSE 22 80 8443
这个用来指定要映射出去的端口,比如容器内部我们启动了sshd和nginx,所以我们需要把22和80端口暴漏出去。这个需要配合-P(大写)来工作,也就是说在启动容器时,需要加上-P,让它自动分配。如果想指定具体的端口,也可以使用-p(小写)来指定。
 
6. ENV:设置环境变量!
格式: ENV  <key> <value>
比如  
ENV PATH /usr/local/mysql/bin:$PATH
它主要是为后续的RUN指令提供一个环境变量,我们也可以定义一些自定义的变量
ENV MYSQL_version 5.6
 
7. ADD: 将本地文件添加到容器中,tar类型文件会自动解压(网络压缩资源不会被解压),可以访问网络资源,类似wget!
格式: add <src> <dest>将本地的一个文件或目录拷贝到容器的某个目录里。 其中src为Dockerfile所在目录的相对路径,它也可以是一个url。
比如
ADD <conf/vhosts> </usr/local/nginx/conf>
 
8. COPY:功能类似ADD,但是不能自动解压文件,也不能访问网络资源!
格式:和ADD一样,不同的是,它不支持url
 
9. ENTRYPOINT:配置容器,使其可执行化。配合CMD可省去"application",只使用参数!
格式:类似CMD
容器启动时要执行的命令,它和CMD很像,也是只有一条生效,如果写多个只有最后一条有效。CMD不同是:
CMD 是可以被 docker run 指令覆盖的,而ENTRYPOINT不能覆盖。比如,容器名字为xiaoming
我们在Dockerfile中指定如下CMD:
CMD ["/bin/echo", "test"]
启动容器的命令是  docker run xiaoming 这样会输出 test
假如启动容器的命令是 docker run -it xiaoming  /bin/bash  什么都不会输出
ENTRYPOINT不会被覆盖,而且会比CMD或者docker run指定的命令要靠前执行
ENTRYPOINT ["echo", "test"]
docker run -it xiaoming  123
则会输出 test  123 ,这相当于要执行命令  echo test  123 
 
10. VOLUME:用于持久化目录!
格式: VOLUME ["/data"]
创建一个可以从本地主机或其他容器挂载的挂载点。
 
11. USER:  指定容器运行用户,一般不指定默认ROOT用户!
格式:USER daemon
指定运行容器的用户
 
12. WORKDIR:  工作目录,类似CD命令!
格式: WORKDIR  /path/to/workdir
为后续的RUN、CMD或者ENTRYPOINT指定工作目录
 
 
二、dockerfile使用
 
需求:使用dockerfile方式,新建一个容器,安装Nginx WEB服务,开放80端口
 
1:编写Dockerfile执行文件
[root@host1 docker]# vim Dockerfile
## Set the base image to CentOS //设置基于为centos镜像
FROM centos
 
## Author information //指定作者信息
MAINTAINER test [email protected]
 
## Installation tools required //安装nginx之前所需要的工具包
RUN yum -y install wget gcc pcre-devel zlib zlib-devel make net-tools
 
## Installl nginx //安装nginx
ADD http://nginx.org/download/nginx-1.14.2.tar.gz .
RUN tar -zxf nginx-1.14.2.tar.gz
RUN cd nginx-1.14.2 && ./configure --prefix=/usr/local/nginx && make && make install
RUN rm -f /usr/local/nginx/conf/nginx.conf
COPY nginx_conf /usr/local/nginx/conf/nginx.conf
 
## ENV Nginx path //设置nginx环境变量
ENV PATH /usr/local/nginx/sbin:$PATH
 
## Open ports //打开80端口外放
EXPOSE 80
 
## Set the default command to execute when creating a new container //
ENTRYPOINT /usr/local/nginx/sbin/nginx && tail -f /etc/passwd
 
 
2:执行Dockerfile文件,镜像名称为ABC,后面点表示docker
[root@host1 docker]# docker build -t abc .
 
3:映射容器80端口,让外部访问
[root@host1 docker]# docker run -itd -p 8080:80 abc bash
 
 
4:进入容器查看是否启动nginx
[root@host1 ~]# docker exec -it 6ce021 bash
[root@6ce021dc323d /]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7/nginx: master pro

Guess you like

Origin www.cnblogs.com/douyi/p/11573783.html