使用dockerfile安装nginx

思路:使用shell脚本(nginx_install.sh)安装,注意最终shell脚本nginx的压缩包及dockerfile文件要放一起

1,#编辑下面的nginx_install.sh脚本(如下)写入文件中
[root@localhost local]# cd /home/
[root@localhost home]# vi nginx_install.sh
2,#编写dockerfile,内容如下..
[root@localhost home]# vi /usr/local/dockerfile 
3,构建镜像,走到最后,看到成功!
[root@localhost local]# docker build -t mycentos:nginx .
Successfully built b0df0546c0e8
Successfully tagged mycentos:nginx
 #进入容器里面
[root@pg-1 nginx-1.16.0]# docker exec -it  5054c45de99 /bin/bash
[root@5054c45de995 /]# cd /usr/local/nginx
[root@localhost nginx]# cd sbin/
[root@localhost sbin]# ll
总用量 3740
-rwxr-xr-x. 1 root root 3825912 12月 20 17:20 nginx
#启动页面访问看到成功!
[root@localhost sbin]# ./nginx 
​
Nginx镜像启动注意:
在容器里nginx是以daemon方式启动,退出容器时,nginx程序也会随着停止:
/usr/local/nginx/sbin/nginx
使用前台方式永久运行:/usr/local/nginx/sbin/nginx -g "daemon off;"
​
#杀掉原来的nginx
[root@localhost sbin]# pkill nginx 
启动,访问页面成功!
[root@localhost sbin]# docker run -itd -p 80:80 mycentos:nginx /usr/local/nginx/sbin/nginx -g "daemon off;"
​
​

dockerfile

FROM centos:7
ADD nginx-1.16.0.tar.gz /usr/local
COPY nginx_install.sh /usr/local
RUN sh /usr/local/nginx_install.sh
EXPOSE 80

nginx_install.sh

#!/bin/bash
yum install -y gcc gcc-c++ make pcre pcre-devel zlib zlib-devel
cd /usr/local/nginx-1.16.0
./configure --prefix=/usr/local/nginx && make && make install
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐