良许Linux教程网 干货合集 Docker 中部署 Go 并使用

Docker 中部署 Go 并使用

Go 是一个开源的编程语言,它能让构造简单、可靠且高效的软件变得容易。Go是从2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持开发,后来还加入了Ian Lance Taylor, Russ Cox等人,并最终于2009年11月开源,在2012年早些时候发布了Go 1稳定版本。现在Go的开发已经是完全开放的,并且拥有一个活跃的社区。

image-20211222122527912

用 Go 语言创建一个 “Hello World” web 应用

首先我们为 Go 应用创建一个目录,它会在浏览器中显示 “Hello World”。创建一个 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 中容器化。因此我们会创建一个 dockerfile 文件,它会告诉 docker 如何容器化我们的 web 应用。

root@demohost:~/web-app# vim.tiny Dockerfile

# 得到最新的 golang docker 镜像

FROM golang:latest

# 在容器内部创建一个目录来存储我们的 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 环境变量

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 了,可以执行下面的命令。

root@demohost:~/web-app# docker run -p 8080:8080 --name="test" -d web-app 7644606b9af28a3ef1befd926f216f3058f500ffad44522c1d4756c576cfa85b

进入 http://localhost:8080/World 浏览你的 web 应用。你已经成功容器化了一个可重复的/确定性的 Go web 应用。使用下面的命令来启动、停止并检查容器的状态。

###列出所有容器

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

重新构建镜像

假设你正在开发 web 应用程序并在更改代码。现在要在更新代码后查看结果,你需要重新生成 docker 镜像、停止旧镜像并运行新镜像,并且每次更改代码时都要这样做。为了使这个过程自动化,我们将使用 docker 卷在主机和容器之间共享一个目录。这意味着你不必为在容器内进行更改而重新构建镜像。容器如何检测你是否对 web 程序的源码进行了更改?答案是有一个名为 “Gin” 的好工具 https://github.com/codegangsta/gin,它能检测是否对源码进行了任何更改,然后重建镜像/二进制文件并在容器内运行更新过代码的进程。

要使这个过程自动化,我们将编辑 Dockerfile 并安装 Gin 将其作为入口命令来执行。我们将开放 3030 端口(Gin 代理),而不是 8080。 Gin 代理将转发流量到 web 程序的 8080 端口。

root@demohost:~/web-app# vim.tiny Dockerfile

# 得到最新的 golang docker 镜像

FROM golang:latest

# 在容器内部创建一个目录来存储我们的 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 环境变量

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 程序了。现在如果你改变了任何代码,会在浏览器刷新后反映在你的浏览器中。

总结

就是这样,我们的 Go web 应用已经运行在 Ubuntu 16.04 Docker 容器中运行了!你可以通过使用 Go 框架来快速开发 API、网络应用和后端服务,从而扩展当前的网络应用。

以上就是良许教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

img
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部