本篇笔记记录了Docker的Nginx镜像制作过程,使用Dockerfile指令,基础镜像使用官方Centos7.8,Nginx源码编译安装
创建镜像制作目录
mkdir nginx-image
cd nginx-image
下载Nginx源码包
wget http://nginx.org/download/nginx-1.18.0.tar.gz
创建站点目录
mkdir wwwroot
创建测试首页
vim wwwroot/index.html
代码如下
<html>
<head>
<title>Nginx</title>
</head>
<body>
<span>Test Nginx Image</span>
</body>
</html>
创建Dockerfile文件
vim Dockerfile
配置如下
#基础镜像,使用官方的centos7.8.2003
FROM centos:centos7.8.2003
#作者信息
MAINTAINER "再现理想"
#Nginx源码解包至容器
ADD nginx-1.18.0.tar.gz /opt
#切换工作目录
WORKDIR /opt/nginx-1.18.0
#容器内执行命令:安装编译依赖,创建用户和组,开始预编译,编译,安装
RUN yum -y install gcc pcre-devel openssl-devel make \
&& groupadd www-data && useradd -s /sbin/nologin -g www-data www-data \
&& ./configure \
--prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf \
--user=www-data \
--group=www-data \
--with-pcre \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module && make && make install
#设置要挂载到宿主机的目录
VOLUME ["/usr/local/nginx/html"]
#设置nginx环境变量
ENV PATH /usr/local/nginx/sbin:$PATH
#暴露80端口
EXPOSE 80
#容器启动时执行nginx命令
ENTRYPOINT ["nginx"]
#nginx命令参数,CMD和ENTRYPOINT一期使用时将作为ENTRYPOINT的参数
CMD ["-g","daemon off;"]
构建镜像
docker build ./ -t jmsite/nginx:1.01
构建过程太长,截图最后的结果
查看当前镜像
docker images
运行Nginx容器
docker run -p 80:80 --name nginx-80 \
-v /root/nginx-image/wwwroot:/usr/local/nginx/html \
-d jmsite/nginx:1.01
查看当前容器
docker container ps -a
容易已经处于UP
运行状态
浏览器访问Nginx
进入容器
docker exec -it 50d892bee755 /bin/bash
查看Nginx编译信息
nginx -V
查看Nginx进程信息
ps aux | grep nginx
镜像导出和导入
导出
docker save -o jmsite-nginx.tar jmsite/nginx:1.01
导入
docker load -i jmsite-nginx.tar