golang測試專案
項目內容如下:
package mainimport ( "fmt" "net/http")func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8000", nil)}func handler(rw http.ResponseWriter, req *http.Request) { fmt.Fprintf(rw, "Hello World,%s", req.URL.Path[1:])}
基礎鏡像準備
自訂最小基礎鏡像
自訂基礎鏡像官方文檔看完後最還是google一下如何構建,tar cv --files-from /dev/null | docker import - scratch,使用docker images查看scratch的大小為0B,基礎鏡像已經構建成功,接下來把golang程式構建鏡像
構建golang鏡像
編寫Dockerfile
在當前項目目錄下建立Dockerfile檔案,如果使用IDE建議安裝plugins就能文法高亮及提示
FROM scratchADD main /mainEXPOSE 8000CMD ["/main"]
構建golang項目鏡像
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main .docker build -t "container/echo:latest" .docker run -itd --name echo -p 8000:8000 container/echo:latest# 查看容器是否啟動成功,沒成功使用docker logs查看日誌docker ps -a
mac os 啟動錯誤
standard_init_linux.go:190: exec user process caused "exec format error"
如果出現以上錯誤是因為你的golang在build時沒有指定為linux平台,因為Docker核心依賴於Linux開發的,所以在mac os啟動Docker時還是使用Boot2Docker拖起的