设置如何进入Docker并部署应用程序

时间:2020-03-05 15:25:25  来源:igfitidea点击:

嗨,在本教程中,我们将学习如何使用Docker部署Golang Web应用程序。
我们可能已经意识到由于其高性能和可靠性,我们可能已经意识到Docker完全编写的Golang。
在我们详细潜水之前,请确保已安装Docker和Golang并基本地了解它们。

关于Docker.

Docker是一个开源程序,使Linux应用程序能够与完整的依赖项捆绑在一起,并打包为共享同一主机操作系统内核的容器。
另一方面,基于超遮阳板的虚拟化操作系统容器如VMware提供了高水平的隔离和安全性,因为客户端和主机之间的通信是通过超遮阳板完成的,因为它们不共享内核空间。
由于硬件仿真,这导致性能开销。
因此,容器虚拟化出生,可确保轻量级虚拟环境,该虚拟环境组群体和隔离主机以及其他容器的一组进程和资源。
容器内部的进程无法在容器外看到进程或者资源。

Go创建"Hello World"Web应用程序

让我们首先为Go Apps创建一个目录,该目录将在浏览器中显示"Hello World"。
创建一个Web应用程序目录并使IT当前目录。
导航到Web应用程序目录并按名称"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 /来测试它。
我们将在浏览器中看到"Hello World"消息。

root@demohost:~/web-app# PORT=8080 go run main.go

我们的下一步是将上面的应用程序集装在Docker中。
因此,我们将创建一个dockerfile,它将告诉Docker如何将我们的Web应用程序容纳。

root@demohost:~/web-app# vim.tiny Dockerfile
# Get latest golang docker image.
FROM golang:latest
# Create a directory inside the container to store our web-app and then make it working directory.
RUN mkdir -p /go/src/web-app
WORKDIR /go/src/web-app
# Copy the web-app directory into the container.
COPY . /go/src/web-app
# Download and install third party dependencies into the container.
RUN go-wrapper download
RUN go-wrapper install
# Set the PORT environment variable
ENV PORT 8080
# Expose port 8080 to the host so that outer-world can access your application
EXPOSE 8080
# Tell Docker what command to run when the container starts
CMD ["go-wrapper", "run"]

构建/运行容器

使用以下命令构建Go Web应用程序,我们将在成功构建后获得确认。

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应用程序的时间,执行以下命令。

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

在http://localhost:8080/world中查看网络应用程序,我们已成功集装于可重复/确定性的Go Web应用程序。
使用以下命令启动,停止并检查容器的状态。

List all containers
root@demohost:~/docker ps -a
Start the container using it's id
root@demohost:~/docker start CONTAINER_ID_OF_WEB_APP
Stop the container using it's id
root@demohost:~/docker stop CONTAINER_ID_OF_WEB_APP

重建Docker镜像

让我们假设我们正在开发Web应用程序并更改代码。
现在要在更新代码后查看结果,我们需要重建Docker镜像,停止旧图像并运行新的图像,并且每次更改代码时都会继续。
要使此过程自动,我们将使用Docker卷来共享主机和容器之间的目录。
这意味着我们不必重建图像以在容器内更改。
现在,如果我们对Web应用程序的源代码进行了更改,如何检测容器会如何检测?
答案是有一个良好的工具,称为"gin"https://github。
com/codegangsta/gin可检测到源代码的任何更改,重建图像/二进制文件,并使用新的更新代码运行容器内的进程。

要使此过程自动进行自动,我们将编辑Dockerfile并安装GIN并将其执行为entry命令。
我们将揭示端口3030(GIN代理)而不是8080.GIN代理将转发到Web应用程序端口8080的流量。

root@demohost:~/web-app# vim.tiny Dockerfile
# Get latest golang docker image.
FROM golang:latest
# Create a directory inside the container to store our web-app and then make it working directory.
RUN mkdir -p /go/src/web-app
WORKDIR /go/src/web-app
# Copy the web-app directory into the container.
COPY . /go/src/web-app
# Download and install third party dependencies into the container.
RUN go get github.com/codegangsta/gin
RUN go-wrapper download
RUN go-wrapper install
# Set the PORT environment variable
ENV PORT 8080
# Expose port 8080 to the host so that outer-world can access your application
EXPOSE 3030
# Run Gin when the container starts
CMD gin run
# Tell Docker what command to run when the container starts
CMD ["go-wrapper", "run"]

现在构建图像并启动容器

root@demohost:~/web-app# docker build --rm -t web-app .

我们将使用当前工作目录运行Docker,作为Web应用程序目录的root,并通过曝光端口NO 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 /世界,我们将能够查看Web应用程序。
现在,如果我们在代码中更改任何内容,则会在刷新浏览器后,它将在浏览器中反映。