国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

詳解如何在 Docker 中設置 Go 并部署應用

2019-11-02 16:05:33
字體:
來源:轉載
供稿:網友

嗨,在本教程中,我們將學習如何使用 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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 红河县| 收藏| 金溪县| 平阴县| 天柱县| 临西县| 宁远县| 铜山县| 潍坊市| 罗平县| 察隅县| 海淀区| 大同市| 深泽县| 措勤县| 奇台县| 吉木乃县| 探索| 河池市| 土默特左旗| 沧州市| 九寨沟县| 五大连池市| 荔浦县| 忻州市| 新巴尔虎左旗| 玉林市| 海原县| 武胜县| 出国| 来宾市| 姜堰市| 普陀区| 普安县| 南丰县| 河池市| 梓潼县| 宝鸡市| 河池市| 太仓市| 育儿|