前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何用10分钟生成多平台docker镜像?

如何用10分钟生成多平台docker镜像?

作者头像
腾讯云TStack
发布2019-11-18 13:10:07
1.2K0
发布2019-11-18 13:10:07
举报

工作中需要在一台x86服务器从写好的golang程序源码生成linux/amd64linux/arm64 docker镜像,查阅了下资料,这里记录一下操作过程。

——Jeremy xu

 一、安装docker  

查阅docker官方文档,需要使用buildx,而Docker 19.03版本已经捆绑了buildx,方便起见,这里就直接使用19.03版本的docker了,过程如下:

12345678910111213141516171819202122232425

$ sudo yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-engine$ sudo yum install -y yum-utils \device-mapper-persistent-data \lvm2$ sudo yum-config-manager \--add-repo \https://download.docker.com/linux/centos/docker-ce.repo# 列一下可安装的docker版本$ yum list docker-ce --showduplicates | sort -r# 安装19.03.2版本的docker$ sudo yum install docker-ce-19.03.2 docker-ce-cli-19.03.2 containerd.io# 启动docker服务$ systemctl start docker

二、安装qemu-user-static  

为了让在x86上可以运行arm64的docker镜像,这里需要安装qemu-user-static,过程如下:

1

$ docker run --rm --privileged multiarch/qemu-user-static --reset -p yes

三、创建构建多平台docker镜像的构建器  

首先打开docker-cli的experimental开关:

123456

$ mkdir ~/.docker$ cat << EOF > ~/.docker/config.json{"experimental": "enabled"}EOF

创建并启动构建器:

123456

# 创建构建器$ docker buildx create --name builder --node default --use# 启动构建器$ docker buildx inspect builder --bootstrap# 观察下当前使用的构建器及构建器支持的cpu架构,可以看到支持很多cpu架构$ docker buildx ls

四、编写脚本生成多平台docker镜像 

假设有一个普通的golang程序源码,我们已经写好了Dockerfile生成其docker镜像,如下:

1234567891011121314151617181920212223242526272829303132

# Start from the latest golang base imageFROM golang:latest as go-builder# Add Maintainer InfoLABEL maintainer="Jeremy Xu <jeremyxu2010@gmail.com>"# Set the Current Working Directory inside the containerWORKDIR /app# Copy go mod and sum filesCOPY go.mod go.sum ./# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changedRUN go mod download# Copy the source from the current directory to the Working Directory inside the containerCOPY ./cmd ./cmd# Build the Go appRUN go build -o output/demo ./cmd# Start from the latest alpine base imageFROM alpine:latest# Set the Current Working Directory inside the containerWORKDIR /app# Copy execute file from go-builderCOPY --from=go-builder /app/output/demo /app/demo# Set docker image commandCMD [ "/app/demo" ]

那么现在只需要使用两条命令,即可生成linux/amd64linux/arm64 docker镜像,如下:

1234

# 生成linux/amd64 docker镜像$ docker buildx build --rm -t go-mul-arch-build:latest-amd64 --platform=linux/amd64 --output=type=docker .# 生成linux/arm64 docker镜像$ docker buildx build --rm -t go-mul-arch-build:latest-arm64 --platform=linux/arm64 --output=type=docker .

最后检查下生成的docker镜像:

1234567891011121314151617

# 运行下linux/amd64的docker镜像,检查镜像的cpu架构$ docker run --rm -ti go-mul-arch-build:latest-amd64 sh/app # ./demoHello world!oh dear/app # uname -mx86_64/app # exit# 运行下linux/arm64的docker镜像,检查镜像的cpu架构$ docker run --rm -ti go-mul-arch-build:latest-arm64 sh/app # ./demoHello world!oh dear/app # uname -maarch64/app # exit

本操作指引中涉及的示例代码、脚本见github项目。(网址见参考链接)

参 考

1、https://docs.docker.com/install/linux/docker-ce/centos/

2、https://docs.docker.com/buildx/working-with-buildx/#build-multi-platform-images

3、https://github.com/docker/buildx

4、https://github.com/multiarch/qemu-user-static

5、https://www.callicoder.com/docker-golang-image-container-example/

6、https://github.com/docker/buildx/issues/138

7、https://github.com/jeremyxu2010/go-mul-arch-build

猜你还想看这些内容

●如何使用dex搭建一个身份认证系统?

●Ceph Bulestore磁盘空间分配初探

●搬运基础服务到kubernetes,遇这3类大坑怎么破?

· END ·

记得文末点个好看鸭~


点就完事儿了!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-11-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 腾讯云TStack 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  •  一、安装docker  
  • 二、安装qemu-user-static  
  • 为了让在x86上可以运行arm64的docker镜像,这里需要安装qemu-user-static,过程如下:
  • 参 考
相关产品与服务
容器镜像服务
容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档