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

用 Go 語(yǔ)言創(chuàng)建一個(gè) “Hello World” web 應(yīng)用
首先我們?yōu)?Go 應(yīng)用創(chuàng)建一個(gè)目錄,它會(huì)在瀏覽器中顯示 “Hello World”。創(chuàng)建一個(gè) web-app 目錄并使它成為當(dāng)前目錄。進(jìn)入 web-app 應(yīng)用目錄并編輯一個(gè)名為 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)
}
使用下面的命令運(yùn)行上面的 “Hello World” Go 程序。在瀏覽器中輸入 http://127.0.0.1:8080/World 測(cè)試,你會(huì)在瀏覽器中看到 “Hello World”。
root@demohost:~/web-app# PORT=8080 go run main.go
下一步是將上面的應(yīng)用在 docker 中容器化。因此我們會(huì)創(chuàng)建一個(gè) dockerfile 文件,它會(huì)告訴 docker 如何容器化我們的 web 應(yīng)用。
root@demohost:~/web-app# vim.tiny Dockerfile
# 得到最新的 golang docker 鏡像
FROM golang:latest
# 在容器內(nèi)部創(chuàng)建一個(gè)目錄來存儲(chǔ)我們的 web 應(yīng)用,接著使它成為工作目錄。
RUN mkdir -p /go/src/web-app
WORKDIR /go/src/web-app
# 復(fù)制 web-app 目錄到容器中
COPY . /go/src/web-app #
下載并安裝第三方依賴到容器中
RUN go-wrapper download RUN go-wrapper install
# 設(shè)置 PORT 環(huán)境變量
ENV PORT 8080
# 給主機(jī)暴露 8080 端口,這樣外部網(wǎng)絡(luò)可以訪問你的應(yīng)用
EXPOSE 8080
# 告訴 Docker 啟動(dòng)容器運(yùn)行的命令
CMD ["go-wrapper", "run"]
構(gòu)建/運(yùn)行容器
使用下面的命令構(gòu)建你的 Go web-app,你會(huì)在成功構(gòu)建后獲得確認(rèn)。
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
現(xiàn)在可以運(yùn)行我們的 web-app 了,可以執(zhí)行下面的命令。
root@demohost:~/web-app# docker run -p 8080:8080 --name="test" -d web-app 7644606b9af28a3ef1befd926f216f3058f500ffad44522c1d4756c576cfa85b
進(jìn)入 http://localhost:8080/World 瀏覽你的 web 應(yīng)用。你已經(jīng)成功容器化了一個(gè)可重復(fù)的/確定性的 Go web 應(yīng)用。使用下面的命令來啟動(dòng)、停止并檢查容器的狀態(tài)。
###列出所有容器
root@demohost:~/ docker ps -a
###使用 id 啟動(dòng)容器
root@demohost:~/ docker start CONTAINER_ID_OF_WEB_APP
###使用 id 停止容器
root@demohost:~/ docker stop CONTAINER_ID_OF_WEB_APP
重新構(gòu)建鏡像
假設(shè)你正在開發(fā) web 應(yīng)用程序并在更改代碼?,F(xiàn)在要在更新代碼后查看結(jié)果,你需要重新生成 docker 鏡像、停止舊鏡像并運(yùn)行新鏡像,并且每次更改代碼時(shí)都要這樣做。為了使這個(gè)過程自動(dòng)化,我們將使用 docker 卷在主機(jī)和容器之間共享一個(gè)目錄。這意味著你不必為在容器內(nèi)進(jìn)行更改而重新構(gòu)建鏡像。容器如何檢測(cè)你是否對(duì) web 程序的源碼進(jìn)行了更改?答案是有一個(gè)名為 “Gin” 的好工具 https://github.com/codegangsta/gin,它能檢測(cè)是否對(duì)源碼進(jìn)行了任何更改,然后重建鏡像/二進(jìn)制文件并在容器內(nèi)運(yùn)行更新過代碼的進(jìn)程。
要使這個(gè)過程自動(dòng)化,我們將編輯 Dockerfile 并安裝 Gin 將其作為入口命令來執(zhí)行。我們將開放 3030 端口(Gin 代理),而不是 8080。 Gin 代理將轉(zhuǎn)發(fā)流量到 web 程序的 8080 端口。
root@demohost:~/web-app# vim.tiny Dockerfile
# 得到最新的 golang docker 鏡像
FROM golang:latest
# 在容器內(nèi)部創(chuàng)建一個(gè)目錄來存儲(chǔ)我們的 web 應(yīng)用,接著使它稱為工作目錄。
RUN mkdir -p /go/src/web-app
WORKDIR /go/src/web-app
# 復(fù)制 web 程序到容器中
COPY . /go/src/web-app
# 下載并安裝第三方依賴到容器中
RUN go get github.com/codegangsta/gin
RUN go-wrapper download
RUN go-wrapper install
# 設(shè)置 PORT 環(huán)境變量
ENV PORT 8080
# 給主機(jī)暴露 8080 端口,這樣外部網(wǎng)絡(luò)可以訪問你的應(yīng)用
EXPOSE 3030
# 啟動(dòng)容器時(shí)運(yùn)行
Gin CMD gin run
# 告訴 Docker 啟動(dòng)容器運(yùn)行的命令
CMD ["go-wrapper", "run"]
現(xiàn)在構(gòu)建鏡像并啟動(dòng)容器:
root@demohost:~/web-app# docker build --rm -t web-app .
我們會(huì)在當(dāng)前 web 程序的根目錄下運(yùn)行 docker,并通過暴露的 3030 端口鏈接 CWD (當(dāng)前工作目錄)到容器中的應(yīng)用目錄下。
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(xiàn)在如果你改變了任何代碼,會(huì)在瀏覽器刷新后反映在你的瀏覽器中。
總結(jié)
就是這樣,我們的 Go web 應(yīng)用已經(jīng)運(yùn)行在 Ubuntu 16.04 Docker 容器中運(yùn)行了!你可以通過使用 Go 框架來快速開發(fā) API、網(wǎng)絡(luò)應(yīng)用和后端服務(wù),從而擴(kuò)展當(dāng)前的網(wǎng)絡(luò)應(yīng)用。
當(dāng)前題目:Docker中部署Go并使用
網(wǎng)站鏈接:http://www.dlmjj.cn/article/cdiccpo.html


咨詢
建站咨詢
