新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

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

0、转载

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

创新互联是一家专注于成都网站制作、做网站、外贸营销网站建设与策划设计,兰西网站建设哪家好?创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:兰西等地区。兰西做网站价格咨询:13518219792

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 etcd
vim etcd/Dockerfile

Dockerfile中的内容,保存并退出

FROM bitnami/etcd:latest

LABEL maintainer="liuyuede123 "

创建etcd-manage Dockerfile

mkdir etcd-manage
vim etcd-manage/Dockerfile

Dockerfile中的内容,保存并退出

FROM evildecay/etcdkeeper

LABEL maintainer="liuyuede123 "

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

这时候课件服务就跑起来了。

然后测试下接口是否可用:

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
}

分享名称:go-zero docker-compose搭建课件服务(四):生成Dockerfile
文章来源:http://wjwzjz.com/article/dsoicds.html
在线咨询
服务热线
服务热线:028-86922220
TOP