写点什么

go-zero docker-compose 搭建课件服务(四):生成 Dockerfile

作者:六月的
  • 2022-10-19
    上海
  • 本文字数:3441 字

    阅读完需:约 1 分钟

0、转载

go-zero docker-compose 搭建课件服务(四):生成Dockerfile并在docker-compose中启动

0.1 源码地址

https://github.com/liuyuede123/go-zero-courseware

1、目前的目录文件结构

目前只是创建好了 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
复制代码

2、项目根目录下创建依赖的服务

创建 etcd Dockerfile


mkdir etcdvim etcd/Dockerfile
复制代码


Dockerfile 中的内容,保存并退出


FROM bitnami/etcd:latest
LABEL maintainer="liuyuede123 <liufutianoppo@163.com>"
复制代码


创建 etcd-manage Dockerfile


mkdir etcd-managevim etcd-manage/Dockerfile
复制代码


Dockerfile 中的内容,保存并退出


FROM evildecay/etcdkeeper
LABEL maintainer="liuyuede123 <liufutianoppo@163.com>"
复制代码

3、courseware api 和 rpc 生成 Dockerfile

到 courseware/api 目录下生成 Dockerfile


cd courseware/apigoctl docker -go courseware.go
复制代码


FROM golang:alpine AS builder
LABEL stage=gobuilder
ENV CGO_ENABLED 0ENV GOPROXY https://goproxy.cn,directRUN 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 downloadCOPY . .COPY api/etc /app/etcRUN 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.crtCOPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/ShanghaiENV TZ Asia/Shanghai
WORKDIR /appCOPY --from=builder /app/courseware /app/coursewareCOPY --from=builder /app/etc /app/etc
CMD ["./courseware", "-f", "etc/courseware.yaml"]
复制代码


到 courseware/rpc 目录下生成 Dockerfile


cd ../rpcgoctl docker -go courseware.go
复制代码


FROM golang:alpine AS builder
LABEL stage=gobuilder
ENV CGO_ENABLED 0ENV GOPROXY https://goproxy.cn,directRUN 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 downloadCOPY . .COPY rpc/etc /app/etcRUN 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.crtCOPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/ShanghaiENV TZ Asia/Shanghai
WORKDIR /appCOPY --from=builder /app/courseware /app/coursewareCOPY --from=builder /app/etc /app/etc
CMD ["./courseware", "-f", "etc/courseware.yaml"]
复制代码

4、创建 docker-compose.yml 文件

到项目根目录


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 ... doneCreating go-zero-courseware_etcd_1 ... doneCreating go-zero-courseware_courseware-api_1 ... doneCreating 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}
复制代码


用户头像

六月的

关注

还未添加个人签名 2019-07-23 加入

还未添加个人简介

评论

发布
暂无评论
go-zero docker-compose搭建课件服务(四):生成Dockerfile_Docker-compose_六月的_InfoQ写作社区