deploy the first gin project using docker

1.encode a simple gin project and compile it to the running version of linux

#Modify the golang default setting to use the domestic available golang proxy server,otherwise the installation will cause a 443 error

go env -w GOPROXY=https://goproxy.cn

#Insall gin project

go get -u github.com/gin-gonic/gin

#Create file main.go 

package main
 
import (
  "net/http"
 
  "github.com/gin-gonic/gin"
)
 
func main() {
  r := gin.Default()
  r.GET("/hello", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{
      "message": "helloworld",
    })
  })
  r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

 # Run main.go in the terminal

go run ./main.go
curl http://127.0.0.1:8080/hello

2.usr docker to package the project

#Set up static complile to prevent the program from running correctly after docker has finished packing due to missing library files

GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build main.go

#The docker runtime evironment is intalled by default

#Create a new dockerfile file,write the configuration

#Create Dockerfile file

FROM scratch
MAINTAINER Hello
WORKDIR .
COPY . .
EXPOSE 8080
CMD ["./main"]

 #Run the following command in terminal

#Package the project into a hello image with docker

docker build -t hello:v2 .

#Run hello docker mirror

docker run -p 8080:8080 -d hello

#Test whether the docker container is running correctly

curl http://127.0.0.1:8080/hello

#Running result:

{"message":"helloworld"}

#Modify the docker warehouse name to registry.cn-hangzhou.aliyuncs.com/gosrc/godemo,

version number to v2

docker tag hello:v2 registry.cn-hangzhou.aliyuncs.com/gosrc/godemo:v2

3.upload the project to the docker server

#Log in to Aliyun Docker Registry

docker login --username=hi313*****@aliyun.com registry.cn-hangzhou.aliyuncs.com

#Push the image to Registry

docker push registry.cn-hangzhou.aliyuncs.com/gosrc/godemo:v2

 4.deploy the project on the target server using docker

#The target linux server already has installed docker by docker

#Log in Ali cloud docker warehouse on the target server

docker login --username=hi313*****@aliyun.com registry.cn-hangzhou.aliyuncs.com

#Pull the image from Registry

docker pull registry.cn-hangzhou.aliyuncs.com/gosrc/godemo:v2

#Run the docker container on the server to provide services

docker run -it -p 80:8080 -d registry.cn-hangzhou.aliyuncs.com/gosrc/godemo:v2

#Use the network public IP access service provided by service

http://112.123.256.245/hello

#Running result:

{"message":"helloworld"}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值