嗨,在本教程中,我们将学习如何使用 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
新闻热点
疑难解答