FROM centos:7.5.1804
MAINTAINER 11@qq.com
CMD echo "hello world"
FROM centos:7.5.1804
# 指定源于一个基础镜像 centos:7.5.1804
# FROM <image>[:<tag>] [AS <name>]
MAINTAINER 11@qq.com
# 维护者/拥有者
# MAINTAINER <name>
CMD echo "hello world"
# 运行命令
# CMD <command> or CMD [<command>]
# 整个Dockerfile 中只能有一个,多个会被覆盖的
// 在Dockerfile 所在的目录下
docker build -t hello .
FROM golang:1.12 as build
ENV GOPROXY https://goproxy.cn
ENV GO111MODULE on
WORKDIR /go/cache
ADD go.mod .
ADD go.sum .
RUN go mod download
WORKDIR /go/release
ADD . .
RUN GOOS=linux CGO_ENABLED=0 go build -ldflags="-s -w" -installsuffix cgo -o app
FROM alpine as prod
ARG MODE="debug"
COPY --from=build /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=build /go/release /go/release/ /home/
WORKDIR /home
ENV MODE ${MODE}
CMD ["./app"]
FROM golang:1.12 as build
# 指定源于一个基础镜像 golang:1.12 并起一个别名 build
# FROM <image>[:<tag>] [AS <name>]
ENV GOPROXY http://goproxy.cn
ENV GO111MODULE on
# ENV 在镜像内设置环境变量
# ENV <key> <value>
WORKDIR /go/cache
# 指定接下来的参数工作目录 你可以理解为 cd /go/cache ,目录不存在时会自动创建
# WORKDIR <dir>
ADD go.mod .
ADD go.sum .
# 这里的意思是 go.mod 和go.sum 添加到 镜像的工作目录下即/go/cache 下
# ADD [--chown=<user>:<group>] <src>... <dest>
RUN go mod download
# 运行 go mod download
# RUN <command>
COPY . .
# 将当前目录的所以文件及文件夹 copy 到 工作目录下
# copy <src> <desc>
RUN GOOS=linux CGO_ENABLED=0 go build -ldflags="-s -w" -installsuffix cgo -o app
# 编译go 程序
FROM alpine as prod
# 再指定一个基础镜像alpine 并起别名 prod
ARG MODE="debug"
# 通过docker构建镜像时, 外部传参, 默认参数debug
# --build-arg MODE=release 外部docker build 时添加这个参数,意思是设置MODE=release
# ARG <key>[=<value>]
COPY --from=build /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=build /go/release /go/release/ /home/
# 将来自build(go:1.12) 镜像下的 /usr/share/zoneinfo/Asia/Shanghai /etc/ssl/certs/ca-certificates.crt /go/release /go/release/
# 复制到 prod 镜像的 /etc/localtime /etc/ssl/certs/ca-certificates.crt home/
# COPY [--from=<image_alias>] <src>...<desc>
WORKDIR /home
# 指定工作目录 可以理解为 cd /home
ENV MODE ${MODE}
# 设置环境变量 ${MODE} 使用的是ARG 里面的MODE
CMD ["./app"]
# CMD 执行命令
docker build --build-arg MODE=release -t app .
// --build-arg 外部传传参数, 多个参数就添加多个--build-arg
原文:https://www.cnblogs.com/xiaobaiskill/p/12046363.html