1.多阶段制作镜像,能够将镜像制作的很小
- 原始镜像
[root@moban go-docerfile]# cat Dockerfile
FROM golang:1.14.4-alpine as builder
WORKDIR /opt
COPY main.go /opt
RUN go build /opt/main.go
CMD "./main"
[root@moban go-docerfile]#
[root@moban go-docerfile]#
[root@moban go-docerfile]# cat main.go
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
[root@moban go-docerfile]#
#制作镜像
[root@moban go-docerfile]# docker build -t go:hello_world .
# 查看镜像的大小
[root@moban go-docerfile]# docker images|grep go
go hello_world 673f39523712 41 minutes ago 372MB
- 多阶段制作镜像
[root@moban go-docerfile]# cat Dockerfile
# build step
FROM golang:1.14.4-alpine
WORKDIR /opt
COPY main.go /opt
RUN go build /opt/main.go
CMD "./main"
# create real app image
FROM alpine:3.8
COPY --from=0 /opt/main /
CMD "./main"
[root@moban go-docerfile]# docker build -t go:alpine .
#查看镜像的大小
[root@moban go-docerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
go alpine 55644e7ca51b 34 seconds ago 6.48MB
(原来的镜像大小372M,现在的镜像6.48M)
[root@moban go-docerfile]# docker run --rm go:alpine
Hello World
[root@moban go-docerfile]#
如果 FROM 多的话 可以用别名
[root@moban go-docerfile]# cat Dockerfile
# build step
FROM golang:1.14.4-alpine as builder
WORKDIR /opt
COPY main.go /opt
RUN go build /opt/main.go
CMD "./main"
# create real app image
FROM alpine:3.8
COPY --from=builder /opt/main /
CMD "./main"
文档更新时间: 2021-11-22 15:58 作者:xtyang