首先,您需要使用systemctl start docker
或启动您的 docker
守护进程service docker start
,如果需要必要权限请使用sudo
。
然后我们将创建我们简单的 go HTTP
代码。
$ mkdir go-dockerfile && cd go-dockerfile
$ go mod init myapp
$ touch server.go
server.go:
package main
import (
"os"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func init() {
godotenv.Load()
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
router := gin.Default()
router.GET("/", func(c *gin.Context) {
c.String(200, "Hello World")
})
router.GET("/env", func(c *gin.Context) {
c.String(200, "Hello %s", os.Getenv("NAME"))
})
router.Run(":" + port)
}
我们将包含一个简单的gin
路由器和可选的.server.gogodotenv/path
将返回“Hello World”,/envpath
将返回“Hello ${NAME}”
。
有多种编写方式Dockerfile
,但我将使用不同的基础图像制作
3 个示例:golang docker、alpine、scratch
。
FROM golang:1.16-alpine
WORKDIR /project/go-docker/
# COPY go.mod, go.sum and download the dependencies
COPY go.* ./
RUN go mod download
# COPY All things inside the project and build
COPY . .
RUN go build -o /project/go-docker/build/myapp .
EXPOSE 8080
ENTRYPOINT [ "/project/go-docker/build/myapp" ]
在此Dockerfile,我们将其分为几个部分:
之后我们需要运行这个命令:
docker build -f Dockerfile -t test-go-docker:latest .
尝试运行此命令docker images
,例如:
450M
FROM golang:1.16-alpine as builder
WORKDIR /project/go-docker/
# COPY go.mod, go.sum and download the dependencies
COPY go.* ./
RUN go mod download
# COPY All things inside the project and build
COPY . .
RUN go build -o /project/go-docker/build/myapp .
FROM alpine:latest
COPY --from=builder /project/go-docker/build/myapp /project/go-docker/build/myapp
EXPOSE 8080
ENTRYPOINT [ "/project/go-docker/build/myapp" ]
与golang 官方基础镜像的区别:
这个Dockerfile
生成的镜像比之前小得多。
15.1M
FROM golang:1.16-alpine as builder
WORKDIR /project/go-docker/
# COPY go.mod, go.sum and download the dependencies
COPY go.* ./
RUN go mod download
# COPY All things inside the project and build
COPY . .
RUN go build -o /project/go-docker/build/myapp .
FROM scratch
COPY --from=builder /project/go-docker/build/myapp /project/go-docker/build/myapp
EXPOSE 8080
ENTRYPOINT [ "/project/go-docker/build/myapp" ]
对于最后一个 Dockerfile
,我们只将alpine
基础镜像更改为scratch
. Scratch
是一个空镜像,所以一旦容器运行,我们就无法执行到容器中,因为它没有 shell
命令。如下是输出的 docker images
。
9.52M
运行镜像docker run -d -p 8080:8080 test-go-docker:latest
,它将端口8080从容器转发到物理节点8080端口并可以访问http://localhost:8080
.
就我个人而言,我会选择第二个Dockerfile
。为什么?因为体积小而且它还有几个命令和一个shell
命令所以我们可以docker exec
进入容器并访问它。如果我们使用scratch
基础镜像,因为我们无法执行它,所以将很难调试正在运行的容器。
这篇关于 Docker
和 Go
编程的文章就到这里了,周五到来,希望你有一个愉快的周末:)。