本文主要介绍如何将go项目打包成镜像,首先介绍Dockerfile常用命令介绍,然后介绍使用工具goctl用于生成Dockerfile,还可以根据需求自定义指令内容,最后讲解如何将go-blog项目打包成镜像,以及如何运行等
参考文档:
开发完项目之后,可以通过dockerfile将项目打包成镜像
Dockerfile 是用于构建 Docker 镜像的文本文件,其中包含一系列指令(命令)。这些指令按照顺序执行,用于定义镜像的构建过程。下面是常用的 Dockerfile 命令及其详细解释:
FROM ubuntu:latest
。LABEL maintainer="[email protected]"
。RUN apt-get update && apt-get install -y curl
。CMD ["nginx", "-g", "daemon off;"]
。EXPOSE 8080
。ENV MYSQL_VERSION 5.7
。ADD app.jar /app/
。COPY ./src /app/src
。WORKDIR /app
。VOLUME /data
ENTRYPOINT ["java", "-jar", "app.jar"]
。USER myuser
。ARG VERSION=latest
。ONBUILD ADD . /app
。下面是一个简单的示例,演示了如何编写一个用于构建基本 Go 应用程序的 Dockerfile。
# 使用官方的 Golang 镜像作为基础镜像
FROM golang:1.16
# 在容器内创建一个目录来存放我们的应用代码
RUN mkdir /app
# 将工作目录切换到 /app
WORKDIR /app
# 将当前目录下的所有文件拷贝到 /app 目录
COPY . .
# 编译 Go 应用程序
RUN go build -o myapp .
# 暴露 8080 端口
EXPOSE 8080
# 运行应用程序
CMD ["./myapp"]
当然还有更简便的方式,使用goctl工具生成Dockerfile,解放了生成力~
go install github.com/zeromicro/go-zero/tools/goctl@latest
(base) yangmiao@ym-mac gin-blog % goctl docker --help
Generate Dockerfile
Usage:
goctl docker [flags]
Flags:
--base string The base image to build the docker image, default scratch (default "scratch")
--branch string The branch of the remote repo, it does work with --remote
--exe string The executable name in the built image
--go string The file that contains main function
-h, --help help for docker
--home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
--port int The port to expose, default none
--remote string The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
--tz string The timezone of the container (default "Asia/Shanghai")
--version string The goctl builder golang image version
在执行该命令后,Goctl 会自动生成一个名为 Dockerfile 的文件,包含适当的环境配置和基础镜像信息。
goctl docker --go main.go
生成的Dockerfile文件如下所示
多阶段构建
FROM golang:alpine AS builder
LABEL stage=gobuilder
ENV CGO_ENABLED 0
ENV GOPROXY https://goproxy.cn,direct
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk update --no-cache && apk add --no-cache tzdata
WORKDIR /build
ADD go.mod .
ADD go.sum .
RUN go mod download
COPY . .
RUN go build -ldflags="-s -w" -o /app/main main.go
FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/Shanghai
ENV TZ Asia/Shanghai
WORKDIR /app
COPY --from=builder /app/main /app/main
CMD ["./main"]
至于docker如何使用,可以参考Docker-常用命令介绍,看这一篇就够了
在项目根目录下,执行docker build
,用于生成镜像,生成镜像后就可以查看和启动了~
docker build -t go-blog:v1 .
(base) yangmiao@ym-mac gin-blog % docker build -t go-blog:v1 .
ERROR: Cannot connect to the Docker daemon at tcp://localhost:2375. Is the docker daemon running?
(base) yangmiao@ym-mac gin-blog % sudo docker build -t go-blog:v1 .
Password:
[+] Building 515.7s (18/18) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 694B 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/golang:alpine 308.4s
=> [internal] load build context 10.3s
=> => transferring context: 215.37MB 10.1s
=> [builder 1/9] FROM docker.io/library/golang:alpine@sha256:70afe55365a265f0762257550bc38440e0d6d6b97020d3f8c85328f00200dd8e 156.9s
=> => resolve docker.io/library/golang:alpine@sha256:70afe55365a265f0762257550bc38440e0d6d6b97020d3f8c85328f00200dd8e 0.0s
=> => sha256:86a63ed24dc22a348b35d99b5ec9dc67ff66563b539875e5c8ab2d870b3991ac 286.31kB / 286.31kB 75.8s
=> => sha256:ae48e6158ebb1a353f7d7b0676b0bd55a09a448a440b65135db378ffb0040919 64.09MB / 64.09MB 153.7s
=> => sha256:70afe55365a265f0762257550bc38440e0d6d6b97020d3f8c85328f00200dd8e 1.65kB / 1.65kB 0.0s
=> => sha256:635bf83d6a1993bf40e3c575d7b522d41950af4f1a5c1c7cd01c81d93b76f4bf 1.16kB / 1.16kB 0.0s
=> => sha256:1ddcbcaf7f02eab589ea6e5727ede30fe040922e4674737894898cddeaba40e0 6.34kB / 6.34kB 0.0s
=> => sha256:2c03dbb20264f09924f9eab176da44e5421e74a78b09531d3c63448a7baa7c59 3.33MB / 3.33MB 76.8s
=> => sha256:edabe92b0de78c4b662f63a4b2884d0821795e38a90c7ec070ccfa98f8aa236c 156B / 156B 151.3s
=> => extracting sha256:2c03dbb20264f09924f9eab176da44e5421e74a78b09531d3c63448a7baa7c59 0.8s
=> => extracting sha256:86a63ed24dc22a348b35d99b5ec9dc67ff66563b539875e5c8ab2d870b3991ac 0.1s
=> => extracting sha256:ae48e6158ebb1a353f7d7b0676b0bd55a09a448a440b65135db378ffb0040919 2.6s
=> => extracting sha256:edabe92b0de78c4b662f63a4b2884d0821795e38a90c7ec070ccfa98f8aa236c 0.0s
=> [builder 2/9] RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories 1.0s
=> [builder 3/9] RUN apk update --no-cache && apk add --no-cache tzdata 2.7s
=> [builder 4/9] WORKDIR /build 0.0s
=> [builder 5/9] ADD go.mod . 0.0s
=> [builder 6/9] ADD go.sum . 0.0s
=> [builder 7/9] RUN go mod download 14.2s
=> [builder 8/9] COPY . . 7.4s
=> [builder 9/9] RUN go build -ldflags="-s -w" -o /app/main main.go 24.5s
=> [stage-1 1/4] COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt 0.0s
=> [stage-1 2/4] COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/Shanghai 0.0s
=> [stage-1 3/4] WORKDIR /app 0.0s
=> [stage-1 4/4] COPY --from=builder /app/main /app/main 0.1s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:556e5362b86a63c8d1325549b595d987c8c79ad749524875c9d018c44cbf3ad5 0.0s
=> => naming to docker.io/library/go-blog:v1 0.0s
sudo docker images | grep go-blog
sudo docker run -it go-blog:v1