尝试在m1 mac上使用Podman运行amd64容器

首先

本文记录了在m1 mac上使用Podman运行MongoDB amd64容器的过程。在MAC环境中,我们使用基于QEMU的虚拟机作为Podman machine。类似的解决方案还有Rancher Desktop和Lima。通过使用QEMU技术,可以在m1上运行amd64容器映像。

前提条件

    インターネットアクセス環境 (数GB程度のダウンロードが走ります)

工作环境

    • Apple MacBook Pro 13, M1, 2020

 

    • 16GB Memory

 

    macOS Ventura

备好

Podman的安装和设置

使用Homebrew安装Podman。然后使用4个vCPU和8GB内存设置Podman机器。默认情况下,它以1个vCPU和2GB内存配置。请根据需要使用–cpus和–memory选项指定。

执行 podman machine init 将下载数GB大小的 Fedora CoreOS 虚拟机。请注意需要具备互联网连接环境。

brew install podman
podman machine init --cpus 4 --memory 8192
podman machine start

版本确认。

podman -v
podman version 4.4.1

如果 Podman Machine 处于停止状态,许多 Podman 命令将会出现以下错误。请在启动状态下使用。

podman ps -a
Error: failed to connect: dial tcp [::1]:54669: connect: connection refused

Podman机器的停止方法是使用”podman machine stop”命令。

我会检查Podman机器的设置。

podman machine list
NAME                     VM TYPE     CREATED            LAST UP            CPUS        MEMORY      DISK SIZE
podman-machine-default*  qemu        About an hour ago  Currently running  4           8.59GB      107.4GB
podman machine info
Host:
  Arch: arm64
  CurrentMachine: podman-machine-default
  DefaultMachine: podman-machine-default
  EventsDir: /var/folders/m3/8c8dm0td3qj7yn_xj16x4x940000gn/T/podman-run--1/podman
  MachineConfigDir: /Users/username/.config/containers/podman/machine/qemu
  MachineImageDir: /Users/username/.local/share/containers/podman/machine/qemu
  MachineState: Running
  NumberOfMachines: 1
  OS: darwin
  VMType: qemu
Version:
  APIVersion: 4.4.1
  Built: 1675882998
  BuiltTime: Thu Feb  9 04:03:18 2023
  GitCommit: 34e8f3933242f2e566bbbbf343cf69b7d506c1cf
  GoVersion: go1.19.5
  Os: darwin
  OsArch: darwin/arm64
  Version: 4.4.1

安装和设置已完成。

检查正在运行的CPU架构。

运行Alpine的Arm64映像

检查确认 M1 Mac
可以运行基于 ARM 的映像。运行 `uname -a` 命令可以确认它是 aarch64 Linux。

podman run --rm docker.io/library/alpine:3.17.2 uname -a
Trying to pull docker.io/library/alpine:3.17.2...
Getting image source signatures
Copying blob sha256:af6eaf76a39c2d3e7e0b8a0420486e3df33c4027d696c076a99a3d0ac09026af
Copying config sha256:d74e625d91152966d38fe8a62c60daadb96d4b94c1a366de01fab5f334806239
Writing manifest to image destination
Storing signatures
Linux 9d77a17f8d28 6.1.11-200.fc37.aarch64 #1 SMP PREEMPT_DYNAMIC Thu Feb  9 19:41:51 UTC 2023 aarch64 Linux
podman image list
REPOSITORY                           TAG               IMAGE ID      CREATED       SIZE
docker.io/library/alpine             3.17.2            d74e625d9115  12 days ago   7.75 MB

执行alpine amd64镜像

与上述一样,使用alpine:3.17.2,但是在amd64镜像上执行uname -a命令,我们将在Docker Hub上确认Digest。首先确认操作系统/架构,然后点击amd64 Digest的部分。

run_amd64_on_m1mac_01.png

由于全文都显示在DIGEST中,请复制。

run_amd64_on_m1mac_02.png

在TAG后面黏贴并执行@和Digest。将显示一条“警告:镜像平台不同”的信息,但可以确认uname -a命令已正确执行,并且x86_64 Linux容器镜像正在运行。

podman run --rm alpine:3.17.2@sha256:e2e16842c9b54d985bf1ef9242a313f36b856181f188de21313820e177002501 uname -a
Resolved "alpine" as an alias (/etc/containers/registries.conf.d/000-shortnames.conf)
Trying to pull docker.io/library/alpine@sha256:e2e16842c9b54d985bf1ef9242a313f36b856181f188de21313820e177002501...
Getting image source signatures
Copying blob sha256:63b65145d645c1250c391b2d16ebe53b3747c295ca8ba2fcb6b0cf064a4dc21c
Copying config sha256:b2aa39c304c27b96c1fef0c06bee651ac9241d49c4fe34381cab8453f9a89c7d
Writing manifest to image destination
Storing signatures
WARNING: image platform (linux/amd64) does not match the expected platform (linux/arm64)
Linux 1de721790093 6.1.11-200.fc37.aarch64 #1 SMP PREEMPT_DYNAMIC Thu Feb  9 19:41:51 UTC 2023 x86_64 Linux

当您显示图像列表时,可以确认新图像。由于标签重复,标签将显示为。

podman image list
REPOSITORY                           TAG               IMAGE ID      CREATED       SIZE
docker.io/library/alpine             <none>            b2aa39c304c2  12 days ago   7.34 MB
docker.io/library/alpine             3.17.2            d74e625d9115  12 days ago   7.75 MB

如果检查没有TAG的那个选项,它会显示如下信息。Digest是在运行时指定的内容,我们也可以确认架构是amd64。

podman inspect b2aa39c304c2
[
     {
          "Id": "b2aa39c304c27b96c1fef0c06bee651ac9241d49c4fe34381cab8453f9a89c7d",
          "Digest": "sha256:e2e16842c9b54d985bf1ef9242a313f36b856181f188de21313820e177002501",
          "RepoTags": [],
          "RepoDigests": [
               "docker.io/library/alpine@sha256:e2e16842c9b54d985bf1ef9242a313f36b856181f188de21313820e177002501"
          ],
          "Parent": "",
          "Comment": "",
          "Created": "2023-02-11T04:46:42.558343068Z",
          "Config": {
               "Env": [
                    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
               ],
               "Cmd": [
                    "/bin/sh"
               ]
          },
          "Version": "20.10.12",
          "Author": "",
          "Architecture": "amd64",
          "Os": "linux",
          "Size": 7340512,
          "VirtualSize": 7340512,
          "GraphDriver": {
               "Name": "overlay",
               "Data": {
                    "UpperDir": "/var/home/core/.local/share/containers/storage/overlay/7cd52847ad775a5ddc4b58326cf884beee34544296402c6292ed76474c686d39/diff",
                    "WorkDir": "/var/home/core/.local/share/containers/storage/overlay/7cd52847ad775a5ddc4b58326cf884beee34544296402c6292ed76474c686d39/work"
               }
          },
          "RootFS": {
               "Type": "layers",
               "Layers": [
                    "sha256:7cd52847ad775a5ddc4b58326cf884beee34544296402c6292ed76474c686d39"
               ]
          },
          "Labels": null,
          "Annotations": {},
          "ManifestType": "application/vnd.docker.distribution.manifest.v2+json",
          "User": "",
          "History": [
               {
                    "created": "2023-02-11T04:46:42.449083344Z",
                    "created_by": "/bin/sh -c #(nop) ADD file:40887ab7c06977737e63c215c9bd297c0c74de8d12d16ebdf1c3d40ac392f62d in / "
               },
               {
                    "created": "2023-02-11T04:46:42.558343068Z",
                    "created_by": "/bin/sh -c #(nop)  CMD [\"/bin/sh\"]",
                    "empty_layer": true
               }
          ],
          "NamesHistory": [
               "docker.io/library/alpine@sha256:e2e16842c9b54d985bf1ef9242a313f36b856181f188de21313820e177002501"
          ]
     }
]

执行MongoDB的amd64镜像

请选择图片。

由于 MongoDB 官方提供了 arm64 的支持,我们将使用 bitnami/mongodb。

在中国切换到本地代码上:bitnami/mongodb:4.2.5启动

podman run \
-d --restart=unless-stopped \
-e MONGODB_USERNAME=user1 \
-e MONGODB_PASSWORD=password \
-e MONGODB_DATABASE=master1 \
--name mongo docker.io/bitnami/mongodb:4.2.5

WARNING: image platform (linux/amd64) does not match the expected platform (linux/arm64)
c90431e2273ebade50761d885dc03dd37237135194f415660cbb19428d36c451

确认动作状态

我要确认集装箱的ID。

podman ps -a
CONTAINER ID  IMAGE                            COMMAND               CREATED      STATUS      PORTS       NAMES
c90431e2273e  docker.io/bitnami/mongodb:4.2.5  /opt/bitnami/scri...  2 hours ago  Up 2 hours              mongo

进入容器并使用mongo命令确认它正在运行。

podman exec -it c90431e2273e bash
1001@c90431e2273e:/$ mongo -u user1 -p password --authenticationDatabase master1
MongoDB shell version v4.2.5
connecting to: mongodb://127.0.0.1:27017/?authSource=master1&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b52f5f23-9415-4412-b757-04e6d56f9d76") }
MongoDB server version: 4.2.5
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
2023-02-23T06:52:32.698+0000 I  STORAGE  [main] In File::open(), ::open for '//.mongorc.js' failed with Permission denied
> use master1
switched to db master1
> show collections
> exit
bye
1001@c90431e2273e:/$ 

请提供一些参考资料。

Podman/开始入门

Podman 安装指南

我們使用podman在amd64的eclipse-mosquitto上運行Mosquitto。

bannerAds