digoal
2023-08-14
PostgreSQL , PolarDB , docker , docker buildx , docker build , amd64 , arm64 , x86 , apple chip , 跨平台
前段时间用x86芯片的macOS创建了一个集成了大量常用插件和软件的PolarDB|PostgreSQL开源docker镜像
《使用Dockerfile+docker build制作PolarDB | PostgreSQL 开源docker镜像, 集成大量插件方便学习, 并推送到阿里云镜像服务》
最近笔记本换成了M2芯片, 以为要重新打包镜像, 结果发现能跑, 只是有个警告信息.
U-4G77XXWF-1921:~ digoal$ docker run -d -it --cap-add=SYS_PTRACE --cap-add SYS_ADMIN --privileged=true --name pg registry.cn-hangzhou.aliyuncs.com/digoal/opensource_database:pg14_with_exts
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
2556adf13522c6f9e58f0f643e50f27bef54155c10d99f2b52f78f0440e1acaa
U-4G77XXWF-1921:~ digoal$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2556adf13522 registry.cn-hangzhou.aliyuncs.com/digoal/opensource_database:pg14_with_exts "su - postgres -c '/…" 8 seconds ago Up 8 seconds 1921/tcp pg
U-4G77XXWF-1921:~ digoal$ docker exec -ti pg bash
root@2556adf13522:~# psql
psql (14.8 (Debian 14.8-1.pgdg110+1))
Type "help" for help.
postgres=#
因为这个image是在intel机器上打包的, 如果要在M2/M1 chip的macOS中运行, 可以加platform参数来启动, 可以消除该警告:
docker run --platform linux/amd64 -d -it --cap-add=SYS_PTRACE --cap-add SYS_ADMIN --privileged=true --name pg registry.cn-hangzhou.aliyuncs.com/digoal/opensource_database:pg14_with_exts
如下, warning消失:
docker stop pg
docker rm pg
U-4G77XXWF-1921:~ digoal$ docker run --platform linux/amd64 -d -it --cap-add=SYS_PTRACE --cap-add SYS_ADMIN --privileged=true --name pg registry.cn-hangzhou.aliyuncs.com/digoal/opensource_database:pg14_with_exts
34fd31ef5b963d19105a05ad97f625a23764375d030521e87c84c4cd67dd62f6
U-4G77XXWF-1921:~ digoal$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34fd31ef5b96 registry.cn-hangzhou.aliyuncs.com/digoal/opensource_database:pg14_with_exts "su - postgres -c '/…" 15 seconds ago Up 14 seconds 1921/tcp pg
以上情况需要在apple chip机器上运行x86应用, 涉及到指令翻译, 性能估计会差一点, 有兴趣的朋友可以测试对比一下.
如果要打包镜像的时候指定多平台, 可以参考docker文档, build时指定platform array. 例如:
docker buildx build --platform=linux/amd64,linux/arm64 -t="digoal/pg14:with_exts" . 2>&1 | tee ./logs/build.log
https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide/
https://github.com/docker/buildx/blob/master/docs/reference/buildx_create.md
https://github.com/docker/buildx/blob/master/docs/reference/buildx_build.md
https://blog.csdn.net/galoiszhou/article/details/123839892
https://www.cnblogs.com/ahfuzhang/p/17449254.html
https://blog.csdn.net/shida_csdn/article/details/123654866
https://support.apple.com/zh-cn/HT211861