新聞中心
Go 是一個開源的編程語言,它能讓構造簡單、可靠且高效的軟件變得容易。Go是從2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持開發(fā),后來還加入了Ian Lance Taylor, Russ Cox等人,并最終于2009年11月開源,在2012年早些時候發(fā)布了Go 1穩(wěn)定版本。現在Go的開發(fā)已經是完全開放的,并且擁有一個活躍的社區(qū)。

用 Go 語言創(chuàng)建一個 “Hello World” web 應用
首先我們?yōu)?Go 應用創(chuàng)建一個目錄,它會在瀏覽器中顯示 “Hello World”。創(chuàng)建一個 web-app 目錄并使它成為當前目錄。進入 web-app 應用目錄并編輯一個名為 main.go 的文件。
root@demohost:~# mkdir web-app
root@demohost:~# cd web-app/
root@demohost:~/web-app# vim.tiny main.go
package main
import (
"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 中容器化。因此我們會創(chuàng)建一個 dockerfile 文件,它會告訴 docker 如何容器化我們的 web 應用。
root@demohost:~/web-app# vim.tiny Dockerfile
# 得到最新的 golang docker 鏡像
FROM golang:latest
# 在容器內部創(chuàng)建一個目錄來存儲我們的 web 應用,接著使它成為工作目錄。
RUN mkdir -p /go/src/web-app
WORKDIR /go/src/web-app
# 復制 web-app 目錄到容器中
COPY . /go/src/web-app #
下載并安裝第三方依賴到容器中
RUN go-wrapper download RUN go-wrapper install
# 設置 PORT 環(huán)境變量
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 kB
Step 1 : FROM golang:latest
latest: Pulling from library/golang
386a066cd84a: Already exists
75ea84187083: Pull complete
88b459c9f665: Pull complete
a31e17eb9485: Pull complete
1b272d7ab8a4: Pull complete
eca636a985c1: Pull complete
08158782d330: Pull complete
Digest: sha256:02718aef869a8b00d4a36883c82782b47fc01e774d0ac1afd434934d8ccfee8c
Status: Downloaded newer image for golang:latest
---> 9752d71739d2
Step 2 : RUN mkdir -p /go/src/web-app
---> Running in 9aef92fff9e8
---> 49936ff4f50c
Removing intermediate container 9aef92fff9e8
Step 3 : WORKDIR /go/src/web-app
---> Running in 58440a93534c
---> 0703574296dd
Removing intermediate container 58440a93534c
Step 4 : COPY . /go/src/web-app
---> 82be55bc8e9f
Removing intermediate container cae309ac7757
Step 5 : RUN go-wrapper download
---> Running in 6168e4e96ab1
+ exec go get -v -d
---> 59664b190fee
Removing intermediate container 6168e4e96ab1
Step 6 : RUN go-wrapper install
---> Running in e56f093b6f03
+ exec go install -v
web-app
---> 584cd410fdcd
Removing intermediate container e56f093b6f03
Step 7 : ENV PORT 8080
---> Running in 298e2a415819
---> c87fd2b43977
Removing intermediate container 298e2a415819
Step 8 : EXPOSE 8080
---> Running in 4f639a3790a7
---> 291167229d6f
Removing intermediate container 4f639a3790a7
Step 9 : CMD go-wrapper run
---> Running in 6cb6bc28e406
---> b32ca91bdfe0
Removing intermediate container 6cb6bc28e406
Successfully built b32ca91bdfe0
現在可以運行我們的 web-app 了,可以執(zhí)行下面的命令。
root@demohost:~/web-app# docker run -p 8080:8080 --name="test" -d web-app 7644606b9af28a3ef1befd926f216f3058f500ffad44522c1d4756c576cfa85b
進入 http://localhost:8080/World 瀏覽你的 web 應用。你已經成功容器化了一個可重復的/確定性的 Go web 應用。使用下面的命令來啟動、停止并檢查容器的狀態(tài)。
###列出所有容器
root@demohost:~/ docker ps -a
###使用 id 啟動容器
root@demohost:~/ docker start CONTAINER_ID_OF_WEB_APP
###使用 id 停止容器
root@demohost:~/ docker stop CONTAINER_ID_OF_WEB_APP
重新構建鏡像
假設你正在開發(fā) web 應用程序并在更改代碼。現在要在更新代碼后查看結果,你需要重新生成 docker 鏡像、停止舊鏡像并運行新鏡像,并且每次更改代碼時都要這樣做。為了使這個過程自動化,我們將使用 docker 卷在主機和容器之間共享一個目錄。這意味著你不必為在容器內進行更改而重新構建鏡像。容器如何檢測你是否對 web 程序的源碼進行了更改?答案是有一個名為 “Gin” 的好工具 https://github.com/codegangsta/gin,它能檢測是否對源碼進行了任何更改,然后重建鏡像/二進制文件并在容器內運行更新過代碼的進程。
要使這個過程自動化,我們將編輯 Dockerfile 并安裝 Gin 將其作為入口命令來執(zhí)行。我們將開放 3030 端口(Gin 代理),而不是 8080。 Gin 代理將轉發(fā)流量到 web 程序的 8080 端口。
root@demohost:~/web-app# vim.tiny Dockerfile
# 得到最新的 golang docker 鏡像
FROM golang:latest
# 在容器內部創(chuàng)建一個目錄來存儲我們的 web 應用,接著使它稱為工作目錄。
RUN mkdir -p /go/src/web-app
WORKDIR /go/src/web-app
# 復制 web 程序到容器中
COPY . /go/src/web-app
# 下載并安裝第三方依賴到容器中
RUN go get github.com/codegangsta/gin
RUN go-wrapper download
RUN go-wrapper install
# 設置 PORT 環(huán)境變量
ENV PORT 8080
# 給主機暴露 8080 端口,這樣外部網絡可以訪問你的應用
EXPOSE 3030
# 啟動容器時運行
Gin CMD gin run
# 告訴 Docker 啟動容器運行的命令
CMD ["go-wrapper", "run"]
現在構建鏡像并啟動容器:
root@demohost:~/web-app# docker build --rm -t web-app .
我們會在當前 web 程序的根目錄下運行 docker,并通過暴露的 3030 端口鏈接 CWD (當前工作目錄)到容器中的應用目錄下。
root@demohost:~/web-app# docker run -p 3030:3030 -v `pwd`:/go/src/web-app --name="test" -d web-app
打開 http://localhost:3030/World, 你就能看到你的 web 程序了?,F在如果你改變了任何代碼,會在瀏覽器刷新后反映在你的瀏覽器中。
總結
就是這樣,我們的 Go web 應用已經運行在 Ubuntu 16.04 Docker 容器中運行了!你可以通過使用 Go 框架來快速開發(fā) API、網絡應用和后端服務,從而擴展當前的網絡應用。
網頁名稱:Docker中部署Go并使用
瀏覽路徑:http://m.fisionsoft.com.cn/article/cdiccpo.html


咨詢
建站咨詢
