[toc]
最后一条也即是我们今天所要讲的,通过Dockfile来创建镜像。
格式 FROM 或者 FROM :, 比如
FROM centos
FROM centos:latest
格式 MAINTAIN ,比如
MAINTAINER xavi [email protected]
格式为 RUN 或者 RUN [“executable”, “param1”, “param2”],比如
RUN yum install httpd
RUN ["/bin/bash", "-c", "echo hello"]
CMD ["executable", "param1", "param2"]
CMD command param1 param2
CMD ["param1", "param2"]
CMD ["/bin/bash", "/usr/local/nginx/sbin/nginx", "-c", "/usr/local/nginx/conf/nginx.conf"]
比如
EXPOSE 22 80 8443
比如
ENV PATH /usr/local/mysql/bin:$PATH
ADD
ADD http://www.asd.com/1.txt /usr/local/src //指定网络下载
比如,容器名字为asd9577
我们在Dockerfile中指定如下CMD:
CMD ["/bin/echo","test"]
启动容器的命令是 docker run asd9577 这样会输出 test
假如启动容器的命令是 docker run -it asd9577 /bin/bash 什么都不会输出
ENTRYPOINT不会被覆盖,而且会比CMD或者docker run指定的命令要靠前执行
ENTRYPOINT ["echo", "test"]
docker run -it asd9577 123
则会输出 test 123 ,这相当于要执行命令 echo test 123
格式 VOLUME ["/data"]
创建一个可以从本地主机或其他容器挂载的挂载点。
格式 USER daemon
指定运行容器的用户
格式 WORKDIR /path/to/workdir
为后续的RUN、CMD或者ENTRYPOINT指定工作目录
vim Dockerfile //内容如下
## Set the base image to CentOS
FROM centos
# File Author / Maintainer
MAINTAINER xavi [email protected]
# Install necessary tools
RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel
# Install Nginx
ADD http://nginx.org/download/nginx-1.8.0.tar.gz .
RUN tar zxvf nginx-1.8.0.tar.gz
RUN mkdir -p /usr/local/nginx
RUN cd nginx-1.8.0 && ./configure --prefix=/usr/local/nginx && make && make install
RUN rm -fv /usr/local/nginx/conf/nginx.conf
ADD http://www.apelearn.com/study_v2/.nginx_conf /usr/local/nginx/conf/nginx.conf
# Expose ports
EXPOSE 80
# Set the default command to execute when creating a new container
ENTRYPOINT /usr/local/nginx/sbin/nginx && tail -f /etc/passwd
详解如下:
## Set the base image to CentOS
FROM centos //docker images 出现的镜像
# File Author / Maintainer
MAINTAINER xavi [email protected] //指定用户信息
# Install necessary tools
RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel //安装必须的插件
# Install Nginx
ADD http://nginx.org/download/nginx-1.8.0.tar.gz . //下载nginx的源码包到本地当前目录
RUN tar zxvf nginx-1.8.0.tar.gz //解压
RUN mkdir -p /usr/local/nginx //级联创建nginx目录
RUN cd nginx-1.8.0 && ./configure --prefix=/usr/local/nginx && make && make install //进入目录编译并安装
RUN rm -fv /usr/local/nginx/conf/nginx.conf //删除原配置文件
ADD http://www.apelearn.com/study_v2/.nginx_conf /usr/local/nginx/conf/nginx.conf //下载配置好的nginx配置文件到指定目录
# Expose ports
EXPOSE 80 //映射出去的端口
# Set the default command to execute when creating a new container
ENTRYPOINT /usr/local/nginx/sbin/nginx && tail -f /etc/passwd //设置开机启动的命令(有仔细的朋友可能已经发现 为什么需要 tail -f 呢?其实很多人认为这是一个bug 如果你不添加这个当执行完他就会退出)
//名字一定要命名为 Dockerfile 不然docker build是找不到文件的,-t 指定容器的名字(不可以出现大写字母), 后面的 . 指定去哪儿找Dockerfile
docker build -t centos_nginx .
有报警,然后中断,再次运行后成功!
最后我们可以看到如下成功的提示:
Successfully built 1f654937dd3b
Successfully tagged centos_nginx_n:latest
检查新的镜像是否已经生成:已经生成centos_nginx
[root@xavi ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx latest b92736976557 3 minutes ago 352MB
centos_with_nginx latest 9539596d8741 24 hours ago 602MB
192.168.72.130:5000/ubuntu latest 113a43faa138 4 weeks ago 81.2MB
ubuntu latest 113a43faa138 4 weeks ago 81.2MB
centos latest 49f7960eb7e4 4 weeks ago 200MB
registry latest d1fd7d86a825 5 months ago 33.3MB
[root@xavi ~]# docker run -itd -p 81:80 centos_nginx bash
aeec1bd1bd2b6e7d7b4a8202d33490c537411d7a842bc333e168fea216ad8675
[root@xavi ~]# docker exec -it aeec1bd1 bash
[root@aeec1bd1bd2b /]# ps aux| grep nginx
root 1 0.1 0.0 11684 1344 pts/0 Ss+ 03:47 0:00 /bin/sh -c /usr/local/nginx/sbin/nginx && tail -f /etc/passwd bash
root 6 0.0 0.0 24884 792 ? Ss 03:47 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 8 0.0 0.1 27328 3360 ? S 03:47 0:00 nginx: worker process
nobody 9 0.0 0.1 27328 3360 ? S 03:47 0:00 nginx: worker process
root 23 0.0 0.0 9092 660 pts/1 S+ 03:48 0:00 grep --color=auto nginx
[root@aeec1bd1bd2b /]# exit
exit
[root@xavi ~]# curl 127.0.0.1:81
Welcome to nginx!