go-zero docker-compose 搭建課件服務(四):生成Dockerfile并在docker-compose中啟動
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設與策劃設計,蘭西網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設十年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:蘭西等地區(qū)。蘭西做網(wǎng)站價格咨詢:13518219792
https://github.com/liuyuede123/go-zero-courseware
目前只是創(chuàng)建好了courseware服務,這一章節(jié)我我們先在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
創(chuàng)建etcd Dockerfile
mkdir etcd
vim etcd/Dockerfile
Dockerfile中的內(nèi)容,保存并退出
FROM bitnami/etcd:latest
LABEL maintainer="liuyuede123 "
創(chuàng)建etcd-manage Dockerfile
mkdir etcd-manage
vim etcd-manage/Dockerfile
Dockerfile中的內(nèi)容,保存并退出
FROM evildecay/etcdkeeper
LABEL maintainer="liuyuede123 "
到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'
# 網(wǎng)絡配置
networks:
backend:
driver: bridge
# 服務容器配置
services:
etcd: # 自定義容器名稱
build:
context: etcd # 指定構(gòu)建使用的 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 # 指定構(gòu)建使用的 Dockerfile 文件
dockerfile: rpc/Dockerfile
environment: # 設置環(huán)境變量
- TZ=Asia/Shanghai
privileged: true
ports: # 設置端口映射
- "9400:9400" # 課件服務rpc端口
stdin_open: true # 打開標準輸入,可以接受外部輸入
tty: true
networks:
- backend
restart: always # 指定容器退出后的重啟策略為始終重啟
courseware-api: # 自定義容器名稱
build:
context: courseware # 指定構(gòu)建使用的 Dockerfile 文件
dockerfile: api/Dockerfile
environment: # 設置環(huán)境變量
- TZ=Asia/Shanghai
privileged: true
ports: # 設置端口映射
- "8400:8400" # 課件服務api端口
stdin_open: true # 打開標準輸入,可以接受外部輸入
tty: true
networks:
- backend
restart: always # 指定容器退出后的重啟策略為始終重啟
然后項目根目錄執(zhí)行:
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
}