使用 Dockerfile 来制作 nginx镜像 


[root@localhost nginx]# ls
Dockerfile nginx-1.16.1.tar.gz

[root@localhost nginx]# cat Dockerfile
FROM ansible/centos7-ansible
LABEL maintainer jonh wick
RUN yum install -y gcc gcc-c++ make \ #将依赖包和调试工具全部安装上,清理其环境。清理yum的缓存
openssl-devel pcre-devel gd-devel \
iproute net-tools telnet wget curl && \
yum clean all && \
rm -rf /var/cache/yum/*

ADD nginx-1.16.1.tar.gz /usr/src ##文件放在当前目录下,拷过去会自动解压

RUN cd /usr/src/nginx-1.16.1 && \
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module && \
make && make install && \
mkdir -p /usr/local/nginx/conf/vhost && \ # COPY nginx.conf/usr/local/nginx/conf/nginx.conf 如果你这里有自己的配置文件可以加上这句
rm -rf /usr/src/nginx-1.16.1 && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #软连接一个上海时区到当前时区

ENV PATH /usr/local/nginx/sbin:$PATH #设置环境变量
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["nginx","-g","daemon off;"]

#这里采用了nginx前台启动的命令,如果直接执行nginx这条命令,那么就是放在后台运行。这里加上了参数-g daemon off表示禁止后台启动。所以前台启动就是hang住容器。这样容器启动就不会被退出。


[root@localhost nginx]# docker build -t cenots7-nginx:v1 .

                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

运行Dockerfile 


[root@localhost nginx]# docker run -itd  -p 80:80 cenots7-nginx:v1
[root@localhost nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a5067663821 cenots7-nginx:v1 "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp nginx

[root@localhost nginx]# docker exec -it 3a5067663821 /bin/bash
[root@3a5067663821 nginx]# cd /usr/local/nginx/
[root@3a5067663821 nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@3a5067663821 nginx]# cd logs/
[root@3a5067663821 logs]# ls
access.log error.log nginx.pid
[root@3a5067663821 logs]# cat access.log
192.168.179.4 - - [09/Nov/2020:16:41:53 +0800] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"
192.168.179.4 - - [09/Nov/2020:16:41:54 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.179.99/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"

 

 浏览器测试


Docker Dockerfile制作Nginx镜像_html