1.首先使用go语言实现基本http服务
使用echo标准库实现8080端口输出helloworld的服务。
安装echo
mygohttp.go:
import (
"github.com/labstack/echo"
"log"
"net/http"
)
func main() {
e := echo.New()
e.GET("/", handlerindex)
log.Println("starting echo")
err := e.Start(":8080")
if err != nil {
log.Fatal("echo", err)
}
}
func handlerindex(c echo.Context) error {
log.Println("hello world handlerindex")
return c.JSON(http.StatusOK, `{"hello":"world"}`)
}
最后编译没问题运行没问题。
wang@wang:~/Desktop/mygohttp$ go build mygohttp.go
wang@wang:~/Desktop/mygohttp$ ls
Dockerfile mygohttp mygohttp.go package
wang@wang:~/Desktop/mygohttp$ ./mygohttp
2.dockerfile 制作
dockerfile 制作的源镜像我们可以在 hub.docker.com 找到 golang官方提供的源镜像,我们采用golang:latest。
#镜像
FROM golang:latest
#作者
MAINTAINER Vong "[email protected]"
#设置工作目录
WORKDIR $GOPATH/src/github.com/mygohttp
#将服务器的go工程代码加入到docker容器中
ADD . $GOPATH/src/github.com/mygohttp
#go get下载失败的库先给copy过去,具体的库有哪些见文章开始处的 安装echo 或者下文目录
COPY package/package_golang_org $GOPATH/src
#获取echo
RUN go get -u github.com/labstack/echo
#go构建可执行文件
RUN go build .
#暴露端口
EXPOSE 6064
#最终运行docker的命令
ENTRYPOINT ["./mygohttp"]
用下面的dockerfile可能好理解一点,两个dockerfile都可以使用:
#镜像
FROM golang:latest
#作者
MAINTAINER Vong "[email protected]"
#WORKDIR $GOPATH/src/mygohttp
#workdir也可以放后面
#将服务器的go工程代码加入到docker容器中
COPY mygohttp.go $GOPATH/src/mygohttp/
#如果没有过加上mygohttp后面的/,在WORKDIR处报错:
#Step 6/9 : WORKDIR $GOPATH/src/mygohttp
#Cannot mkdir: /go/src/mygohttp is not a directory
#拷贝一些依赖
COPY package/package_golang_org $GOPATH/src
#下载并安装echo
RUN go get -u github.com/labstack/echo
#go构建可执行文件
WORKDIR $GOPATH/src/mygohttp
RUN go build .
#暴露端口
EXPOSE 6064
#最终运行docker的命令
ENTRYPOINT ["./mygohttp"]
wang@wang:~/Desktop/mygohttp$ tree -L 5
.
├── Dockerfile
├── mygohttp
├── mygohttp.go
└── package
└── package_golang_org
└── golang.org
└── x
├── crypto
├── net
├── sys
└── text
8 directories, 3 files
注意dockerfile文件名称必须是Dockerfile,其文件必须在工程目录下。
3.执行dockerfile,并生成docker镜像
wang@wang:~/Desktop/mygohttp$ docker build -t hellomygohttp .
Sending build context to Docker daemon 93.11MB
Step 1/9 : FROM golang:latest
---> dc7582e06f8e
Step 2/9 : MAINTAINER Vong "[email protected]"
---> Using cache
---> eac484e76a9b
Step 3/9 : WORKDIR $GOPATH/src/github.com/mygohttp
---> Using cache
---> 3d13786d3164
Step 4/9 : ADD . $GOPATH/src/github.com/mygohttp
---> d5278249d1fc
Step 5/9 : COPY package/package_golang_org $GOPATH/src
---> 0fcd383c661b
Step 6/9 : RUN go get -u github.com/labstack/echo
---> Running in 5f628ca631b5
Removing intermediate container 5f628ca631b5
---> 52662b89355a
Step 7/9 : RUN go build .
---> Running in 8b5bf90f4d2c
Removing intermediate container 8b5bf90f4d2c
---> 6ee33dd9a5b1
Step 8/9 : EXPOSE 6064
---> Running in 59855e66a139
Removing intermediate container 59855e66a139
---> 9de288b63b60
Step 9/9 : ENTRYPOINT ["./mygohttp"]
---> Running in e9e544202997
Removing intermediate container e9e544202997
---> 81d7fc0aaf8f
Successfully built 81d7fc0aaf8f
Successfully tagged hellomygohttp:latest
制作完成后,我们即可以用docker images查看制作好的镜像。
wang@wang:~/go/src/github.com$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hellomygohttp latest 81d7fc0aaf8f 2 minutes ago 1.01GB
4.docker运行mygohttp
wang@wang:~/1.0/scripts$ docker run --name mygohttp -p 9090:8080 -d hellomygohttp
9ff0d40f234dab6d0baf1274f11c0639f3cb099d3837c0d70ea8c9076c6706c3
此时便启动了docker容器。
我们可以在http://localhost:9090/访问该服务。
输出:
至此服务便通了。这是我们也可以用如下指令查看容器内日志。
wang@wang:~/Desktop/mygohttp/package$ docker logs -f mygohttp
____ __
/ __/___/ / ___
/ _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.1.11
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
O\
⇨ http server started on [::]:8080
2019/10/28 08:57:39 starting echo
2019/10/28 09:13:19 hello world handlerindex
5.停止并删除mygohttp服务和镜像
#停止服务
docker stop mygohttp
#删除container实例
docker rm mygohttp
#删除container镜像
docker rmi mygohttp
6.如何在go工程启动时带入变量参数
我们在运行go工程的时候,往往需要带入动态变量去执行,那么如何做到?
我们采用 系统变量的方式
go语言对应的库是syscall.Getenv
修改代码如下:
package main
import (
"github.com/labstack/echo"
"log"
"net/http"
"syscall"
)
func main() {
v, ok := syscall.Getenv("TASKID")
log.Println("Getenv", v, ok)
e := echo.New()
e.GET("/", handlerindex)
log.Println("starting echo")
err := e.Start(":8080")
if err != nil {
log.Fatal("echo", err)
}
}
func handlerindex(c echo.Context) error {
log.Println("hello world handlerindex")
return c.JSON(http.StatusOK, `{"hello":"world"}`)
}
dockerfile 不需要修改,重新制作dockerfile。docker build -t hellomygohttp .
7.运行带系统参数的go工程容器
docker运行时 带-e,可以加入系统配置参数。
wang@wang:~/Desktop/mygohttp$ docker run --name mygohttp2 -e TASKID=abc hellomygohttp
____ __
/ __/___/ / ___
/ _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.1.11
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
O\
2019/10/28 09:26:23 Getenv abc true
2019/10/28 09:26:23 starting echo
⇨ http server started on [::]:8080
其中:
2019/10/28 09:26:23 Getenv abc true
2019/10/28 09:26:23 starting echo
便是日志输出