feat:构建和部署 Go 应用程序的 Docker镜像
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
- 新增 build_img.sh 脚本,用于构建 Go 应用程序和 Docker镜像 - 创建 Dockerfile,定义 Alpine Linux 基础镜像和应用程序配置 - 更新 hello.go,实现一个简单的 HTTP服务器 - 删除 hello.sh,移除不再需要的示例脚本
This commit is contained in:
parent
fe590df87b
commit
de44063645
|
|
@ -0,0 +1,19 @@
|
||||||
|
# 使用Alpine Linux作为基础镜像
|
||||||
|
FROM alpine:latest
|
||||||
|
|
||||||
|
# 设置作者标签
|
||||||
|
LABEL authors="18257"
|
||||||
|
|
||||||
|
# 安装必要的运行时依赖(如果有的话)
|
||||||
|
# 例如,如果你的应用程序需要CGO支持,你可能需要安装一些库
|
||||||
|
# RUN apk add --no-cache libc6-compat
|
||||||
|
|
||||||
|
# 将Go应用程序二进制文件复制到镜像中
|
||||||
|
# 假设你的Go应用程序二进制文件名为hello
|
||||||
|
COPY hello /app/hello
|
||||||
|
|
||||||
|
# 设置工作目录
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# 设置容器启动时运行的命令
|
||||||
|
ENTRYPOINT ["/app/hello"]
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
# 构建Go应用程序
|
||||||
|
CGO_ENABLED=0 GOOS=linux go build -o hello ./hello.go
|
||||||
|
|
||||||
|
# 构建Docker镜像
|
||||||
|
docker build -t hello-app .
|
||||||
|
|
||||||
|
# 清理生成的二进制文件
|
||||||
|
rm hello
|
||||||
|
|
||||||
|
# 运行Docker容器
|
||||||
|
docker run --rm -p 8080:8080 hello-app
|
||||||
19
hello.go
19
hello.go
|
|
@ -1,3 +1,18 @@
|
||||||
func main() {
|
package main
|
||||||
fmt.Println("Hello, World!")
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func helloHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintf(w, "Hello, World!")
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", helloHandler)
|
||||||
|
fmt.Println("Starting server at port 8080")
|
||||||
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue