嗨,在本教程中,我們將學習如何使用 docker 部署 golang web 應用程序。 你可能已經知道,由于 golang 的高性能和可靠性,docker 是完全是用 golang 寫的。在我們詳細介紹之前,請確保你已經安裝了 docker 以及 golang 并對它們有基本了解。
關于 docker
Docker 是一個開源程序,它可以將應用及其完整的依賴包捆綁到一起,并打包為容器,與宿主機共享相同的 Linux 內核。另一方面,像 VMware 這樣的基于 hypervisor 的虛擬化操作系統容器提供了高級別的隔離和安全性,這是由于客戶機和主機之間的通信是通過 hypervisor 來實現的,它們不共享內核空間。但是硬件仿真也導致了性能的開銷,所以容器虛擬化誕生了,以提供一個輕量級的虛擬環境,它將一組進程和資源與主機以及其它容器分組及隔離,因此,容器內部的進程無法看到容器外部的進程或資源。
用 Go 語言創建一個 “Hello World” web 應用
首先我們為 Go 應用創建一個目錄,它會在瀏覽器中顯示 “Hello World”。創建一個 web-app 目錄并使它成為當前目錄。進入 web-app 應用目錄并編輯一個名為 main.go 的文件。
root@demohost:~# mkdir web-approot@demohost:~# cd web-app/root@demohost:~/web-app# vim.tiny main.gopackage mainimport ( "fmt" "net/http")func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello %s", r.URL.Path[1:])}func main() { http.HandleFunc("/World", handler) http.ListenAndServe(":8080", nil)}使用下面的命令運行上面的 “Hello World” Go 程序。在瀏覽器中輸入 http://127.0.0.1:8080/World 測試,你會在瀏覽器中看到 “Hello World”。
root@demohost:~/web-app# PORT=8080 go run main.go
下一步是將上面的應用在 docker 中容器化。因此我們會創建一個 dockerfile 文件,它會告訴 docker 如何容器化我們的 web 應用。
root@demohost:~/web-app# vim.tiny Dockerfile# 得到最新的 golang docker 鏡像FROM golang:latest# 在容器內部創建一個目錄來存儲我們的 web 應用,接著使它成為工作目錄。RUN mkdir -p /go/src/web-appWORKDIR /go/src/web-app# 復制 web-app 目錄到容器中COPY . /go/src/web-app# 下載并安裝第三方依賴到容器中RUN go-wrapper downloadRUN go-wrapper install# 設置 PORT 環境變量ENV PORT 8080# 給主機暴露 8080 端口,這樣外部網絡可以訪問你的應用EXPOSE 8080# 告訴 Docker 啟動容器運行的命令CMD ["go-wrapper", "run"]
構建/運行容器
使用下面的命令構建你的 Go web-app,你會在成功構建后獲得確認。
root@demohost:~/web-app# docker build --rm -t web-app .Sending build context to Docker daemon 3.584 kBStep 1 : FROM golang:latestlatest: Pulling from library/golang386a066cd84a: Already exists75ea84187083: Pull complete88b459c9f665: Pull completea31e17eb9485: Pull complete1b272d7ab8a4: Pull completeeca636a985c1: Pull complete08158782d330: Pull completeDigest: sha256:02718aef869a8b00d4a36883c82782b47fc01e774d0ac1afd434934d8ccfee8cStatus: Downloaded newer image for golang:latest---> 9752d71739d2Step 2 : RUN mkdir -p /go/src/web-app---> Running in 9aef92fff9e8---> 49936ff4f50cRemoving intermediate container 9aef92fff9e8Step 3 : WORKDIR /go/src/web-app---> Running in 58440a93534c---> 0703574296ddRemoving intermediate container 58440a93534cStep 4 : COPY . /go/src/web-app---> 82be55bc8e9fRemoving intermediate container cae309ac7757Step 5 : RUN go-wrapper download---> Running in 6168e4e96ab1+ exec go get -v -d---> 59664b190feeRemoving intermediate container 6168e4e96ab1Step 6 : RUN go-wrapper install---> Running in e56f093b6f03+ exec go install -vweb-app---> 584cd410fdcdRemoving intermediate container e56f093b6f03Step 7 : ENV PORT 8080---> Running in 298e2a415819---> c87fd2b43977Removing intermediate container 298e2a415819Step 8 : EXPOSE 8080---> Running in 4f639a3790a7---> 291167229d6fRemoving intermediate container 4f639a3790a7Step 9 : CMD go-wrapper run---> Running in 6cb6bc28e406---> b32ca91bdfe0Removing intermediate container 6cb6bc28e406Successfully built b32ca91bdfe0
新聞熱點
疑難解答