Home >Operation and Maintenance >Docker >Docker deploys two basic images of go

Docker deploys two basic images of go

王林
王林forward
2020-06-15 17:50:173411browse

Docker deploys two basic images of go

1. golang:latest basic image

mkdir gotest
touch main.go
touch Dockerfile

Sample 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))
}

Dockerfile configuration

#源镜像
FROM golang:latest
#设置工作目录
WORKDIR $GOPATH/src/github.com/gotest
#将服务器的go工程代码加入到docker容器中
ADD . $GOPATH/src/github.com/gotest
#go构建可执行文件
RUN go build .
#暴露端口
EXPOSE 3000
#最终运行docker的命令
ENTRYPOINT ["./gotest"]

Packaging Image

docker build -t gotest .

golang:latest The compilation process is actually building a go development environment in the container. This source image package is about 800M, which is relatively large.

2. alpine:latest basic image

  • The general process of using this image is to package the go program on the Linux machine first into a binary file, then throw it into the apine environment, and execute the compiled file.

  • By default, Go's runtime environment variable CGO_ENABLED=1, which means cgo is started by default, allowing you to call C code in Go code. CGO is disabled by setting CGO_ENABLED=0. So you need to execute: CGO_ENABLED=0 go build. That's it.

  • This basic image package is only 13M, which is very small.

#源镜像
FROM alpine:latest
#设置工作目录
WORKDIR $GOPATH/src/github.com/common
#将服务器的go工程代码加入到docker容器中
ADD . $GOPATH/src/github.com/common
#暴露端口
EXPOSE 3002
#最终运行docker的命令
ENTRYPOINT ["./common"]

Packaging image:

docker build -t common .

Recommended tutorial: docker

The above is the detailed content of Docker deploys two basic images of go. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete