Skip to content

Commit 0b3c598

Browse files
authored
Merge pull request #1 from vearne/feat/compose
add docker compose file
2 parents 572aec2 + 8d2f167 commit 0b3c598

File tree

4 files changed

+86
-14
lines changed

4 files changed

+86
-14
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,34 @@ autotest --config-file=${CONFIG_FILE}
2222
autotest --config-file=${CONFIG_FILE} --env-file=${ENV_FILE}
2323
```
2424
## Example
25-
### 1) start a fake api service
25+
### 1) start a fake http api service
26+
```
27+
cd docker-compose
28+
docker compose up -d
29+
```
30+
#### Add
31+
```
32+
curl -X POST 'http://localhost:8080/api/books' \
33+
--header 'Content-Type: application/json' \
34+
--data '{"title": "book3_title", "author": "book3_author"}'
35+
```
36+
37+
#### Delete
38+
```
39+
curl -X DELETE 'http://localhost:8080/api/books/1'
40+
```
41+
42+
#### Modify
43+
```
44+
curl -X PUT 'localhost:8080/api/books/3' \
45+
--header 'Content-Type: application/json' \
46+
--data '{"title": "book3_title", "author": "book3_author-2"}'
47+
```
48+
#### List
49+
```
50+
curl 'http://localhost:8080/api/books'
51+
```
52+
2653

2754
### 2) run automated test cases
2855
```

README_zh.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,33 @@ autotest --config-file=${CONFIG_FILE} --env-file=${ENV_FILE}
2121
```
2222

2323
## 示例
24-
### 1) 启动一个api服务
24+
### 1) 启动一个伪造的http api服务
25+
```
26+
cd docker-compose
27+
docker compose up -d
28+
```
29+
#### 添加
30+
```
31+
curl -X POST 'http://localhost:8080/api/books' \
32+
--header 'Content-Type: application/json' \
33+
--data '{"title": "book3_title", "author": "book3_author"}'
34+
```
35+
36+
#### 删除
37+
```
38+
curl -X DELETE 'http://localhost:8080/api/books/1'
39+
```
40+
41+
#### 修改
42+
```
43+
curl -X PUT 'localhost:8080/api/books/3' \
44+
--header 'Content-Type: application/json' \
45+
--data '{"title": "book3_title", "author": "book3_author-2"}'
46+
```
47+
#### 列表
48+
```
49+
curl 'http://localhost:8080/api/books'
50+
```
2551

2652
### 2) 运行自动化测试用例
2753
```

docker-compose/docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: '2'
2+
3+
services:
4+
httpapi:
5+
image: woshiaotian/httpapi:latest
6+
ports:
7+
- '8080:8080'

example/http_api/Dockerfile

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
# build
2-
FROM golang:1.21 as builder
1+
# 使用更新的 golang 基础镜像
2+
FROM golang:1.21 AS builder
33

4-
RUN mkdir -p /tmp/http_api
5-
COPY go.mod /tmp/http_api/go.mod
6-
COPY go.sum /tmp/http_api/go.sum
7-
COPY main.go /tmp/http_api/main.go
4+
# 设置工作目录
5+
WORKDIR /app
86

9-
WORKDIR /tmp/http_api
10-
RUN go build -o /bin/httpapi -ldflags "-s -w" /tmp/http_api
7+
# 复制 Go 模块文件和主文件
8+
COPY go.mod go.sum ./
9+
COPY main.go .
1110

11+
# 下载依赖
12+
RUN go mod download
1213

13-
FROM woshiaotian/simple-base-image:v0.1.8
14+
# 使用 CGO_DISABLED=0 来构建 Go 二进制文件
15+
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o httpapi .
1416

15-
WORKDIR /data
16-
COPY --from=builder /bin/httpapi /data/httpapi
17+
# 使用轻量级的 Alpine Linux 基础镜像
18+
FROM alpine:latest
1719

18-
CMD ["/data/httpapi"]
20+
# 设置工作目录
21+
WORKDIR /app
22+
23+
# 从构建阶段将二进制文件复制到最终镜像中
24+
COPY --from=builder /app/httpapi .
25+
26+
# 暴露应用运行的端口
27+
EXPOSE 8080
28+
29+
# 运行二进制文件
30+
CMD ["./httpapi"]

0 commit comments

Comments
 (0)