苹果容器革命:用 Swift 构建的开源 Docker 替代方案

苹果容器革命:用 Swift 构建的开源 Docker 替代方案

苹果为开发者带来重磅更新——macOS 原生容器支持

在今年的 WWDC 大会上,苹果悄然发布了一个专为 Apple Silicon 设计的原生容器化框架。这一重大突破为 macOS 开发者生态带来了全新的容器解决方案。

Apple Container 框架让开发者能够拉取、构建和运行 OCI 兼容的容器镜像,而无需依赖 Docker 或 Podman。该框架完全用 Swift 构建,专为 M 系列芯片优化。告别繁琐的工作流程,告别性能妥协,享受内置于 macOS 的纯净 Swift 驱动容器管理体验。

Apple Container 使用 Containerization Swift 包进行底层容器、镜像和进程管理。

🚀 核心特性

  • ✅ 支持从任何标准容器注册表拉取镜像
  • ✅ 轻松构建和推送 OCI 镜像
  • ✅ 与现有容器生态系统完全兼容
  • 100% Swift 原生实现 🍎

系统要求

使用 Apple Container 需要满足以下条件:

  • macOS 15 Sequoia 或更高版本
  • Xcode 26 Beta 或更高版本
  • Apple Silicon Mac

安装指南

步骤 1:验证系统要求

确保你的设备满足以下条件:

  • Apple Silicon Mac(M1/M2/M3 系列)
  • macOS 15.5 Sequoia
  • Xcode 26 Beta

下载 Xcode 26 Beta:访问 Apple 开发者下载页面

步骤 2:安装 Apple Container 包

从 Apple 官方 GitHub 仓库下载并安装:

下载地址GitHub Releases

推荐下载:container-0.1.0-installer-signed.pkg(本文撰写时的最新版本)

双击 .pkg 文件并按照安装提示完成安装。

步骤 3:启动 Apple Container 服务

在 macOS 终端中执行:

A. 启动服务

1
2
3
4
5
6
7
$ container system start

Verifying apiserver is running...
Installing base container filesystem...
No default kernel configured.
Install the recommended default kernel from [https://github.com/kata-containers/kata-containers/releases/download/3.17.0/kata-static-3.17.0-arm64.tar.xz]? [Y/n]: Y
Installing kernel...

B. 检查容器列表(初次安装应无结果)

1
2
3
$ container ls -a

ID IMAGE OS ARCH STATE ADDR

实战演示:启动你的第一个 Apple Container

步骤 4:创建 PostgreSQL 容器

A. 创建工作目录

1
2
mkdir postgresql15
cd postgresql15/

B. 创建 Dockerfile

1
2
3
4
5
6
FROM arm64v8/postgres:15.6
ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=mypasswd1234
ENV POSTGRES_DB=myappdb
ENV POSTGRES_LISTEN_ADDRESS=0.0.0.0
CMD ["postgres"]

C. 构建镜像

1
container build --tag my-apple-postgresql --file Dockerfile .

D. 查看镜像列表

1
2
3
4
5
container image list

NAME TAG DIGEST
arm64v8/postgres 15.6 0526cc72d34b102c7dc6b57e...
my-apple-postgresql latest c6416c9a65895c2c5b14a110...

E. 运行容器

1
2
3
4
5
6
7
8
9
# 启动容器(后台运行)
container run -d --name my-apple-postgresql my-apple-postgresql

# 检查运行状态
container ls

ID IMAGE OS ARCH STATE ADDR
my-apple-postgresql my-apple-postgresql:latest linux arm64 running 192.168.64.3
buildkit ghcr.io/apple/container-builder-shim/builder:0.1.0 linux arm64 running 192.168.64.2

-d 参数表示在后台运行容器

F. 与容器交互

1
2
3
4
5
6
container exec my-apple-postgresql df -h

Filesystem Size Used Avail Use% Mounted on
/dev/vdb 504G 490M 504G 1% /
none 496M 0 496M 0% /dev
tmpfs 64M 1.1M 63M 2% /dev/shm

G. 进入容器内部 Shell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
container exec -it my-apple-postgresql /bin/bash

### >> Shell 会话开始
root@my-apple-postgresql:/# echo $SHELL
/bin/bash

root@my-apple-postgresql:/# psql -h 192.168.64.3 -p 5432 -U postgres -d myappdb
Password for user postgres:
psql (15.6 (Debian 15.6-1.pgdg120+2))
Type "help" for help.

myappdb=# SELECT version();
---------------------------------------------------------------------------------------------------------------------------
PostgreSQL 15.6 (Debian 15.6-1.pgdg120+2) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
(1 row)

myappdb=# \q
root@my-apple-postgresql:/# exit
exit
### >> Shell 会话结束

-it 参数开启交互式终端会话

H. 从本地机器连接数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 安装 socat(用于端口转发)
brew install socat

# 设置端口转发
socat TCP-LISTEN:5432,fork,bind=127.0.0.1 TCP:192.168.64.3:5432

# 验证端口监听
netstat -an | grep 5432
tcp4 0 0 127.0.0.1.5432 *.* LISTEN

# 测试连接
telnet localhost 5432
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.

现在你可以使用 DBeaver 等数据库客户端工具连接到容器内的 PostgreSQL 数据库。

重要提示:由于 Apple Container 目前不支持 -p 参数进行本地到远程端口绑定,需要使用 socat 进行端口转发。

镜像发布

步骤 5:发布你的镜像

登录容器注册表

1
2
3
4
5
container registry login docker.io

Provide registry username docker.io: yourusername
Provide registry password:
Login succeeded

为镜像打标签

1
2
3
container images tag my-apple-postgresql docker.io/yourusername/my-apple-postgresql:latest

## Image my-apple-postgresql tagged as docker.io/yourusername/my-apple-postgresql:latest

推送镜像

1
container images push docker.io/yourusername/my-apple-postgresql:latest

系统会提示你授权注册表访问并输入管理员密码。

1
⠴ Pushing image docker.io/yourusername/my-apple-postgresql:latest 29% (14 of 16 blobs, 42.2/142.6 MB) [1m 1s]

推送完成后,你的镜像将可供其他人在任何 OCI 兼容平台(包括 Docker、Podman 或 Kubernetes)上拉取和运行。

高级命令

步骤 6:掌握高级用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 查看所有容器命令
container --help

# 控制分配给容器的内存和 CPU
container builder start --cpus 4 --memory 8g

# 为 Apple Silicon (arm64) 和 Intel (amd64) 构建镜像
container build --arch arm64 --arch amd64 --tag my-postgres-arch-image .

# 检查容器(输出 JSON 格式的容器配置)
container inspect --debug my-apple-postgresql

# 设置 docker 别名
alias docker='container'

docker ls

# 查看容器日志
container logs -f my-apple-postgresql

# 停止容器
container stop my-apple-postgresql

# 列出镜像
container images ls

# 删除镜像
container image rm my-apple-postgresql:latest

总结

为什么这很重要?

通过这一举措,苹果可能正在将 Docker 和 Podman 从 macOS 容器开发者的宝座上推下来。由于它与任何 OCI 兼容平台完美协作,你的容器仍然具有可移植性——只是比以往更快、更精简、更具”Mac 风格”。

如果你是 macOS 高级用户、Swift 开发者或容器技术爱好者——这个工具值得认真了解。苹果刚刚为你提供了一个优雅的新工具,是时候体验一下了!

参考资料


本文基于 Apple Container 0.1.0 版本撰写,随着技术发展,部分内容可能会有所变化。建议查看官方文档获取最新信息。