diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..28c7759 --- /dev/null +++ b/Dockerfile @@ -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"] diff --git a/build_img.sh b/build_img.sh new file mode 100644 index 0000000..001ab92 --- /dev/null +++ b/build_img.sh @@ -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 \ No newline at end of file diff --git a/hello.go b/hello.go index 8ac7076..ac74fb4 100644 --- a/hello.go +++ b/hello.go @@ -1,3 +1,18 @@ +package main + +import ( + "fmt" + "net/http" +) + +func helloHandler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello, World!") +} + func main() { - fmt.Println("Hello, World!") -} \ No newline at end of file + http.HandleFunc("/", helloHandler) + fmt.Println("Starting server at port 8080") + if err := http.ListenAndServe(":8080", nil); err != nil { + fmt.Println(err) + } +} diff --git a/hello.sh b/hello.sh index e5967d4..e69de29 100644 --- a/hello.sh +++ b/hello.sh @@ -1,7 +0,0 @@ -docker --version - -docker ps - -docker pull hello-world - -docker run hello-world \ No newline at end of file