mirror of
https://github.com/apple/container.git
synced 2026-10-17 19:49:20 +00:00
examples: add ctr-example (#295)
Add very basic example of running a Linux container using Containerization Signed-off-by: Eric Ernst <eric_ernst@apple.com>
This commit is contained in:
1 parent
898e3ae6be
commit
51ef9f81fe
6 files changed
+384
No files matched your search
@@ -0,0 +1,74 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Copyright © 2025 Apple Inc. and the Containerization project authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Containerization
|
||||
import ContainerizationOS
|
||||
import Foundation
|
||||
|
||||
@main
|
||||
struct CtrExample {
|
||||
static func main() async throws {
|
||||
print("Starting container example...")
|
||||
|
||||
// Set up terminal in raw mode (like cctl)
|
||||
let current = try Terminal.current
|
||||
try current.setraw()
|
||||
defer { current.tryReset() }
|
||||
|
||||
// Create container manager with file-based initfs
|
||||
var manager = try await ContainerManager(
|
||||
kernel: Kernel(path: URL(fileURLWithPath: "./vmlinux"), platform: .linuxArm),
|
||||
initfsReference: "vminit:latest",
|
||||
network: try ContainerManager.VmnetNetwork()
|
||||
)
|
||||
|
||||
let containerId = "ctr-example"
|
||||
let imageReference = "docker.io/library/alpine:3.16"
|
||||
|
||||
print("Creating container from \(imageReference)...")
|
||||
|
||||
// Create container with simple configuration
|
||||
let container = try await manager.create(
|
||||
containerId,
|
||||
reference: imageReference,
|
||||
rootfsSizeInBytes: 1.gib()
|
||||
) { @Sendable config in
|
||||
config.cpus = 2
|
||||
config.memoryInBytes = 512.mib()
|
||||
config.process.setTerminalIO(terminal: current)
|
||||
config.process.arguments = ["/bin/sh"]
|
||||
config.process.workingDirectory = "/"
|
||||
}
|
||||
|
||||
// Clean up on exit
|
||||
defer {
|
||||
try? manager.delete(containerId)
|
||||
}
|
||||
|
||||
print("Starting container...")
|
||||
try await container.create()
|
||||
try await container.start()
|
||||
|
||||
// Resize terminal to match current window
|
||||
try? await container.resize(to: try current.size)
|
||||
|
||||
// Wait for container to finish
|
||||
let exitCode = try await container.wait()
|
||||
|
||||
print("Container exited with code \(exitCode)")
|
||||
try await container.stop()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user