go-zero docker-compose 搭建课件服务(四):生成Dockerfile并在docker-compose中启动
https://github.com/liuyuede123/go-zero-courseware
目前只是创建好了courseware服务,这一章节我我们先在docker-compose跑通课件服务
.
├── courseware
│ ├── api
│ │ ├── courseware.api
│ │ ├── courseware.go
│ │ ├── etc
│ │ │ └── courseware.yaml
│ │ └── internal
│ │ ├── config
│ │ │ └── config.go
│ │ ├── handler
│ │ │ ├── coursewareaddhandler.go
│ │ │ ├── coursewaredeletehandler.go
│ │ │ ├── coursewaregethandler.go
│ │ │ ├── coursewareupdatehandler.go
│ │ │ └── routes.go
│ │ ├── logic
│ │ │ ├── coursewareaddlogic.go
│ │ │ ├── coursewaredeletelogic.go
│ │ │ ├── coursewaregetlogic.go
│ │ │ └── coursewareupdatelogic.go
│ │ ├── svc
│ │ │ └── servicecontext.go
│ │ └── types
│ │ └── types.go
│ ├── go.mod
│ ├── go.sum
│ └── rpc
│ ├── courseware
│ │ ├── courseware.pb.go
│ │ └── courseware_grpc.pb.go
│ ├── courseware.go
│ ├── courseware.proto
│ ├── coursewareclient
│ │ └── courseware.go
│ ├── etc
│ │ └── courseware.yaml
│ ├── internal
│ │ ├── config
│ │ │ └── config.go
│ │ ├── logic
│ │ │ ├── addlogic.go
│ │ │ ├── deletelogic.go
│ │ │ ├── getlogic.go
│ │ │ └── updatelogic.go
│ │ ├── server
│ │ │ └── coursewareserver.go
│ │ └── svc
│ │ └── servicecontext.go
│ └── model
│ ├── courseware.sql
│ ├── coursewaremodel.go
│ ├── coursewaremodel_gen.go
│ └── vars.go
└── user
├── api
│ └── user.api
└── rpc
└── user.proto
创建etcd Dockerfile
mkdir etcd
vim etcd/Dockerfile
Dockerfile中的内容,保存并退出
FROM bitnami/etcd:latest
LABEL maintainer="liuyuede123 <liufutianoppo@163.com>"
创建etcd-manage Dockerfile
mkdir etcd-manage
vim etcd-manage/Dockerfile
Dockerfile中的内容,保存并退出
FROM evildecay/etcdkeeper
LABEL maintainer="liuyuede123 <liufutianoppo@163.com>"
到courseware/api目录下生成Dockerfile
cd courseware/api
goctl docker -go courseware.go
FROM golang:alpine AS builder
LABEL stage=gobuilder
ENV CGO_ENABLED 0
ENV GOPROXY https://goproxy.cn,direct
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk update --no-cache && apk add --no-cache tzdata
WORKDIR /build
ADD go.mod .
ADD go.sum .
RUN go mod download
COPY . .
COPY api/etc /app/etc
RUN go build -ldflags="-s -w" -o /app/courseware api/courseware.go
FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/Shanghai
ENV TZ Asia/Shanghai
WORKDIR /app
COPY --from=builder /app/courseware /app/courseware
COPY --from=builder /app/etc /app/etc
CMD ["./courseware", "-f", "etc/courseware.yaml"]
到courseware/rpc目录下生成Dockerfile
cd ../rpc
goctl docker -go courseware.go
FROM golang:alpine AS builder
LABEL stage=gobuilder
ENV CGO_ENABLED 0
ENV GOPROXY https://goproxy.cn,direct
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk update --no-cache && apk add --no-cache tzdata
WORKDIR /build
ADD go.mod .
ADD go.sum .
RUN go mod download
COPY . .
COPY rpc/etc /app/etc
RUN go build -ldflags="-s -w" -o /app/courseware rpc/courseware.go
FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/Shanghai
ENV TZ Asia/Shanghai
WORKDIR /app
COPY --from=builder /app/courseware /app/courseware
COPY --from=builder /app/etc /app/etc
CMD ["./courseware", "-f", "etc/courseware.yaml"]
到项目根目录
touch docker-compose.yml
version: '3.5'
# 网络配置
networks:
backend:
driver: bridge
# 服务容器配置
services:
etcd: # 自定义容器名称
build:
context: etcd # 指定构建使用的 Dockerfile 文件
environment:
- TZ=Asia/Shanghai
- ALLOW_NONE_AUTHENTICATION=yes
- ETCD_ADVERTISE_CLIENT_URLS=http://etcd:2379
ports: # 设置端口映射
- "2379:2379"
networks:
- backend
restart: always
etcd-manage:
build:
context: etcd-manage
environment:
- TZ=Asia/Shanghai
ports:
- "7000:8080" # 设置容器8080端口映射指定宿主机端口,用于宿主机访问可视化web
depends_on: # 依赖容器
- etcd # 在 etcd 服务容器启动后启动
networks:
- backend
restart: always
courseware-rpc: # 自定义容器名称
build:
context: courseware # 指定构建使用的 Dockerfile 文件
dockerfile: rpc/Dockerfile
environment: # 设置环境变量
- TZ=Asia/Shanghai
privileged: true
ports: # 设置端口映射
- "9400:9400" # 课件服务rpc端口
stdin_open: true # 打开标准输入,可以接受外部输入
tty: true
networks:
- backend
restart: always # 指定容器退出后的重启策略为始终重启
courseware-api: # 自定义容器名称
build:
context: courseware # 指定构建使用的 Dockerfile 文件
dockerfile: api/Dockerfile
environment: # 设置环境变量
- TZ=Asia/Shanghai
privileged: true
ports: # 设置端口映射
- "8400:8400" # 课件服务api端口
stdin_open: true # 打开标准输入,可以接受外部输入
tty: true
networks:
- backend
restart: always # 指定容器退出后的重启策略为始终重启
然后项目根目录执行:
docker-compose up -d
Creating go-zero-courseware_courseware-rpc_1 ... done
Creating go-zero-courseware_etcd_1 ... done
Creating go-zero-courseware_courseware-api_1 ... done
Creating go-zero-courseware_etcd-manage_1 ... done
这时候课件服务就跑起来了。
然后测试下接口是否可用:
http://localhost:8400/api/courseware/get
{
"id": 1
}
http://localhost:8400/api/courseware/add
{
"name": "多媒体课件11",
"code": "CH500",
"type": 4
}
http://localhost:8400/api/courseware/update
{
"id" : 1,
"name": "多媒体课件1133",
"code": "CH500",
"type": 4
}
http://localhost:8400/api/courseware/delete
{
"id": 1
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章