docker 多阶段构建

多阶段构建优点

1. 只生成最后一阶段镜像,不生成中间镜像

2. 后阶段可以使用前阶段生成的文件,从而构建最小化镜像

3. 构建可以镜像时,可以指定构建特定阶段,不执行全部构建内容。日常使用第一阶段构建程序或运行程序,稳定时执行全部,构建出需要的镜像。

后阶段可以使用前阶段生成的文件:使用方法

第一阶段中《FROM golang:1.21 as build》 别名设定为 build

第二阶段中《COPY --from=build /bin/hello /bin/hello》使用--from=build 时,会调用第一阶段生成文件进行引用。不使用则引用不到。

准备测试文件

建立测试文件

#建立测试文件
cat > ./main.go <<EOF 
package main

import "fmt"

func main() {
  fmt.Println("hello, world")
}
EOF

建立Dockerfile文件

# syntax=docker/dockerfile:1
FROM golang:1.21 as build
WORKDIR /src
COPY ./main.go ./
RUN go build -o /bin/hello ./main.go

FROM scratch
COPY --from=build /bin/hello /bin/hello
CMD ["/bin/hello"]

使用命名多阶段构建

运行命令

docker build -t hello .

执行结果如下

user@tesla-t4:~/docker-multi$ docker build -t hello .

DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/

Sending build context to Docker daemon  3.072kB
Step 1/7 : FROM golang:1.21 as build
 ---> 9c541743933a
Step 2/7 : WORKDIR /src
 ---> Using cache
 ---> 9dcfcbaf87ab
Step 3/7 : COPY ./main.go ./
 ---> 0203b496f714
Step 4/7 : RUN go build -o /bin/hello ./main.go
 ---> Running in 4b312410cc7d
Removing intermediate container 4b312410cc7d
 ---> e6bedc995657
Step 5/7 : FROM scratch
 ---> 
Step 6/7 : COPY --from=build /bin/hello /bin/hello
 ---> a74952fc7368
Step 7/7 : CMD ["/bin/hello"]
 ---> Running in 653537b32292
Removing intermediate container 653537b32292
 ---> 605a275a494c
Successfully built 605a275a494c
Successfully tagged hello:latest

在特定的生成阶段停止

运行命令

docker build --target build -t hello .

 执行结果如下

user@tesla-t4:~/docker-multi$ docker build --target build -t hello .

DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/

Sending build context to Docker daemon  3.072kB
Step 1/4 : FROM golang:1.21 as build
 ---> 9c541743933a
Step 2/4 : WORKDIR /src
 ---> Using cache
 ---> 9dcfcbaf87ab
Step 3/4 : COPY ./main.go ./
 ---> Using cache
 ---> 0203b496f714
Step 4/4 : RUN go build -o /bin/hello ./main.go
 ---> Using cache
 ---> e6bedc995657
Successfully built e6bedc995657
Successfully tagged hello:latest

 

使用外部图像作为构建

COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker Buildx 是 Docker 官方发布的一款用于构建和打包 Docker 镜像的工具。它可以支持多种不同的构建器,包括 Dockerfile、BuildKit、Jib 等,同时还支持多种不同的平台和架构。 以下是使用 Docker Buildx 进行构建的步骤: 1. 安装 Docker Buildx 在 Docker 19.03 版本以上,Docker Buildx 已经内置,可以直接使用。如果你使用的是旧版本的 Docker,则需要安装 Docker Buildx 插件。可以通过以下命令进行安装: ``` docker buildx install ``` 2. 创建一个可用的构建器 使用 `docker buildx create` 命令创建一个可用的构建器,可以使用 `docker buildx ls` 命令查看所有可用的构建器。例如: ``` docker buildx create --name mybuilder ``` 3. 切换到指定的构建器 使用 `docker buildx use` 命令切换到指定的构建器,例如: ``` docker buildx use mybuilder ``` 4. 构建 Docker 镜像 使用 `docker buildx build` 命令来构建 Docker 镜像,例如: ``` docker buildx build --platform linux/amd64,linux/arm64 -t myimage:latest . ``` 其中 `--platform` 参数指定了要构建的平台和架构,`-t` 参数指定了镜像的名称和版本,`.` 表示 Dockerfile 所在的目录。 5. 推送 Docker 镜像 使用 `docker buildx push` 命令来推送 Docker 镜像,例如: ``` docker buildx push myimage:latest ``` 这样就完成了 Docker 镜像的构建和推送。 除了上述基本操作,Docker Buildx 还支持其他一些高级功能,如多阶段构建构建缓存、并发构建等。更多详细的使用方法可以参考 Docker 官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值