Dockerfile制作nginx业务镜像

一 工作目录

root@harbor-01:/data/k8s/dockerfile/web/nginx/nginx-app01# pwd
/data/k8s/dockerfile/web/nginx/nginx-app01

二 准备业务文件

2.1 nginx配置文件

查看代码
root@harbor-01:/data/k8s/dockerfile/web/nginx/nginx-app01# cat nginx.conf

user  nginx nginx;
worker_processes  auto;
daemon off;   #关闭后台运行
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format log_json  '{'
                        '"@timestamp":"$time_iso8601",'
                        '"@source":"$server_addr",'
                        '"@nginx_fields":{'
                            '"x_forwarded_for":"$http_x_forwarded_for",'
                            '"request":"$request",'
                            '"status":"$status",'
                            '"size":"$body_bytes_sent",'
                            '"http_referer":"$http_referer",'
                            '"client":"$remote_addr",'
                            '"responsetime":"$request_time",'
                            '"upstreamtime":"$upstream_response_time",'
                            '"upstreamaddr":"$upstream_addr",'
                            '"request_method":"$request_method",'
                            '"domain":"$host",'
                            '"url":"$uri",'
                            '"request_body":"$request_body",'
                            '"http_user_agent":"$http_user_agent"'
                        '}'
                    '}';
    access_log  logs/access.log  log_json;
    sendfile        on;
   
    keepalive_timeout  65;


    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

     

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

2.2 静态资源

root@harbor-01:/data/k8s/dockerfile/web/nginx/nginx-app01# cat webapp/index.html 
hello wgs

root@harbor-01:/data/k8s/dockerfile/web/nginx/nginx-app01# cat index.html 
hello nginx

三 编写Dockerfile

root@harbor-01:/data/k8s/dockerfile/web/nginx/nginx-app01# cat Dockerfile 
FROM 192.168.174.120/baseimages/nginx:1.20.2

ADD nginx.conf /usr/local/nginx/conf/nginx.conf
ADD webapp  /usr/local/nginx/html/webapp/
ADD index.html  /usr/local/nginx/html/index.html



EXPOSE 80 443

CMD ["nginx", "-g", "daemon off;"]

四 编写构建镜像脚本

root@harbor-01:/data/k8s/dockerfile/web/nginx/nginx-app01# cat build-command.sh 
#!/bin/bash
TAG=$1
docker build -t 192.168.174.120/baseimages/nginx-web1:${TAG} .
#echo "镜像构建完成,即将上传到harbor"
#sleep 1
#docker push 192.168.174.120/baseimages/nginx-web1:${TAG}
#echo "镜像上传到harbor完成"

五 构建镜像

root@harbor-01:/data/k8s/dockerfile/web/nginx/nginx-app01# ./build-command.sh v1.0
Sending build context to Docker daemon  7.168kB
Step 1/5 : FROM 192.168.174.120/baseimages/nginx:1.20.2
 ---> 9e15403c2475
Step 2/5 : ADD nginx.conf /usr/local/nginx/conf/nginx.conf
 ---> 7ccdf3526bff
Step 3/5 : ADD webapp  /usr/local/nginx/html/
 ---> 2fa2d565445d
Step 4/5 : EXPOSE 80 443
 ---> Running in 63c2b2439a49
Removing intermediate container 63c2b2439a49
 ---> c1104eda9ce9
Step 5/5 : CMD ["nginx", "-g", "daemon off;"]
 ---> Running in b8dc47613df1
Removing intermediate container b8dc47613df1
 ---> 2a266ab40b6d
Successfully built 2a266ab40b6d
Successfully tagged 192.168.174.120/baseimages/nginx-web1:v1.0

六 测试镜像

6.1 启动镜像

root@harbor-01:~# docker run -it --rm  -p 8001:80 192.168.174.120/baseimages/nginx-web1:v1.0

6.2 访问镜像

root@harbor-01:~# curl 192.168.174.120:8001
hello nginx

root@harbor-01:~# curl 192.168.174.120:8001/webapp/index.html
test webapp

6.3 查看访问日志

root@harbor-01:~# docker run -it --rm  -p 8001:80 192.168.174.120/baseimages/nginx-web1:v1.0 
{"@timestamp":"2021-12-13T12:04:35+08:00","@source":"172.17.0.2","@nginx_fields":{"x_forwarded_for":"-","request":"GET / HTTP/1.1","status":"200","size":"12","http_referer":"-","client":"192.168.174.120","responsetime":"0.000","upstreamtime":"-","upstreamaddr":"-","request_method":"GET","domain":"192.168.174.120","url":"/index.html","request_body":"-","http_user_agent":"curl/7.68.0"}}
{"@timestamp":"2021-12-13T12:05:02+08:00","@source":"172.17.0.2","@nginx_fields":{"x_forwarded_for":"-","request":"GET /webapp/index.html HTTP/1.1","status":"200","size":"12","http_referer":"-","client":"192.168.174.120","responsetime":"0.000","upstreamtime":"-","upstreamaddr":"-","request_method":"GET","domain":"192.168.174.120","url":"/webapp/index.html","request_body":"-","http_user_agent":"curl/7.68.0"}}

七 上传镜像到harbor

root@harbor-01:~# docker push 192.168.174.120/baseimages/nginx-web1:v1.0

 

posted @ 2021-12-13 12:07  小吉猫  阅读(231)  评论(0编辑  收藏  举报