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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!