Skip to content

Commit 622f730

Browse files
authored
Set the default arguments passed to the entrypoint (#155)
Motivation ---------- Allow configuring the CMD entry of the generated image config. Modifications ------------- Added the argument parsing in containertool.swift and pass it to the publish method. Given the CMD entries are often options themselves, the .remaining parsing strategy is used. This means this option must be the last. Also means that in ContainerImageBuilder/main.swift, the order has been altered when building the arguments used to call the containertool process. Result ------ Can configure CMD entry, e.g. for a Vapor example swift package --swift-sdk aarch64-swift-linux-musl build-container-image --repository localhost:5000/hello --cmd "serve" "--env" "production" "--hostname" "0.0.0.0" "--port" "8080" Fixes: #89 Test Plan --------- Added a test-containertool-cmd.sh script to perform some basic testing.
1 parent 6dbb4af commit 622f730

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

Plugins/ContainerImageBuilder/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ extension PluginError: CustomStringConvertible {
9999
let helperURL = try context.tool(named: "containertool").url
100100
let helperArgs =
101101
(FileManager.default.fileExists(atPath: resources.path) ? ["--resources", resources.path] : [])
102-
+ extractor.remainingArguments
103102
+ builtExecutables.map { $0.url.path }
103+
+ extractor.remainingArguments
104104
let helperEnv = ProcessInfo.processInfo.environment.filter { $0.key.starts(with: "CONTAINERTOOL_") }
105105

106106
let err = Pipe()

Sources/containertool/Extensions/RegistryClient+publish.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func publishContainerImage<Source: ImageSource, Destination: ImageDestination>(
2525
destination: Destination,
2626
architecture: String,
2727
os: String,
28+
cmd: [String],
2829
resources: [String],
2930
tag: String?,
3031
verbose: Bool,
@@ -80,7 +81,7 @@ func publishContainerImage<Source: ImageSource, Destination: ImageDestination>(
8081
// and override the entrypoint.
8182
var inheritedConfiguration = baseImageConfiguration.config ?? .init()
8283
inheritedConfiguration.Entrypoint = ["/\(executableURL.lastPathComponent)"]
83-
inheritedConfiguration.Cmd = []
84+
inheritedConfiguration.Cmd = cmd
8485
inheritedConfiguration.WorkingDir = "/"
8586

8687
let configuration = ImageConfiguration(

Sources/containertool/containertool.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
6464

6565
@Option(help: "Operating system")
6666
var os: String?
67+
68+
@Option(parsing: .remaining, help: "Default arguments to pass to the entrypoint process")
69+
var cmd: [String] = []
6770
}
6871

6972
@OptionGroup(title: "Image configuration options")
@@ -222,6 +225,7 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
222225
destination: destination,
223226
architecture: architecture,
224227
os: os,
228+
cmd: imageConfigurationOptions.cmd,
225229
resources: imageBuildOptions.resources,
226230
tag: repositoryOptions.tag,
227231
verbose: verbose,

Sources/swift-container-plugin/Documentation.docc/build-container-image.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ Wrap a binary in a container image and publish it.
5656
- term `--os <os>`:
5757
Operating system required to run the image. (default: `linux`)
5858

59+
- term `--cmd <cmd1> <cmd2> …`:
60+
Default arguments to pass to the entrypoint process.
61+
This MUST be the last option present as all following arguments are considered part of the CMD entry.
62+
5963
### Authentication options
6064

6165
- term `--default-username <username>`:

scripts/test-containertool-cmd.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bash
2+
##===----------------------------------------------------------------------===##
3+
##
4+
## This source file is part of the SwiftContainerPlugin open source project
5+
##
6+
## Copyright (c) 2025 Apple Inc. and the SwiftContainerPlugin project authors
7+
## Licensed under Apache License v2.0
8+
##
9+
## See LICENSE.txt for license information
10+
## See CONTRIBUTORS.txt for the list of SwiftContainerPlugin project authors
11+
##
12+
## SPDX-License-Identifier: Apache-2.0
13+
##
14+
##===----------------------------------------------------------------------===##
15+
16+
#
17+
# This script assumes that the Static Linux SDK has already been installed
18+
#
19+
20+
log() { printf -- "** %s\n" "$*" >&2; }
21+
error() { printf -- "** ERROR: %s\n" "$*" >&2; }
22+
fatal() { error "$@"; exit 1; }
23+
24+
set -euo pipefail
25+
26+
RUNTIME=${RUNTIME-"docker"}
27+
28+
#
29+
# Create a test package
30+
#
31+
PKGPATH=$(mktemp -d)
32+
swift package --package-path "$PKGPATH" init --type executable --name hello
33+
34+
cleanup() {
35+
log "Deleting temporary package $PKGPATH"
36+
rm -rf "$PKGPATH"
37+
}
38+
trap cleanup EXIT
39+
40+
#
41+
# Build and package an aarch64 binary
42+
#
43+
swift build --package-path "$PKGPATH" --swift-sdk aarch64-swift-linux-musl
44+
45+
IMGREF=$(swift run containertool --repository localhost:5000/elf_test "$PKGPATH/.build/aarch64-swift-linux-musl/debug/hello" --from scratch --cmd "arg1" "--option" "opt" "--flag")
46+
$RUNTIME pull "$IMGREF"
47+
CMD=$($RUNTIME inspect "$IMGREF" --format "{{.Config.Cmd}}")
48+
if [ "$CMD" = "[arg1 --option opt --flag]" ] ; then
49+
log "cmd option: PASSED"
50+
else
51+
fatal "cmd option: FAILED - cmd was $CMD; expected [arg1 --option opt --flag]"
52+
fi

0 commit comments

Comments
 (0)