Docker的使用&制作Golang微服务镜像并运行

868 阅读2分钟

一、Docker简述

1.什么是Docker

笼统一点理解的话,你可以把它看成一个轻量级的VMware虚拟机,

docker和VMware比较.jpg

2.优点

  • 启动速度快
  • 占用资源少
  • 部署方便
  • 隔离性好
  • 易于管理
  • 可持久化

3.怎么使用Docker

参考 Docker命令大全

二、使用Docker制作Golang微服务镜像并运行

1.准备文件practice.go和Dockerfile文件放同一目录,并进入目录下

package main

import (
	"github.com/gin-gonic/gin"
)

func main() {
	// 创建一个默认的路由引擎
	r := gin.Default()
	// GET:请求方式;/hello:请求的路径
	// 当客户端以GET方法请求/hello路径时,会执行后面的匿名函数
	r.GET("/hello", func(c *gin.Context) {
		// c.JSON:返回JSON格式的数据
		c.JSON(200, gin.H{
			"message": "Hello world!",
		})
	})
	// 启动HTTP服务,默认在0.0.0.0:8080启动服务
	r.Run()
}
FROM alpine

RUN sed -i 's/https/http/' /etc/apk/repositories
RUN apk add curl

RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata
ENV TZ Asia/Shanghai

WORKDIR /app
COPY ./practice /app/practice
#COPY --from=builder /app/etc /app/etc

EXPOSE 8080

CMD ["./practice"]

2.编译alpine二进制文件

xxxx@ubuntu:~/MyProject/Practice$ go env -w GOOS=linux
xxxx@ubuntu:~/MyProject/Practice$ go env -w CGO_ENABLED=0
xxxx@ubuntu:~/MyProject/Practice$ go build practice.go
当前目录下生成了可执行文件practice
xxxx@ubuntu:~/MyProject/Practice$ tree 
.
├── Dockerfile
├── practice
└── practice.go
可以在linux环境下执行
xxxx@ubuntu:~/MyProject/Practice$ ./practice 
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /hello                    --> main.main.func1 (3 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
验证结果

go build.png

3.制作Docker镜像

root@ubuntu:/home/xxxx/MyProject/Practice# docker build -t practice .
Sending build context to Docker daemon  15.51MB
Step 1/9 : FROM alpine
 ---> 6dbb9cc54074
Step 2/9 : RUN sed -i 's/https/http/' /etc/apk/repositories
 ---> Using cache
 ---> 3fda4e22207c
Step 3/9 : RUN apk add curl
 ---> Using cache
 ---> 14273f662442
Step 4/9 : RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata
 ---> Using cache
 ---> 6d2f22d57416
Step 5/9 : ENV TZ Asia/Shanghai
 ---> Using cache
 ---> b4a0dbb84330
Step 6/9 : WORKDIR /app
 ---> Using cache
 ---> b97a1d5811c7
Step 7/9 : COPY ./practice /app/practice
 ---> c0ff57ecd31a
Step 8/9 : EXPOSE 8080
 ---> Running in c76c2a2c7a6a
Removing intermediate container c76c2a2c7a6a
 ---> c2c88532e0f9
Step 9/9 : CMD ["./practice"]
 ---> Running in 6a43d98e96ba
Removing intermediate container 6a43d98e96ba
 ---> 4b84b2b9664f
Successfully built 4b84b2b9664f
Successfully tagged practice:latest
可以看到生成的镜像
root@ubuntu:/home/xxxx/MyProject/Practice# docker images
REPOSITORY                             TAG       IMAGE ID       CREATED          SIZE
practice                               latest    4b84b2b9664f   3 minutes ago    26.5MB

4.运行镜像

root@ubuntu:/home/xxxx/MyProject/Practice# docker run --net=host --name practice  -d  practice
818293911f4aa67541a88f99c9161f8db295899e5e2f71b8a00b30532f542226
可以看到运行的容器
root@ubuntu:/home/caoxun/MyProject/Practice# docker ps
CONTAINER ID   IMAGE      COMMAND        CREATED          STATUS          PORTS     NAMES
818293911f4a   practice   "./practice"   44 seconds ago   Up 43 seconds             practice

go build.png