ᕕ( ᐛ )ᕗ Jimyag's Blog

Docker部署Go项目

记录自己用dockers部署Go项目

Go

项目文件结构如下

image-20220110233652763

首先实现一个测试demo main.go

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	fmt.Println("service start")
	router := gin.Default()
	router.GET("/ping", func(context *gin.Context) {
		context.JSON(http.StatusOK, gin.H{"message": "pong"})
		fmt.Println("service healthy")
	})
	router.Run(":9999")
}

Dockerfile

编写Dockerfile文件Dockerfile

FROM golang:1.17-alpine

WORKDIR /app
ADD * /app

ENV GO111MODULE=on \
        CGO_ENABLED=0 \
        GOOS=linux \
        GOARCH=amd64 \
    	GOPROXY="https://goproxy.io,direct"
RUN go mod download

RUN go build -o test_go .

EXPOSE 9999

CMD ./test_go

编写启动构建和启动脚本

构建脚本docker_build.sh

docker build -t my_test .

运行脚本 docker_run.sh

docker run -p 9999:9999 --name gin_test my_test

执行构建脚本

docker_build.sh

执行运行脚本

docker_run.sh

测试

在ApiPost进行测试

image-20220110235714740

#教程 #Docker #Gin