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

Docker deploys two basic images of go

王林
Release: 2020-06-15 17:50:17
forward
3409 people have browsed it

Docker deploys two basic images of go

1. golang:latest basic image

mkdir gotest
touch main.go
touch Dockerfile
Copy after login

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))
}
Copy after login

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"]
Copy after login

Packaging Image

docker build -t gotest .
Copy after login

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"]
Copy after login

Packaging image:

docker build -t common .
Copy after login

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!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template