【实验环境】

1
2
3
4
5
root@ubuntu18:~# cat /etc/issue
Ubuntu 18.04.6 LTS \n \l

root@ubuntu18:~# docker --version
Docker version 24.0.2, build cb74dfc

【所需文件】

1、准备 alpine 镜像的国内加速地址

1
2
3
4
root@ubuntu18:/data/dockerfile/alpine-nginx# cat >> repositories << EOF
> https://mirrors.ustc.edu.cn/alpine/v3.5/main
> https://mirrors.ustc.edu.cn/alpine/v3.5/community
> EOF

2、准备镜像的构建和上传脚本

1
2
3
4
root@ubuntu18:/data/dockerfile/alpine-nginx# cat image-build.sh 
#!/bin/bash
docker build -t registry.cn-hangzhou.aliyuncs.com/wuhaolam/myserver:alping-nginx-v1 .
docker push registry.cn-hangzhou.aliyuncs.com/wuhaolam/myserver:alping-nginx-v1

3、准备 nginx 的源码包以及自定义的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
root@ubuntu18:/data/dockerfile/alpine-nginx# ls
index.html nginx-1.18.0.tar.gz nginx.conf

root@ubuntu18:/data/dockerfile/alpine-nginx# cat index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的静态网页</title>
</head>
<body>
<h1>欢迎来到我的网页!</h1>
<p>这是一个示例网页,用于展示标题、文字和图片的基本结构。</p>
<img src="https://cdn.jsdelivr.net/gh/wuhaolam/picgo_demo/img/QQ%E6%88%AA%E5%9B%BE20231129163610.png" alt="light belfry">
</body>
</html>

root@ubuntu18:/data/dockerfile/alpine-nginx# cat nginx.conf
user nginx nginx;
worker_processes 2;

error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid /apps/nginx/run/nginx.pid;


events {
worker_connections 1024;
use epoll;
accept_mutex on;
multi_accept on;
}


http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 30G;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
#listen 443 ssl;
#server_name harbor.wuhaolam.top;
#ssl_certificate /mnt/sdb/data/nginx/certs/harbor.wuhaolam.top.pem;
#ssl_certificate_key /mnt/sdb/data/nginx/certs/harbor.wuhaolam.top.key;

#charset koi8-r;

#access_log logs/host.access.log main;

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

location /myapp {
#proxy_pass http://myserver-tomcat-service:8080; # 两种写法都可,前一种相当于 root 方式 追加,后一种相当于 alias 替换
proxy_pass http://myserver-tomcat-service:8080/myapp/;
}

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

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

}

include /apps/nginx/conf/conf.d/*.conf;

}

【Dockerfile】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
FROM registry.cn-hangzhou.aliyuncs.com/wuhaolam/baseimage:alpine_3.5

COPY repositories /etc/apk/repositories
RUN apk update
RUN apk add vim bash gcc libgcc libc-dev libcurl libc-utils gzip zlib zlib-dev libnfs make pcre pcre2 pcre-dev zip unzip net-tools pstree wget libevent libevent-dev
RUN addgroup -S nginx && adduser -S nginx -G nginx -s /sbin/nologin

ADD nginx-1.18.0.tar.gz /usr/local/src/

RUN cd /usr/local/src/nginx-1.18.0 && ./configure --prefix=/apps/nginx/ --user=nginx --group=nginx && make && make install && ln -s /apps/nginx/sbin/nginx /usr/sbin/nginx && mkdir -p /apps/nginx/run/

ADD nginx.conf /apps/nginx/conf/
ADD index.html /apps/nginx/html/

RUN chown -R nginx.nginx /apps/nginx/

EXPOSE 80 8008

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

【结果展示】

1、运行镜像构建和上传脚本

1
2
3
4
5
root@ubuntu18:/data/dockerfile/alpine-nginx# bash image-build.sh

root@ubuntu18:/data/dockerfile/alpine-nginx# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/wuhaolam/myserver alping-nginx-v1 bd7fef5a30bf 25 seconds ago 199MB

2、运行镜像成为容器

1
2
root@ubuntu18:~# docker run -itd --restart=always -p 8008:80 --name=alpine-nginx-v1 registry.cn-hangzhou.aliyuncs.com/wuhaolam/myserver:alping-nginx-v1
a10c6920ab001dbc295103a8034364f9f7956aaca2ebcf253550b81396d98b43

3、在浏览器查看

image-20231212150412819