Why Docker
The main goal with Docker is containerization. This is to provide a consistent environment for your application, independent of the host on which it is running. Imagine if you also had a scenario where you developed your application locally, it probably had many dependencies or packages, and there were even strict requirements on the specific versions of dependencies, and when the development process was complete, you wanted to deploy the application to a Web server. At this point you must make sure that all dependencies are installed correctly and of the same version, otherwise the application may crash and not work. If you want to deploy the application on another Web server, you must repeat the process from scratch. This scenario is where Docker comes in. For the host running our application, whether it’s a laptop or a Web server, the only thing we need to do is run a Docker container platform. From now on, you don’t have to worry about whether you’re using MacOS, Ubuntu, Arch or whatever. You only need to define an application once, and it can run anywhere, anytime.
Docker official documentation
Docker deployment example
Let’s start with the following code:
package main import ( "fmt" "net/http" "github.com/gin-gonic/gin" config "GoProject/ConfigClass" test "GoProject/TestClass" ) // func main() { // fmt.Print("hello world\n") // f, _ := os.Create("gin.testlog") // gin.DefaultWriter = io.MultiWriter(f) // r := gin.Default() // r.GET("/ping", Func (c *gin.Context) {// c.json (200, gin.H{// "message": "success", // "descprition": "This is a try ", // "vendor": "1", Struct {User string 'form:" User "JSON :" User" XML :" User" binding:"required"` Password string `form:"password" json:"password" xml:"password" binding:"required"` } func main() { Gin.ForceConsoleColor() test.log () config.configlog () fmt.println (" ") Router := gin. "manu", "password": "123"}) router.POST("/loginJSON", func(c *gin.Context) { var json Login if err := c.ShouldBindJSON(&json); err ! = nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if json.User ! = "manu" || json.Password ! = "123" { c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"}) return } c.JSON(http.StatusOK, gin.H{"status": "you are logged in"}) }) router.POST("/loginXML", func(c *gin.Context) { var xml Login if err := c.ShouldBindXML(&xml); err ! = nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if xml.User ! = "manu" || xml.Password ! = "123" { c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"}) return } c.JSON(http.StatusOK, gin.H{"status": "You are logged in"})}) // Example of binding an HTML form (user=manu&password=123) router.post ("/loginForm", Func (c *gin.Context) {var form Login // This will use the Content-type header to infer which dependency the binder will use. if err := c.ShouldBind(&form); err ! = nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if form.User ! = "manu" || form.Password ! = "123" { c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"}) return } c.JSON(http.StatusOK, gin.H{"status": "you are logged in"}) }) router.GET("/getJson", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "Congratulations", "id" : "123"}) FMT. Println (" request succeeded ")}) router. The Run (" : "8080)}Copy the code
Create a Docker image
An image contains everything you need to run your application — code or binaries, runtimes, dependencies, and any other file system objects you need. Or simply put, an image is everything that defines the application and how it runs.
To create a Docker image, you must specify the steps in the configuration file. This file is usually called Dockerfile by default. Now let’s write a Dockerfile, which looks like this:
FROM golang:alpine # Set ENV GO111MODULE=on CGO_ENABLED=0 GOOS= Linux GOARCH=amd64 # Move to working directory: /build WORKDIR /build # COPY the code to the container COPY.. # compile our code to the binary executable app RUN go build -o app. # Move to the /dist directory used to store the generated binaries RUN cp /build/app. RUN cp /build/app.Copy the code
Dockerfile parsing
We are using the base image Golang: Alpine to create our image. This is a basic image that we can access stored in the Docker repository, just like the image we're going to create. This image runs the Alpine Linux distribution, which is small in size and has Go built in, perfect for our use case. There are a number of publicly available Docker images, please see https://hub.docker.com/_/golang Env to set up the environment variables we need to use during compilation. The WORKDIR, COPY, and RUN commands do all this in comments, so it's easy to understand. Finally, we declare the service port, because this is the port on which our application listens and provides services externally. We also defined CMD ["/dist/app"] to be executed by default when we run the image.Copy the code
Build the mirror
CD Go to the project directory and run the following command to create an image named goproject_app. Note: Names must be lowercase
docker build . -t goproject_app
Copy the code
Now we are ready to mirror it, but so far it has done nothing. What we need to do next is run our image so that it can handle our request. A running image is called a container. Execute the following command to run the image:
docker run -p 8080:8080 goproject_app
Copy the code
The flag bit -p defines the port binding. Since the application in the container is running on port 8888, we bind it to host port 8080 as well. To bind to another port, use -p $HOST_PORT:8080. For example, -p 55:8080. Now can test the our web application is working correctly, the browser type http://127.0.0.1:8080/getJson can see the response content is as follows:
Go language project Demo