Hello folks,今天给大家介绍一下如何在 Dockerfile 中进行 Go 代码编写及运用(备注:此处暂不关注所部署的容器大小及性能问题)。现在,让我们准备测试应用程序,主要涉及 2 部分内容,具体包括:
1、一个简单的 HTTP 应用程序
2、一个类似 Sleep 的 CLI 工具
下面为一个完整的 Dockerfile 文件内容,如下所示:
# syntax=docker/dockerfile:1.4 # --- 此部定义构建相关内容 --- FROM golang:1.18 as builder WORKDIR / COPY <<EOF server.go # --- 此部分定义一个简单的 HTTP 程序 --- package main import ( "fmt" "log" "net/http" ) func hello(w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, "hello\n") } func headers(w http.ResponseWriter, req *http.Request) { for name, headers := range req.Header { for _, h := range headers { fmt.Fprintf(w, "%v: %v\n", name, h) } } } func main() { http.HandleFunc("/hello", hello) http.HandleFunc("/headers", headers) http.ListenAndServe(":8080", nil) } EOF COPY <<EOF sleep.go # --- 此部分定义一个简单的 Sleep cli 工具 --- package main import ( "log" "time" ) func main() { for { log.Println("What a fucking day !...") time.Sleep(2 * time.Second) } } EOF RUN CGO_ENABLED=0 go build -o server server.go RUN CGO_ENABLED=0 go build -o sleep sleep.go # --- 此处定义一个简单的测试 Demo --- FROM scratch COPY --from=builder /server /server COPY --from=builder /sleep /sleep CMD ["/server"]
编写完 Dockerfile 后,我们尝试对其进行编译,看是否能够操作成功,具体步骤及输出如下所示:
[leonli@192 luga ] % docker buildx build -t fucking-day-app . [+] Building 5.5s (17/17) FINISHED => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 1.17kB 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => resolve image config for docker.io/docker/dockerfile:1.4 1.3s => CACHED docker-image://docker.io/docker/dockerfile:1.4@sha256:91f386bc3ae6cd5585fbd02f811e295b4a7020c23c7691d686830bf6233e91ad 0.0s => [internal] load .dockerignore 0.0s => [internal] load build definition from Dockerfile 0.0s => [internal] load metadata for docker.io/library/golang:1.18 1.2s => [internal] preparing inline document 0.0s => CACHED [builder 1/6] FROM docker.io/library/golang:1.18@sha256:12d3995156cb0dcdbb9d3edb5827e4e8e1bf5bf92436bfd12d696ec997001a9a 0.0s => CACHED [internal] preparing inline document 0.0s => [builder 2/6] COPY <<EOF server.go 0.0s => [builder 3/6] COPY <<EOF sleep.go 0.0s => [builder 4/6] RUN CGO_ENABLED=0 go build -o server server.go 1.8s => [builder 5/6] RUN CGO_ENABLED=0 go build -o sleep sleep.go 0.3s => [stage-1 1/2] COPY --from=builder /server /server 0.0s => [stage-1 2/2] COPY --from=builder /sleep /sleep 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:3b0654a3f178886aebfecc7ec85b87861acf124a2776948686df5fda7a2b4577 0.0s => => naming to docker.io/library/fucking-day-app
基于上述的输出信息,我们发现没有异常信息抛出,貌似一切都很顺利。接下来,我们通过命令行查看一下所创建的 Image 文件,具体如下所示:
[leonli@192 luga ] % docker images REPOSITORY TAG IMAGE ID CREATED SIZE fucking-day-app latest 3b0654a3f178 54 seconds ago 7.85MB registry 2 c776ad7009a1 6 weeks ago 22.6MB rancher/k3d-tools 5.3.0 30d7dcea1d69 3 months ago 18.2MB rancher/k3d-proxy 5.3.0 0fec4b02df22 3 months ago 39.7MB rancher/k3s v1.22.6-k3s1 ee6507fdfdfe 3 months ago 155MB
可以看到,此时 Image 文件 已成功生成,然后,我们创建一个命名为 “fucking-day-app” 的容器并将其运行,具体如下:
[leonli@192 luga ] % docker create --name fucking-day-app fucking-day-app d9903f249897dadadb7e80cf0ac539b8d71abcd421c5b5f22a4d7a72c44d6f4e [leonli@192 luga ] % docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9903f249897 fucking-day-app "/server" 5 seconds ago Created fucking-day-app 56ce21236c38 rancher/k3d-proxy:5.3.0 "/bin/sh -c nginx-pr…" 4 weeks ago Exited (137) 2 weeks ago k3d-devops-cluster-serverlb 47f510e0045c rancher/k3s:v1.22.6-k3s1 "/bin/k3d-entrypoint…" 4 weeks ago Exited (0) 2 weeks ago k3d-devops-cluster-agent-1 8b2010a8f994 rancher/k3s:v1.22.6-k3s1 "/bin/k3d-entrypoint…" 4 weeks ago Exited (0) 2 weeks ago k3d-devops-cluster-agent-0 211d105c1d88 rancher/k3s:v1.22.6-k3s1 "/bin/k3d-entrypoint…" 4 weeks ago Exited (0) 2 weeks ago k3d-devops-cluster-server-0
[leonli@192 luga ] % docker start d9903f249897 d9903f249897 leonli@192 luga % docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9903f249897 fucking-day-app "/server" 38 seconds ago Up 2 seconds fucking-day-app c1d43fad83d0 fucking-app "/server" 6 hours ago Up 6 hours fucking-app
截止到此处,fucking-day-app 容器已正常启动,我们来看一下其后台日志,以验证是否存在异常错误,具体如下所示:
[leonli@192 luga ] % docker logs -f d9903f249897 2022/05/06 14:03:38 Starting HTTP server...
接下来,我们基于 Dockerfile 文件中所定义的测试 Demo 进行验证,按理来说,其应该正常会输出:“What a fucking day !...” 相关内容,如下所示:
[leonli@192 luga ] % docker exec -it fucking-day-app /sleep 2022/05/06 14:04:45 What a fucking day !... 2022/05/06 14:04:47 What a fucking day !... 2022/05/06 14:04:49 What a fucking day !... 2022/05/06 14:04:51 What a fucking day !... 2022/05/06 14:04:53 What a fucking day !... 2022/05/06 14:04:55 What a fucking day !... 2022/05/06 14:04:57 What a fucking day !... 2022/05/06 14:04:59 What a fucking day !... 2022/05/06 14:05:01 What a fucking day !... 2022/05/06 14:05:03 What a fucking day !... 2022/05/06 14:05:05 What a fucking day !... 2022/05/06 14:05:07 What a fucking day !... 2022/05/06 14:05:09 What a fucking day !... 2022/05/06 14:05:11 What a fucking day !... 2022/05/06 14:05:13 What a fucking day !... 2022/05/06 14:05:15 What a fucking day !... 2022/05/06 14:05:17 What a fucking day !... 2022/05/06 14:05:19 What a fucking day !... 2022/05/06 14:05:21 What a fucking day !... 2022/05/06 14:05:23 What a fucking day !... 2022/05/06 14:05:25 What a fucking day !... 2022/05/06 14:05:27 What a fucking day !...
与 Dockerfile 文件中所定义的内容一致,每间隔 2 s 输出 “What a fucking day !...” 相关内容。至此,一个简单的 Demo 先解析到此为止,希望大家有所收获!
Adiós !