docker介绍
在这里简单介绍下Docker,建议深入学习
Docker 是一个开源的轻量级容器技术,让开发者可以打包他们的应用以及应用运行的上下文环境到一个可移植的镜像中,然后发布到任何支持Docker的系统上运行。 通过容器技术,在几乎没有性能开销的情况下,Docker 为应用提供了一个隔离运行环境
- 简化配置
- 代码流水线管理
- 提高开发效率
- 隔离应用
- 快速、持续部署
golang
一、编写Dockerfile
项目根目录创建 Dockerfile 文件,写入内容
FROM golang:1.18
WORKDIR /src/gin_blog
COPY . /src/gin_blog
ENV GO111MODULE=on
ENV GOPROXY="https://goproxy.cn"
RUN go mod tidy
RUN go build .
EXPOSE 8000
ENTRYPOINT ["./gin_log"]作用
golang:latest 镜像为基础镜像,将工作目录设置为 $GOPATH/src/go-gin-example,并将当前上下文目录的内容复制到 $GOPATH/src/go-gin-example 中
在进行 go build 编译完毕后,将容器启动程序设置为 ./go-gin-example,也就是我们所编译的可执行文件
注意 go-gin-example 在 docker 容器里编译,并没有在宿主机现场编译
说明
""ENTRYPOINT [ "curl", "-s", "http://ip.cn" ]ENTRYPOINT 二、构建镜像
sudo docker build -t gin_blog .......
go: downloading github.com/kr/text v0.2.0
go: downloading github.com/rogpeppe/go-internal v1.8.0
Removing intermediate container 98561ca7f5b5
---> 33f53945e296
Step 7/9 : RUN go build .
---> Running in 229d911348fd
Removing intermediate container 229d911348fd
---> e3adde861873
Step 8/9 : EXPOSE 8000
---> Running in 26df2f261f02
Removing intermediate container 26df2f261f02
---> 6255c0629227
Step 9/9 : ENTRYPOINT ["./gin_log"]
---> Running in 53855e2097f7
Removing intermediate container 53855e2097f7
---> 44f8f8bf076a
Successfully built 44f8f8bf076a
Successfully tagged gin_blog:latest三、验证镜像
sudo docker images创建并运行一个容器
sudo docker run -it -p 8000:8000 --volume /home/mayanan/gin_blog/runtime:/src/gin_blog/runtime --name gin_blog gin_blog- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /test --> gin_log/routers.InitRouter.func1 (3 handlers)
[GIN-debug] GET /auth --> gin_log/routers/api/v1.GetAuth (3 handlers)
[GIN-debug] GET /api/v1/tags --> gin_log/routers/api/v1.GetTags (4 handlers)
[GIN-debug] POST /api/v1/tags --> gin_log/routers/api/v1.AddTag (4 handlers)
[GIN-debug] PUT /api/v1/tag/:id --> gin_log/routers/api/v1.EditTag (4 handlers)
[GIN-debug] DELETE /api/v1/tag/:id --> gin_log/routers/api/v1.DeleteTag (4 handlers)
[GIN-debug] GET /api/v1/articles --> gin_log/routers/api/v1.GetArticles (4 handlers)
[GIN-debug] GET /api/v1/article/:id --> gin_log/routers/api/v1.GetArticle (4 handlers)
[GIN-debug] POST /api/v1/article --> gin_log/routers/api/v1.AddArticle (4 handlers)
[GIN-debug] PUT /api/v1/article/:id --> gin_log/routers/api/v1.EditArticle (4 handlers)
[GIN-debug] DELETE /api/v1/article/:id --> gin_log/routers/api/v1.DeleteArticle (4 handlers)服务启动成功。
思考
虽然应用已经能够跑起来了
但如果对 Golang 和 Docker 有一定的了解,我希望你能够想到至少2个问题
- 为什么 gin_blog 占用空间这么大?(可用 docker ps -as | grep gin_blog 查看)
创建超小的容器镜像
FROM golang:1.18构建Scratch镜像
Scratch镜像,简洁、小巧,基本是个空镜像
- 修改Dockerfile
FROM scratch
WORKDIR /src/gin_blog
COPY . /src/gin_blog
EXPOSE 8000
CMD ["./gin_log"]sudo docker build -t gin_blog .sudo docker run -it -p 8000:8000 --name gin_blog -v /home/mayanan/gin_blog/runtime:/src/gin_blog/runtime gin_blogsudo docker ps -as|grep gin_blog7060bab18b26 gin_blog "./gin_blog" 3 minutes ago Up 3 minutes 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp gin_blog 0B (virtual 17.1MB)仅仅只占了17.1兆。