Implementation of two basic images for Docker deployment of Go

Implementation of two basic images for Docker deployment of Go

1. golang:latest base image

mkdir gotest
touch main.go
touch Dockerfile

1. Example code

package main

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

func main() {
    http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
        fmt.Fprint(writer, "Hello World")
    })
    fmt.Println("3000!!")
    log.Fatal(http.ListenAndServe(":3000", nil))
}

2. Dockerfile configuration

#Source image FROM golang:latest
#Set the working directory WORKDIR $GOPATH/src/github.com/gotest
#Add the server's go project code to the docker container ADD . $GOPATH/src/github.com/gotest
#go build executable file RUN go build .
#Expose port EXPOSE 3000
#Finally run the docker command ENTRYPOINT ["./gotest"]

3. Packaging the image

docker build -t gotest .
  • The golang:latest compilation process actually builds a go development environment in the container.
  • This source image package is about 800M, which is relatively large.

2. alpine:latest base image

  1. The general process of using this image is to package the go program into a binary file on the Linux machine, then drop it into the apine environment and execute the compiled file.
  2. By default, Go's runtime environment variable CGO_ENABLED=1, which means cgo is started by default, allowing you to call C code from Go code. CGO can be disabled by setting CGO_ENABLED=0. So you need to execute: CGO_ENABLED=0 go build .
  3. This base image is only 13M in size, which is very small.
#Source image FROM alpine:latest
#Set the working directory WORKDIR $GOPATH/src/github.com/common
#Add the server's go project code to the docker container ADD . $GOPATH/src/github.com/common
#Expose port EXPOSE 3002
#Finally run the docker command ENTRYPOINT ["./common"]

Packaging image

docker build -t common .

This is the end of this article about the implementation of two basic images for Docker deployment of Go. For more relevant content about Docker deployment of Go images, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Methods and steps for deploying go projects based on Docker images

<<:  Vue implements the magnifying glass effect of tab switching

>>:  Vue Basics Introduction: Vuex Installation and Use

Recommend

What is jQuery used for? jQuery is actually a js framework

Introduction to jQuery The jQuery library can be ...

HTML+CSS merge table border sample code

When we add borders to table and td tags, double ...

Detailed tutorial on installing centos8 on VMware

CentOS official website address https://www.cento...

React internationalization react-i18next detailed explanation

Introduction react-i18next is a powerful internat...

503 service unavailable error solution explanation

1. When you open the web page, 503 service unavai...

Detailed installation process of Jenkins on Linux

Table of contents 1. Install JDK 2. Install Jenki...

Linux nohup command principle and example analysis

nohup Command When using Unix/Linux, we usually w...

Solution for FileZilla 425 Unable to connect to FTP (Alibaba Cloud Server)

Alibaba Cloud Server cannot connect to FTP FileZi...

JavaScript uses setTimeout to achieve countdown effect

In order to enhance the ability to write JavaScri...

Disable IE Image Toolbar

I just tried it on IE6, and it does show the toolb...