https://studygolang.com/articles/12670
参考的这篇文章。踩了几个坑。
这里配上成功的流程:
1. 创建工程
mkdir helloWorldDocker
cd helloWorldDocker
2. 创建main.go文件
package main
import (
"fmt"
"net/http"
)
func main() {
fmt.Println("abc")
http.HandleFunc("/", homePage)
http.HandleFunc("/login", login)
_ = http.ListenAndServe(":80", nil)
}
func homePage(w http.ResponseWriter, r *http.Request) {
fmt.Println("Hello Docker")
}
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("Login Docker")
}
3 在helloWorldDocker下创建Dockerfile
FROM golang:latest
WORKDIR $GOPATH/src/helloWorldDocker
COPY . $GOPATH/src/helloWorldDocker
RUN go build .
EXPOSE 8080
ENTRYPOINT ["./helloWorldDocker"]
4 返回上级目录
cd ../
5 docker编译你的项目-注意docker的命名不能有大写
docker build -t hellow ./helloWorldDocker
Sending build context to Docker daemon 14.34kB
Step 1/6 : FROM golang:latest
---> 54e71dcafb7c
Step 2/6 : WORKDIR $GOPATH/src/helloWorldDocker
---> Using cache
---> fcf23a94eb82
Step 3/6 : COPY . $GOPATH/src/helloWorldDocker
---> 72aa02c9ebe4
Step 4/6 : RUN go build .
---> Running in 9ff4b85593b2
Removing intermediate container 9ff4b85593b2
---> d8a47e47f37a
Step 5/6 : EXPOSE 8080
---> Running in c41b4f3394c0
Removing intermediate container c41b4f3394c0
---> e8842a313901
Step 6/6 : ENTRYPOINT ["./hellow"]
---> Running in 6e5729ec5078
Removing intermediate container 6e5729ec5078
---> 7c045fb4a075
Successfully built 7c045fb4a075
Successfully tagged hellow:latest
6 docker 上运行项目
docker run -p 8080:80 hellow
abc
7 http访问
Login Docker
Hello Docker