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