feat:构建和部署 Go 应用程序的 Docker镜像
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:
hsc 2025-04-26 19:19:09 +08:00
parent fe590df87b
commit de44063645
4 changed files with 49 additions and 9 deletions

19
Dockerfile Normal file
View File

@ -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"]

13
build_img.sh Normal file
View File

@ -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

View File

@ -1,3 +1,18 @@
func main() {
fmt.Println("Hello, World!")
package main
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)
}
}

View File

@ -1,7 +0,0 @@
docker --version
docker ps
docker pull hello-world
docker run hello-world