Get the App
SLTechnology News&Howtos  ›  Database  › 

How to build Golang Development Environment in Docker

Shulou Source: shulou.com Published: 2022-05-31 18:58:18 09月28日 Update

Today, I will talk to you about how to build the Golang development environment in Docker. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Get Docker image

We use the latest stable version of the official image golang:latest. Before obtaining the image, you have to make sure that the Docker client is running on your computer. If not, go to the official website and download it.

Download the image on dockerHub and use the command directly:

Docker pull golang

After downloading the image, run a container with the image:

Docker run-rm-it-name go-http-demo golang bash

The above command creates a container called go-http-demo with the mirror golang, and creates a Bash session in the container. The-- rm option specifies that the container is automatically removed when it exits.

After running the above command, we enter the running container and run go version to view the following go version:

[email protected]: / go# go version

Go version go1.13.6 linux/amd64

[email protected]: / go#

You can find the version of the golang image running container in https://hub.docker.com/_/golang according to your needs.

Run the Go project in the container

We use the source code of HTTPServer that supports graceful shutdown service written at the end of "learn more about writing a HTTP server in Go" and put it in a Docker container to serve host requests. The source code of HTTP Server is as follows. If you want to explore how the code is realized, just look at the article in the link.

Package mainimport ("context"fmt"log"net/http"os"os/signal"syscall") func main () {mux: = http.NewServeMux () mux.Handle ("/", & helloHandler {}) server: = & http.Server {Addr: ": 8080", Handler: mux,} / / create the system signal receiver done: = make (chan os.Signal) signal.Notify (done, os.Interrupt, syscall.SIGINT) Syscall.SIGTERM) go func () {

Tags: Container run command image project code directory faceted file service content port environment development source code computer and then host that is server Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Docker Microsoft NVidia MariaDB