Use Docker in Go language projects

Containers bind a program and whatever it needs, including dependencies, tools, configuration files, and so on. In this way, the program is not affected by the environment, and the development environment and the generation environment are completely consistent in a real sense.

On the basis of containers, Docker carries out further encapsulation from file system, network interconnection to process isolation, which greatly simplifies the creation and maintenance of containers. Compared with traditional virtual machines, Docker also has many advantages, such as more efficient system resource utilization and faster startup time.

In this article, you’ll learn how to use Docker in a Go language project through a simple Go language project.

Golang and Docker logo

Create a simple Go language project

Let’s create a Go language project as an example. On the command line, type the following command to create a folder:

mkdir gdp
Copy the code

We will use the Go Module for dependency management. Go to the project root directory and initialize the Go Module:

cd gdp
go mod init github.com/linehk/gdp
Copy the code

We will create a simple Hello server. Create a new file named hello_server.go in the project root directory:

touch hello_server.go
Copy the code

The content of the document is as follows:

package main

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/gorilla/mux"
)

func handler(w http.ResponseWriter, r *http.Request) {
	query := r.URL.Query()
	name := query.Get("name")
	if name == "" {
		name = "Guest"
	}
	log.Printf("Received request for %s.\n", name)
	w.Write([]byte(fmt.Sprintf("Hello, %s! \n", name)))
}

func main(a) {
	r := mux.NewRouter()
	r.HandleFunc("/", handler)
	server := &http.Server{
		Handler:      r,
		Addr:         ": 8080",
		ReadTimeout:  10 * time.Second,
		WriteTimeout: 10 * time.Second,
	}
	log.Println("Starting Server.")
	iferr := server.ListenAndServe(); err ! =nil {
		log.Fatal(err)
	}
}
Copy the code

This project uses gorilla/ MUx packages to create HTTP routes (imported to show the Docker container binding dependencies) at localhost:8080.

Try compiling and running the project locally

Let’s first try compiling and running the project locally. To compile the project, enter the following command in the project root directory:

go build
Copy the code

The go build command will generate an executable named GDP. You can run the file like this:

./gdp
2020/08/19 21:33:49 Starting Server.
Copy the code

Our Hello server is now running and you can try using curl or some other tool to interact with it:

curl http://localhost:8080
Hello, Guest!
Copy the code
curl http://localhost:8080? name=sulinehk Hello, sulinehk!Copy the code

Write Dockerfile to define Docker image

Let’s write a Dockerfile for this project and create a file named Dockerfile in the root directory as follows:

# Pull the latest base image of the Go language
FROM golang:latest

Set /app to the current working directory in the container
WORKDIR /app

Copy files to current working directory
COPY.
Set the GOPROXY environment variable
ENV GOPROXY="https://goproxy.cn"

Download all dependencies
RUN go mod download

# build project
RUN go build -o gdp .

Expose port 8080
EXPOSE 8080

Execute the executable file
CMD ["./gdp"]
Copy the code

Build the image and run the container

An Image is the actual software distribution package that contains everything you need to run your application.

The container, built from an image, is a working example of a mirror, similar to the relationship between structure definitions and structure variables in Go.

  • Build an image:
docker build -t gdp .
Copy the code
  • Run container:
docker run -d -p 8080:8080 gdp
aa6a1afbe1b13ad0b0d1d656e157f762c5fe2229a8e0d95a025df26396ffc08f
Copy the code
  • Interacting with the server running inside the container:
curl http://localhost:8080
Hello, Guest!
Copy the code
curl http://localhost:8080? name=sulinehk Hello, sulinehk!Copy the code

Here are some other Docker commands:

Docker command

conclusion

As you can see, a well-defined Dockfile serves as a link between the preceding and the following throughout the process.

Dockerfile

Refer to the link

Docker – From getting started to practice

Docker Documentation