一、Dockerfile文件内容:

FROM golang:1.18 
 
LABEL maintainer="fp" 
ENV GOPROXY https://goproxy.cn,direct
 
# 安装必要的软件包和依赖包
USER root                           
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/' /etc/apt/sources.list && \ 
    sed -i 's/security.debian.org/mirrors.tuna.tsinghua.edu.cn/' /etc/apt/sources.list && \
    sed -i 's/security-cdn.debian.org/mirrors.tuna.tsinghua.edu.cn/' /etc/apt/sources.list && \
    apt-get update && \               
    apt-get upgrade -y && \
    apt-get install -y --no-install-recommends \
    curl \
    zip \
    unzip \
    git \
    vim 
 
# 安装 goctl
USER root
RUN GOPROXY=https://goproxy.cn/,direct go install github.com/tal-tech/go-zero/tools/goctl@cli
 
# 安装 protoc
USER root
RUN curl -L -o /tmp/protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v3.19.1/protoc-3.19.1-linux-x86_64.zip && \
    unzip -d /tmp/protoc /tmp/protoc.zip && \
    mv /tmp/protoc/bin/protoc $GOPATH/bin
 
# 安装 protoc-gen-go
USER root
RUN go get -u google.golang.org/protobuf
 
# $GOPATH/bin添加到环境变量中
ENV PATH $GOPATH/bin:$PATH
 
# 清理垃圾
USER root
RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
    rm /var/log/lastlog /var/log/faillog
 
# 设置工作目录
WORKDIR /usr/src/code
 
EXPOSE 8000
EXPOSE 8001
EXPOSE 8002
EXPOSE 8003
EXPOSE 9000
EXPOSE 9001
EXPOSE 9002
EXPOSE 9003

二、运行镜像:

 docker build -t go-server .

三、VSCODE安装docker组件

搜索docker  管理镜像,容器

remote-containers  vscode通过工作区连接到容器。我没装

四、vscode进入到容器

五、拉入kotos框架

kotos是个微服务框架,可以继承gen支持web,  也可以运行服务器命令方式。这里不做过多介绍

apt-get update && apt-get -y install protobuf-compiler
go install github.com/go-kratos/kratos/cmd/kratos/v2@latest && kratos upgrad
kratos new demo
cd demo/ && go mod tidy
kratos run