创建服务
package main
import (
"fmt"
"net/http"
)
func handlerHello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello docker")
}
func main() {
http.HandleFunc("/", handlerHello)
fmt.Println("run 8080")
http.ListenAndServe(":8080", nil)
}
dockerFIle
进入工作目录
/Users/zhangsan/Documents/GitHub/web-test
-> % tree
.
├── dockerfile
├── go.mod
└── main.go
0 directories, 3 files
docker内容
FROM golang:latest
ENV GO111MODULE=on \
CGO_ENABLED=1
WORKDIR /Users/zhangsan/Documents/GitHub/web-test
COPY . /Users/zhangsan/Documents/GitHub/web-test
RUN go build .
EXPOSE 8080
ENTRYPOINT ["./web-test"]
FROM 母镜像
MAINTAINER 维护者信息
WORKDIR工作目录
ADD 将文件复制到镜像中
RUN 执行操作
EXPOSE 监听端口
ENTRYPOINT 程序入口
项目目录执行
docker build -t testweb .
-> % docker build -t testweb .
Sending build context to Docker daemon 4.096kB
Step 1/7 : FROM golang:latest
---> 028d102f774a
Step 2/7 : ENV GO111MODULE=on CGO_ENABLED=1
---> Using cache
---> 3aec09f7569c
Step 3/7 : WORKDIR /Users/zhangsan/Documents/GitHub/web-test
---> Running in cf907f5da8e0
Removing intermediate container cf907f5da8e0
---> e111905b2f0c
Step 4/7 : COPY . /Users/zhangsan/Documents/GitHub/web-test
---> 5c06746de2df
Step 5/7 : RUN go build .
---> Running in 85ee6f5aa4fe
Removing intermediate container 85ee6f5aa4fe
---> 6cbafd7f5b58
Step 6/7 : EXPOSE 8080
---> Running in eefd150be5ea
Removing intermediate container eefd150be5ea
---> baf71f368cfa
Step 7/7 : ENTRYPOINT ["./web-test"]
---> Running in b75142748ae3
Removing intermediate container b75142748ae3
---> c88bc3a4ff72
Successfully built c88bc3a4ff72
Successfully tagged testweb:latest
#启动景象
-> % docker run -p 8080:8080 -d testweb
b0e843cc8e3063769c35905a4c0a01749d1c4129d1283accdc31d3e9aaac7b6a
验证
-> % curl http://localhost:8080/
hello docker%