主题
当前项目需要批量安装多个nginx服务,但是需要nginx配置支持ipv6,但是docker公有仓库的镜像并没有把这个模块加上,于是想通过centos为基础镜像,通过Dockerfile制作一个nginx源码镜像,这样就可以方便后续添加nginx模块和第三方模块。
操作步骤
制作基础镜像
下载centos镜像
docker pull centos:7
验证已经下载好的镜像
docker images | grep centos
创建文件夹
mkdir -p ./docker/imagesMake/nginx
cd ./docker/imagesMake/nginx
下载nginx源码包
wget http://nginx.org/download/nginx-1.15.6.tar.gz
创建Dockerfile
vi Dockerfile
编辑Dockerfile配置
# 基础镜像 base image
FROM centos:7
# 作者 MAINTAINER
MAINTAINER zyr
# 将nginx包放到contes并解压 put nginx-1.15.6.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.15.6.tar.gz /usr/local/src
# 执行nginx编译需要的环境包安装命令 running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
# 改变工作目录change dir to /usr/local/src/nginx-1.15.6
WORKDIR /usr/local/src/nginx-1.15.6
# 在容器装载(创建)文件夹mount a dir to container
VOLUME ["/usr/local/nginx", "/etc/nginx", "/var/log/nginx"]
# 执行命令编译ngingx execute command to compile nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-ipv6 --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_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_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
# 设置环境变量 setup PATH
ENV PATH /usr/local/nginx/sbin:$PATH
# 发布端口 EXPOSE
EXPOSE 80
# 设置容器启动命令 the command of entrypoint
ENTRYPOINT ["nginx"]
# 设置启动命令参数
CMD ["-g","daemon off;"]
制作镜像
docker build -t centos_nginx:v1 .
创建容器并启动
docker run --name nginx1 -d -p80:80 centos_nginx:v1
浏览器输入http://127.0.0.1验证
创带上目录映射的容器并启动
本地创建映射用的目录
mkdir -p /Users/zhengyongrui/docker/imagesMake/nginx/html
mkdir -p /Users/zhengyongrui/docker/imagesMake/nginx/logs
mkdir -p /Users/zhengyongrui/docker/imagesMake/nginx/conf
创建并启动容器
docker run -d --name nginx1 -p 81:80 -v /Users/zhengyongrui/docker/imagesMake/nginx/html:/usr/local/nginx/html -v /Users/zhengyongrui/docker/imagesMake/nginx/logs:/var/log/nginx -v /Users/zhengyongrui/docker/imagesMake/nginx/conf:/etc/nginx/nginx.conf centos_nginx:v1
验证
在/usr/local/share/nginx/html放一个a.html并验证
http://127.0.0.1:81/a.html
注意事项
- Dockerfile大小写一定要注意,不要写错
- -v的映射目录要注意权限