Swift容器插件
Swift Container Plugin

原始链接: https://github.com/apple/swift-container-plugin

Swift 容器插件增强了 Swift 包管理器,使其能够构建和发布 Swift 服务器的容器镜像,简化了云部署流程。这简化了将 `Package.swift` 中定义的任何可执行产品打包到容器中的过程。 添加插件后,只需一条命令即可为特定的 Linux 环境构建镜像并将其发布到容器注册表。插件会提示网络权限,以允许将镜像上传到注册表。 此过程涉及构建应用程序,基于依赖项和应用程序代码创建层,然后上传这些层。最终输出是一个容器镜像引用,允许您部署到云平台或使用 `podman` 等工具在本地运行。 该插件需要 Swift 6.0+,可在 macOS 和 Linux 上运行。在 macOS 上,需要使用 Linux SDK(如 Swift 静态 Linux SDK)进行交叉编译。构建镜像不需要容器运行时,但运行镜像需要。提供了一些使用示例来帮助您入门。

Hacker News 上的一个讨论围绕着苹果新的 Swift 容器插件展开。许多人赞赏 Swift 的设计以及其在服务器端开发的潜力,但编译速度慢以及严重依赖苹果生态系统等持续存在的问题是主要的担忧。一些人认为 Swift 的发展过于以苹果为中心,阻碍了跨平台的采用和功能开发。另一些人则赞扬 Swift 在构建最终用户产品方面的实用性及其与 C/C++ 集成的便捷性。 编译时间问题和 Xcode 等工具的质量经常受到批评。几位评论者分享了编译器频繁崩溃和类型检查缓慢的经历。macOS 上缺乏原生容器技术以及围绕苹果对跨平台支持的承诺的不确定性也受到了讨论,促使人们呼吁苹果放弃对 Swift 未来的一些控制。尽管存在这些批评,许多人仍然对 Swift 的潜力持乐观态度,特别是作为服务器端应用程序和通用跨平台开发的语言。

原文

Build and publish container images using Swift Package Manager.

Container images are the standard way to package cloud software today. Once you have packaged your server in a container image, you can deploy it on any container-based public or private cloud service, or run it locally using a desktop container runtime.

Swift Container Plugin makes it easy to build container images for servers written in Swift, using Swift Package Manager.

Find out more and see it in action:

Swift Container Plugin can package any executable product defined in Package.swift in a container image and publish it to a container registry.

Build and publish a container image

After adding the plugin to your project, you can build and publish a container image in one step:

% swift package --swift-sdk x86_64-swift-linux-musl \
        build-container-image --repository registry.example.com/myservice
...
Plugin ‘ContainerImageBuilder’ wants permission to allow all network connections on all ports.
Stated reason: “This command publishes images to container registries over the network”.
Allow this plugin to allow all network connections on all ports? (yes/no) yes
...
Building for debugging...
Build of product 'containertool' complete! (4.95s)
...
Build of product 'hello-world' complete! (5.51s)
...
[ContainerImageBuilder] Found base image manifest: sha256:7bd643386c6e65cbf52f6e2c480b7a76bce8102b562d33ad2aff7c81b7169a42
[ContainerImageBuilder] Found base image configuration: sha256:b904a448fde1f8088913d7ad5121c59645b422e6f94c13d922107f027fb7a5b4
[ContainerImageBuilder] Built application layer
[ContainerImageBuilder] Uploading application layer
[ContainerImageBuilder] Layer sha256:dafa2b0c44d2cfb0be6721f079092ddf15dc8bc537fb07fe7c3264c15cb2e8e6: already exists
[ContainerImageBuilder] Layer sha256:2565d8e736345fc7ba44f9b3900c5c20eda761eee01e01841ac7b494f9db5cf6: already exists
[ContainerImageBuilder] Layer sha256:2c179bb2e4fe6a3b8445fbeb0ce5351cf24817cb0b068c75a219b12434c54a58: already exists
registry.example.com/myservice@sha256:a3f75d0932d052dd9d448a1c9040b16f9f2c2ed9190317147dee95a218faf1df

You can deploy your service in the cloud, or use a standards-compliant container runtime such as podman to run it locally:

% podman run -p 8080:8080 registry.example.com/myservice@sha256:a3f75d0932d052dd9d448a1c9040b16f9f2c2ed9190317147dee95a218faf1df
Trying to pull registry.example.com/myservice@sha256:a3f75d0932d052dd9d448a1c9040b16f9f2c2ed9190317147dee95a218faf1df...
...
2024-05-26T22:57:50+0000 info HummingBird : [HummingbirdCore] Server started and listening on 0.0.0.0:8080

Platforms and requirements

  • Swift Container Plugin runs on macOS and Linux and requires Swift 6.0 or later.
  • On macOS you must install a cross-compilation Swift SDK, such as the Swift Static Linux SDK, in order to build executables which can run on Linux-based cloud infrastructure.
  • A container runtime is not required to build an image, but one must be available wherever the image is to be run.

Learn more about setting up your project in the plugin documentation.

Take a look at the Examples.

联系我们 contact @ memedata.com