创建镜像所在的文件夹和Dockerfile文件
[root@localhost ~]# mkdir nginx
[root@localhost ~]# cd nginx/
[root@localhost nginx]# touch Dockerfile
在Dockerfile文件中写入指令,每一条指令都会更新镜像
[root@localhost nginx]# vim Dockerfile
FROM daocloud.io/library/centos:7
MAINTAINER nginx
RUN yum install -y epel-release
RUN yum install -y gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-server make
RUN useradd -s /sbin/nologin -M nginx
ADD nginx-1.16.0.tar.gz /usr/local/
WORKDIR /usr/local/nginx-1.16.0/
RUN ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx
RUN make && make install
RUN mkdir /tmp/nginx
EXPOSE 80
CMD /bin/sh -c 'nginx -g "daemon off;"'
每一个指令后面需要跟空格,语法。
FROM 命令是告诉docker 我们的镜像什么从哪里下载。
MAINTAINER 是描述 镜像的创建人。
RUN 命令是在镜像内部执行。就是说他后面的命令应该是针对镜像可以运行的命令。
将安装包移动到和Dockerfile同一目录下
[root@localhost nginx]# ls
Dockerfile nginx-1.16.0.tar.gz
创建镜像
[root@localhost nginx]# docker build -t nginx:v1 .
通过docker images查看镜像
知识点
Docker 容器启动时,默认会把容器内部第一个进程,也就是pid=1的程序,作为docker容器是否正在运行的依据,如果 docker 容器pid=1的进程挂了,那么docker容器便会直接退出。
使用Dockerfile安装nginx时
需要运行daemon off命令。放置前台启动
nginx默认是以后台模式启动的,Docker未执行自定义的CMD之前,nginx的pid是1,执行到CMD之后,nginx就在后台运行,bash或sh脚本的pid变成了1。所以一旦执行完自定义CMD,nginx容器也就退出了。为了保持nginx的容器不退出,应该关闭nginx后台运行