自学内容网 自学内容网

Docker Engine多平台镜像构建(ARM64、x64、riscv64...)

Docker Engine多平台镜像构建(ARM64、x64、riscv64…)

1. Docker Engine安装

  1. 设置 Docker 的存储库
    # Add Docker's official GPG key:
    sudo apt-get update
    sudo apt-get install ca-certificates curl
    sudo install -m 0755 -d /etc/apt/keyrings
    sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
    sudo chmod a+r /etc/apt/keyrings/docker.asc
    
    # Add the repository to Apt sources:
    echo \
    "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
    $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
    sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt-get update
    
    
  2. 安装 Docker 软件包
    sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    
  3. 以非 root 用户身份管理 Docker
    运行完这里,记得重启电脑
    sudo groupadd docker
    sudo usermod -aG docker $USER
    newgrp docker
    

2. bridge-nf-call-iptables问题

docker info
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
# 解决办法:
sudo vi /etc/sysctl.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
sudo sysctl -p
# 检查是否生效
sudo sysctl net.bridge.bridge-nf-call-iptables
sudo sysctl net.bridge.bridge-nf-call-ip6tables

3. 代理配置问题

用代理就没必要设置镜像源了

sudo mkdir /etc/systemd/system/docker.service.d
sudo touch /etc/systemd/system/docker.service.d/proxy.conf
sudo vim /etc/systemd/system/docker.service.d/proxy.conf
# 把下面的内容拷贝进proxy.conf, 代理端口7897需要根据自己的情况设置
[Service] 
Environment="HTTP_PROXY=localhost:7897" 
Environment="HTTPS_PROXY=localhost:7897"

测试代理设置

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete 
Digest: sha256:305243c734571da2d100c8c8b3c3167a098cab6049c9a5b066b6021a60fcb966
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

4. 构建多平台镜像问题

4.1 前言

目的是:在X64电脑上运行ARM64架构的docker镜像,这样可以在镜像上开发代码,最后打包镜像分发。这么做好处是:可以直接在ARM64端安装docker并下载镜像,运行镜像即可实现项目部署。

4.2 buildx安装和测试

  1. 安装
    # 将 buildx 设置为默认构建器
    docker buildx install
    # 安装 QEMU 模拟支持多架构
    docker run --privileged --rm tonistiigi/binfmt --install all
    
  2. 编辑Dockerfile
    touch Dockerfile
    
    把下面的内容复制进Dockerfile中
    # syntax=docker/dockerfile:1
    
    # 使用一个轻量级的 ARM 架构镜像
    FROM --platform=linux/arm64 alpine:latest
    
    # 设置工作目录
    WORKDIR /app
    
    # 创建一个简单的文件
    RUN echo "Hello from ARM64 architecture!" > hello.txt
    
    # 显示文件内容
    CMD cat hello.txt
    
    
  3. 测试
    # 创建镜像
    docker buildx build --platform linux/arm64 -t my-arm64-image .
    # 运行镜像
    docker run --platform linux/arm64 my-arm64-image
    

5 参考资料

  1. Docker Engine官方安装教程文档
  2. ROS2交叉编译官方文档
  3. docker多平台镜像构建工具:buildx
  4. buildx官方文档

原文地址:https://blog.csdn.net/weixin_51226647/article/details/144359571

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!