diff --git a/maker/golang/Dockerfile b/maker/golang/Dockerfile new file mode 100644 index 0000000..ded7bc0 --- /dev/null +++ b/maker/golang/Dockerfile @@ -0,0 +1,35 @@ +# ============================================ +# ip2region xdb maker - Golang version +# Multi-stage build, resulting in an image of approximately 10 MB. +# ============================================ + +FROM golang:1.22-alpine AS builder + +WORKDIR /build + +COPY . ./ + +# Compile a static binary (disable CGO for fully static linking) +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o xdb_maker + +FROM alpine:3.19 + +RUN apk --no-cache add ca-certificates tzdata && \ + cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ + echo "Asia/Shanghai" > /etc/timezone + +# Create a non-root user. +RUN adduser -D -g '' appuser + +WORKDIR /app + +# Copy binaries from the build stage. +COPY --from=builder /build/xdb_maker /app/ + +RUN mkdir -p /app/data && chown -R appuser:appuser /app + +USER appuser + +ENTRYPOINT ["/app/xdb_maker"] + +CMD ["--help"] diff --git a/maker/golang/README.md b/maker/golang/README.md index f4aecec..36a0ab2 100644 --- a/maker/golang/README.md +++ b/maker/golang/README.md @@ -156,3 +156,34 @@ For example: use the source files under data to bench test the xdb files in data *Please note that the `src` file used for the bench test must be the same as the source file used to generate the xdb*. If an error occurs during execution, it will stop immediately. You can also execute with the `--ignore-error=true` parameter to ignore errors and view the failed statistics at the end. + + +# Docker + +```bash +# Build the image (run in the maker/golang directory). +cd ip2region/maker/golang +docker build -t ip2region-maker . + +# Generate IPv4 xdb +docker run --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + gen --src=/app/data/ipv4_source.txt \ + --dst=/app/data/ip2region_v4.xdb \ + --version=ipv4 + +# Generate IPv6 xdb +docker run --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + gen --src=/app/data/ipv6_source.txt \ + --dst=/app/data/ip2region_v6.xdb \ + --version=ipv6 + +# Interactive IPv4 Query +docker run -it --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + search --db=/app/data/ip2region_v4.xdb + +# Bench test IPv4 +docker run --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + bench --db=/app/data/ip2region_v4.xdb \ + --src=/app/data/ipv4_source.txt \ + --version=ipv4 +``` diff --git a/maker/golang/README_zh.md b/maker/golang/README_zh.md index 63015e3..8709059 100644 --- a/maker/golang/README_zh.md +++ b/maker/golang/README_zh.md @@ -5,16 +5,18 @@ # 程序编译 通过如下方式编译得到 xdb_maker 可执行程序: + ``` -# 切换到golang maker 根目录 +# 切换到 golang maker 根目录 make ``` -编译成功后会在当前目录生成一个 xdb_maker 的可执行文件 +编译成功后会在当前目录生成一个 xdb_maker 的可执行文件 # `xdb` 数据生成 通过 `xdb_maker gen` 命令生成 ip2region.xdb 二进制文件: + ``` ./xdb_maker gen [command options] options: @@ -26,6 +28,7 @@ options: ``` 例如,使用默认的仓库 data/ 下默认的原始数据生成生成 xdb 文件到当前目录: + ```bash # ipv4 ./xdb_maker gen --src=../../data/ipv4_source.txt --dst=./ip2region_v4.xdb --version=ipv4 @@ -35,10 +38,10 @@ options: 生成过程中数据字段自定义请参考 [xdb-文件生成#自定义数据字段](https://ip2region.net/doc/data/xdb_make#field-list) - # `xdb` 数据查询 通过 `xdb_maker search` 命令来测试查询输入的 ip: + ``` ➜ golang git:(v2.0_xdb) ✗ ./xdb_maker search ./xdb_maker search [command options] @@ -47,6 +50,7 @@ options: ``` 例如,使用自带的 xdb 文件来运行查询测试: + ```bash # ipv4 ./xdb_maker search --db=../../data/ip2region_v4.xdb @@ -78,6 +82,7 @@ ip2region>> 240e:3b7:3273:51d0:13f9:bf0:3db1:aa3f # `xdb` 数据编辑 通过 `xdb_maker edit` 命令来编辑原始的 IP 数据: + ``` ./xdb_maker edit [command options] options: @@ -86,6 +91,7 @@ options: ``` 例如,使用编辑器打开 `./data/ipv4_source.txt` 会看到如下的操作面板: + ```bash ./xdb_maker edit --src=../../data/ipv4_source.txt --version=ipv4 init the editor from source @ `../../data/ipv4_source.txt` ... @@ -101,6 +107,7 @@ editor>> ``` 通过 `put` 命令修改指定 IP 段的定位信息,例如: + ```bash editor>> put 36.132.128.0|36.132.147.255|中国|黑龙江省|哈尔滨市|移动|CN Put(36.132.128.0|36.132.147.255|中国|黑龙江省|哈尔滨市|移动): Ok, with 1 deletes and 2 additions @@ -108,6 +115,7 @@ Put(36.132.128.0|36.132.147.255|中国|黑龙江省|哈尔滨市|移动): Ok, wi ``` 通过 `put_file` 命令从文件中批量载入修改,文件中的 IP 段不需要像 ./data/ipvx_source.txt 中的数据那么严格,不需要前后连续,不同 IP 段有重叠也没关系,编辑器会自动分析处理,例如: + ```bash *editor>> put_file ../../data/sample/ip.test.txt PutFile(../../data/sample/ip.test.txt): Ok, with 25 deletes and 25 additions @@ -115,6 +123,7 @@ PutFile(../../data/sample/ip.test.txt): Ok, with 25 deletes and 25 additions ``` 通过 `save` 命令保存修改,保存成功后,再通过上面的命令从修改后的原始 IP 文件重新生成 xdb 即可: + ```bash *editor>> save all segments saved to ../../data/ipv4_source.txt @@ -124,6 +133,7 @@ editor>> # bench 测试 如果你自主生成了 `xdb` 文件,请确保运行如下的 `xdb_maker bench` 命令来确保生成的的 `xdb` 文件的正确性: + ``` ./xdb_maker bench [command options] options: @@ -135,6 +145,7 @@ options: ``` 例如:使用 data 下的源文件来 bench 测试 data 的 xdb 文件: + ```bash # ipv4 ./xdb_maker bench --db=../../data/ip2region_v4.xdb --src=../../data/ipv4_source.txt --version=ipv4 @@ -142,5 +153,37 @@ options: #ipv6 ./xdb_maker bench --db=../../data/ip2region_v6.xdb --src=../../data/ipv6_source.txt --version=ipv6 ``` + *请注意 bench 测试使用的 `src` 文件需要是对应的生成 xdb 的源文件相同*。 如果运行过程中有错误会立马停止运行,也可以执行 --ignore-error=true 参数来忽略错误,在最后看 failed 的统计结果。 + + +# Docker + +```bash +# 构建镜像(在 maker/golang 目录下执行) +cd ip2region/maker/golang +docker build -t ip2region-maker . + +# 生成 IPv4 xdb +docker run --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + gen --src=/app/data/ipv4_source.txt \ + --dst=/app/data/ip2region_v4.xdb \ + --version=ipv4 + +# 生成 IPv6 xdb +docker run --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + gen --src=/app/data/ipv6_source.txt \ + --dst=/app/data/ip2region_v6.xdb \ + --version=ipv6 + +# 交互式查询 IPv4 +docker run -it --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + search --db=/app/data/ip2region_v4.xdb + +# Bench 测试 IPv4 +docker run --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + bench --db=/app/data/ip2region_v4.xdb \ + --src=/app/data/ipv4_source.txt \ + --version=ipv4 +```