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

slowquery發表於2022-08-29

0、轉載

go-zero docker-compose 搭建課件服務(四):生成Dockerfile並在docker-compose中啟動

0.1原始碼地址

github.com/liuyuede123/go-zero-cou...

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 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>"

3、courseware api和rpc生成Dockerfile

到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"]

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 ... done
Creating go-zero-courseware_etcd_1           ... done
Creating go-zero-courseware_courseware-api_1 ... done
Creating go-zero-courseware_etcd-manage_1    ... done

這時候課件服務就跑起來了。

然後測試下介面是否可用:

```json
localhost:8400/api/courseware/get
{
“id”: 1
}
localhost:8400/api/courseware/add
{
“name”: “多媒體課件11”,
“code”: “CH500”,
“type”: 4
}
localhost:8400/api/courseware/updat...
{
“id” : 1,
“name”: “多媒體課件1133”,
“code”: “CH500”,
“type”: 4
}
localhost:8400/api/courseware/delet...
{
“id”: 1
}

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章