交叉编译docker镜像

结论:
通过buildx可以使用Dockerfile构建多平台镜像;
但是针对java工程的maven Jib插件,可以直接使用其中高版本的实验功能“platforms”构建多平台镜像,不需要修改docker配置。
可使用buildx验证或docker inspect查看镜像的编译平台

交叉编译教程
模拟目标硬件的用户空间
binfmt_misc
程序运行时动态翻译二进制文件

使用交叉编译器

构建多平台 Docker 镜像
Docker 19.03 引入的插件 buildx,构建多平台 Docker 镜像
利用 BuildKit的全部功能扩展了 docker build 的功能

#启用 buildx 插件
#Docker 版本不低于 19.03

#设置环境变量(开启实验特性)
vi /etc/profile
export DOCKER_CLI_EXPERIMENTAL=enabled
source /etc/profile

#验证是否开启
docker buildx version
#github.com/docker/buildx v0.3.1-tp-docker 6db68d029599c6710a32aa7adcba8e5a344795a7

#启用 binfmt_misc
docker run --rm --privileged docker/binfmt:66f9012c56a8316f9244ffd7622d7c21c1f6f28d

#验证是 binfmt_misc 否开启
ls -al /proc/sys/fs/binfmt_misc/
cat /proc/sys/fs/binfmt_misc/qemu-aarch64

从默认的构建器切换到多平台构建器

#创建一个新的构建器
docker buildx create --use --name mybuilder

#启动构建器
docker buildx inspect mybuilder --bootstrap

#查看当前构建器
docker buildx ls

#使用Dockerfile构建本地镜像
docker buildx build -t hello:amd64 --platform=linux/amd64 -o type=docker .
docker buildx build -t hello:arm64 --platform=linux/arm64 -o type=docker .

#运行镜像
#后台运行
docker run --name hello-amd64 -d hello:amd64
docker run --name hello-arm64 -d hello:arm64

Dockerfile:

#amd64
#FROM adoptopenjdk/openjdk8-openj9:alpine-slim

#arm64
FROM adoptopenjdk/openjdk8-openj9:aarch64-ubi-minimal-jre8u-nightly
EXPOSE 8080
VOLUME /tmp
ADD hello.jar /app.jar
ENV JAVA_OPTS="-Dfile.encoding=UTF8  -Duser.timezone=GMT+08"
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

maven jib插件构建多平台镜像

jib多平台构建参数

jib多平台构建具体操作
若只编译多平台镜像,则此方法无需修改docker相关配置

获取多平台的基础镜像

docker官网搜索可用的多平台镜像,Tags的OS/ARCH会显示具体支持平台
openjdk:8-jdk-alpine

远程镜像查看命令(需要启用Docker CLI实验特性,之前在启用buildx时已经设置相关环境变量):
docker manifest inspect openjdk:8-jdk-alpine

结果:
交叉编译docker镜像_第1张图片

修改jib插件配置

实验特性:platforms
from.platforms.platform :配置基础映像的平台以从清单清单中进行选择。

Property Type Default Description
architecture string amd64 The architecture of a base image to select from a manifest list.
os string linux The OS of a base image to select from a manifest list.

pom.xml

<jib-maven-plugin.version>2.8.0jib-maven-plugin.version>
<plugin>
    <groupId>com.google.cloud.toolsgroupId>
    <artifactId>jib-maven-pluginartifactId>
    <version>${jib-maven-plugin.version}version>
    <configuration>
        <from>
            <image>${docker.image.from}image>
            <platforms>
                <platform>
                    <architecture>amd64architecture>
                    <os>linuxos>
                platform>
                <platform>
                    <architecture>arm64architecture>
                    <os>linuxos>
                platform>
            platforms>
        from>
        ...
     configuration>
plugin>

运行maven构建命令

多平台构建只能运行jib:build 推送到镜像仓库

mvn clean package jib:build
构建结果:
交叉编译docker镜像_第2张图片
验证命令:docker buildx imagetools inspect 127.0.0.1:5000/demo/snapshots/hello

交叉编译docker镜像_第3张图片

可以通过 docker pull 127.0.0.1:5000/demo/snapshots/hello 拉取刚刚创建的镜像了,Docker 将会根据你的 CPU 架构拉取匹配的镜像

启用本地镜像仓库

若没有私有的远程仓库,则可以启用本地仓库

#启用本地镜像仓库
docker pull registry

#启动
docker run -itd -v /root/docker-registry:/data/registry --restart=always --name docker-registry -p 5000:5000 registry:latest

#本地镜像打tag
docker tag a3562aa0b991 127.0.0.1:5000/openjdk:8-jdk-alpine

#推送到本地镜像库
docker push 127.0.0.1:5000/openjdk:8-jdk-alpine

通过命令行指定jib编译平台

pom文件设置

<properties>
        <docker.repostory>xxxdocker.repostory>
        <docker.registry.name>xxxdocker.registry.name>
        <docker.image.from>openjdk:8-jdk-alpinedocker.image.from>
        <docker.image.to>xxxdocker.image.to>
        <docker.platform.arch>amd64docker.platform.arch>
properties>
<build>
  <plugin>
  	<groupId>com.google.cloud.toolsgroupId>
		<artifactId>jib-maven-pluginartifactId>
		<version>${jib-maven-plugin.version}version>
		<configuration>
			<from>
				<image>${docker.image.from}image>
				<platforms>
					<platform>
						<architecture>${docker.platform.arch}architecture>
						<os>linuxos>
					platform>
				platforms>
			from>
			<to>
				<image>
					${docker.repostory}/${docker.registry.name}/${docker.image.to}/${project.artifactId}
				image>
			to>
			...
		configuration>
 	plugin>
build>

arm64平台编译

mvn clean package jib:build -Ddocker.platform.arch=arm64 -Djib.to.tags=arm64

拉取生成的镜像后,使用docker inspect 查看结果:
docker inspect 127.0.0.1:5000/demo/snapshots/hello:arm64
交叉编译docker镜像_第4张图片

x86-64平台编译

mvn clean package jib:build -Ddocker.platform.arch=amd64 -Djib.to.tags=amd64

拉取生成的镜像后,使用docker inspect 查看结果:
docker inspect 127.0.0.1:5000/demo/snapshots/hello:amd64
交叉编译docker镜像_第5张图片

参考文档:

buildx教程
buildx官方文档
jib操作教程1

jib操作教程2

你可能感兴趣的:(编程,读书笔记,docker,交叉编译)