资源

xxl.zip nginx-1.22.0.tar.gz

创建目录sinatra,下载源码

mkdir sinatra
cd sinatra
wgt -O http://47.109.98.8:8090/upload/xxl.zip
wgt -O http://47.109.98.8:8090/upload/nginx-1.22.0.tar.gz

编写Dockerfile构建镜像

apk安装nginx(不推荐)

#nginx.conf
server {
    # 监听在 80端口
    listen       80;
    #服务域名,当前随意填写
    server_name seapigx.icu;
    #指定字符编码
    charset  utf-8;
    #指定服务访问日志
    access_log  /var/log/nginx/game.access.log  main;

    # 定义通用规则,指定服务目录
    location / {
        #指定服务根目录
        root   /var/lib/nginx/html;
        #指定索引页面类型
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
#Dockerfile
FROM alpine:latest
MAINTAINER seapigx.icu

# 更改apk源为阿里云源
RUN sed  -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g'  /etc/apk/repositories

# 更新包管理器并安装nginx,同时清理缓存
RUN apk update && apk add nginx

# 复制nginx配置文件
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./xxl /var/lib/nginx/html

# 暴露nginx默认端口
EXPOSE 80

# 启动nginx
CMD ["nginx", "-g", "daemon off;"]

源码安装nginx

#目录结构
[root@bolog sinatra]# ls
Dockerfile  nginx-1.22.0.tar.gz  nginx.conf  xxl  xxl.zip
# nginx.conf
# 使用 nobody 用户运行 nginx 进程
user  nobody;

# 工作进程数,通常设置为 CPU 核心数
worker_processes  1;

# 错误日志文件及其日志级别
error_log  logs/error.log  notice;

# nginx 进程 ID 文件位置
pid        logs/nginx.pid;

events {
    # 每个工作进程的最大连接数
    worker_connections  1024;
}

http {
    # 包含 mime.types 文件,用于映射文件扩展名到 MIME 类型
    include       mime.types;

    # 默认 MIME 类型
    default_type  application/octet-stream;

    # 定义日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    # 开启高效文件传输模式
    sendfile        on;

    # 防止网络阻塞
    tcp_nopush      on;

    # 保持连接超时时间
    keepalive_timeout  20;

    # 启用 gzip 压缩
    gzip  on;

    server {
        # 监听 80 端口
        listen       80;

        # 服务器名称
        server_name  seapigx.icu;

        # 设置字符集
        charset utf-8;

        # 访问日志文件及其使用的日志格式
        access_log  logs/game.access.log  main;

        location / {
            # 网站根目录
            root   /usr/local/nginx/html;

            # 默认索引文件
            index  index.html index.htm;
        }
    }
}
# Dockerfile
FROM alpine:latest
MAINTAINER "seapigx.icu"
COPY nginx-1.22.0.tar.gz   /tmp
RUN    sed  -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g'  /etc/apk/repositories  && \
       cd   /tmp  && \
       tar  -xf  nginx-1.22.0.tar.gz && \
       cd   nginx-1.22.0 && \
       apk  add  --no-cache make gcc  libgcc libc-dev zlib-dev  pcre-dev openssl-dev && \
       ./configure --prefix=/usr/local/nginx  --with-http_ssl_module \
          --with-http_auth_request_module --with-http_random_index_module && \
       make  -j2  && make install && \
       apk del make  && \
       rm  -rf  /tmp/*   /var/cache/apk/*
COPY   nginx.conf   /usr/local/nginx/conf/nginx.conf
COPY   xxl         /usr/local/nginx/html

WORKDIR   /usr/local/nginx/html
CMD    ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]                                                           

镜像构建

[root@bolog sinatra]# docker build -t xxl-alpine-src:v1 .
[+] Building 174.5s (11/11) FINISHED                                                               docker:default
 => [internal] load build definition from Dockerfile                                                         0.0s
 => => transferring dockerfile: 834B                                                                         0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                             3.3s
 => [internal] load .dockerignore                                                                            0.0s
 => => transferring context: 2B                                                                              0.0s
 => [1/4] FROM docker.io/library/alpine:latest@sha256:0a4eaa0eecf5f8c050e5bba433f58c052be7587ee8af3e8b3910e  2.9s
 => => resolve docker.io/library/alpine:latest@sha256:0a4eaa0eecf5f8c050e5bba433f58c052be7587ee8af3e8b3910e  0.0s
 => => sha256:0a4eaa0eecf5f8c050e5bba433f58c052be7587ee8af3e8b3910ef9ab5fbe9f5 1.85kB / 1.85kB               0.0s
 => => sha256:eddacbc7e24bf8799a4ed3cdcfa50d4b88a323695ad80f317b6629883b2c2a78 528B / 528B                   0.0s
 => => sha256:324bc02ae1231fd9255658c128086395d3fa0aedd5a41ab6b034fd649d1a9260 1.47kB / 1.47kB               0.0s
 => => sha256:c6a83fedfae6ed8a4f5f7cbb6a7b6f1c1ec3d86fea8cb9e5ba2e5e6673fde9f6 3.62MB / 3.62MB               2.7s
 => => extracting sha256:c6a83fedfae6ed8a4f5f7cbb6a7b6f1c1ec3d86fea8cb9e5ba2e5e6673fde9f6                    0.1s
 => [internal] load build context                                                                            0.0s
 => => transferring context: 3.51kB                                                                          0.0s
 => [2/6] COPY nginx-1.22.0.tar.gz   /tmp                                                                    0.0s
 => [3/6] RUN    sed  -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g'  /etc/apk/repositories  &&        170.5s
 => [4/6] COPY   nginx.conf   /usr/local/nginx/conf/nginx.conf                                               0.0s 
 => [5/6] COPY   xxl         /usr/local/nginx/html                                                           0.0s 
 => [6/6] WORKDIR   /usr/local/nginx/html                                                                    0.0s 
 => exporting to image                                                                                       0.6s 
 => => exporting layers                                                                                      0.6s 
 => => writing image sha256:5d8f65081b648579cdcd1d3e7060bee6f53fd30d0923e34656b2b1a030584dcc                 0.0s 
 => => naming to docker.io/library/xxl-alpine-src:v1    

检查镜像并运行容器

[root@bolog sinatra]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
xxl-alpine-src   v1        5d8f65081b64   13 minutes ago   176MB
nginx            v1        ce6cde96bb94   12 hours ago     46.2MB
nginx            alpine    1ae23480369f   5 weeks ago      43.2MB
[root@bolog sinatra]# docker run  -dt  --name game -p80:80 xxl-alpine-src:v1
bea8055b95df32275efb260af69d1417d8ca9d24b819237734e3ebd5f02f3e69
[root@bolog sinatra]# docker ps
CONTAINER ID   IMAGE               COMMAND                   CREATED         STATUS         PORTS                               NAMES
bea8055b95df   xxl-alpine-bin:v1   "/usr/local/nginx/sb…"   3 seconds ago   Up 2 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   game