Skip to content

Commit 3d334d1

Browse files
euanhheckj
andauthored
docs: Add examples of building and publishing images (#108)
Motivation ---------- As with authentication configuration, it is useful for new users to be able to refer to concrete examples showing how to use the plugin. Modifications ------------- * Add a section to the 'run' document showing how to build and run an image in one command. * Add sections showing how to publish images to some popular public container registries. Result ------ Users can refer to examples showing how to use the plugin to build and run images. Test Plan --------- Steps tested manually. All automated tests continue to pass. --------- Co-authored-by: Joseph Heck <[email protected]>
1 parent 3326f8f commit 3d334d1

File tree

8 files changed

+71
-16
lines changed

8 files changed

+71
-16
lines changed

Package.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ let package = Package(
7878
),
7979
dependencies: [.target(name: "containertool")]
8080
),
81-
// Empty target that builds the DocC catalog at /SwiftContainerPluginDocumentation/Documentation.docc.
82-
// The ContainerImageBuilder catalog includes high-level, user-facing documentation about using
83-
// the ContainerImageBuilder plugin from the command-line.
81+
// Empty target which builds high-level, user-facing documentation about using the plugin from the command-line.
8482
.target(
8583
name: "swift-container-plugin",
8684
exclude: ["README.md"]

Sources/containertool/Documentation.docc/containertool.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Wrap a binary in a container image and publish it.
1313
`containertool` can be run directly but its main role is to be a helper tool used by the `ContainerImageBuilder` command plugin. See the plugin documentation for examples of how to use it in this way.
1414

1515
```text
16-
OVERVIEW: Build and upload a container image
16+
OVERVIEW: Build and publish a container image
1717
1818
USAGE: containertool --repository <repository> <executable> [--username <username>] [--password <password>] [--verbose] [--allow-insecure-http <allow-insecure-http>] [--architecture <architecture>] --from <from> [--os <os>] [--tag <tag>]
1919

Sources/containertool/containertool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
2525
@main struct ContainerTool: AsyncParsableCommand {
2626
static let configuration = CommandConfiguration(
2727
commandName: "containertool",
28-
abstract: "Build and upload a container image"
28+
abstract: "Build and publish a container image"
2929
)
3030

3131
@Option(help: "Default registry for references which do not specify a registry")

Sources/swift-container-plugin/Documentation.docc/Adding-the-plugin-to-your-project.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Swift Container Plugin is distributed as a Swift Package Manager package. To
1414
Recent versions of `swift package` support the `add-dependency` command:
1515

1616
```shell
17-
swift package add-dependency https://github.com/apple/swift-container-plugin --from 0.5.0
17+
swift package add-dependency https://github.com/apple/swift-container-plugin --from 1.0.0
1818
```
1919

2020
### Install the plugin by manually editing `Package.swift`
@@ -23,7 +23,7 @@ If you cannot use the `swift package add-dependency` comand, append the followin
2323

2424
```swift
2525
package.dependencies += [
26-
.package(url: "https://github.com/apple/swift-container-plugin", from: "0.5.0"),
26+
.package(url: "https://github.com/apple/swift-container-plugin", from: "1.0.0"),
2727
]
2828
```
2929

Sources/swift-container-plugin/Documentation.docc/Swift-Container-Plugin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ To use the plugin:
2929
### Build and publish a container image
3030

3131
After adding the plugin to your project, you can build and publish a container image in one step.
32-
Here is how to build the [HelloWorld example](https://github.com/apple/swift-container-plugin/tree/main/Examples/HelloWorldHummingbird) as a static binary for Linux running the `x86_64` architecture:
32+
Here is how to build the [HelloWorld example](https://github.com/apple/swift-container-plugin/tree/main/Examples/HelloWorldHummingbird) as a static executable for Linux running the `x86_64` architecture:
3333

3434
```
3535
% swift package --swift-sdk x86_64-swift-linux-musl \

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

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# Build and package your service
22

3-
Build a container image and upload it to a registry.
3+
Build a container image and publish it to a registry.
44

55
## Overview
66

7-
The plugin exposes the command `build-container-image` which you invoke to build your service, package it in a container image and upload it to a container registry, in a single command:
7+
The plugin exposes the command `build-container-image` which you invoke to build your service, package it in a container image and publish it to a container registry, in a single command:
88

99
```shell
1010
% swift package --swift-sdk x86_64-swift-linux-musl \
1111
build-container-image --from swift:slim --repository registry.example.com/myservice
1212
```
1313

14-
* The `--swift-sdk` argument specifies the Swift SDK with which to build the executable. In this case we are using the Static Linux SDK, installed earlier, to build an statically-linked x86_64 Linux binary.
14+
* The `--swift-sdk` argument specifies the Swift SDK with which to build the executable. In this case we are using the Static Linux SDK, [installed earlier](<doc:requirements>), to build an statically-linked x86_64 Linux executable.
1515
* The `--from` argument specifies the base image on which our service will run. `swift:slim` is the default, but you can choose your own base image or use `scratch` if your service does not require a base image at all.
16-
* The `--repository` argument specifies the repository to which the plugin will upload the finished image.
16+
* The `--repository` argument specifies the repository to which the plugin will publish the finished image.
1717

1818
> Note: on macOS, the plugin needs permission to connect to the network to publish the image to the registry.
1919
>
@@ -43,3 +43,45 @@ registry.example.com/myservice@sha256:a3f75d0932d052dd9d448a1c9040b16f9f2c2ed919
4343
4444
When the plugin finishes, it prints a reference identifying the new image.
4545
Any standard container runtime can use the reference to pull and run your service.
46+
47+
### Default registry
48+
49+
If you don't include a registry name in the `--repository` argument, the plugin will publish your image to Docker Hub by default.
50+
51+
You can override the default registry by using the `--default-registry` argument or setting the `CONTAINERTOOL_DEFAULT_REGISTRY` environment variable.
52+
53+
The following examples show how to publish images to some popular registry providers.
54+
55+
### Docker Hub
56+
57+
The following example publishes an image to a repository named `mydockerid/example` on Docker Hub.
58+
59+
The repository will be created if it does not already exist.
60+
61+
```
62+
swift package --swift-sdk x86_64-swift-linux-musl build-container-image \
63+
--repository mydockerid/example
64+
```
65+
66+
### GitHub Container Registry
67+
68+
The following example publishes an image to a repository named `mydockerid/example` on GitHub Container Registry.
69+
70+
The repository will be created if it does not already exist.
71+
72+
```
73+
swift package --swift-sdk x86_64-swift-linux-musl build-container-image \
74+
--repository ghcr.io/mygithubusername/example
75+
```
76+
77+
### Amazon Elastic Container Registry
78+
79+
The following example publishes an image to the repository named `example/test` on Amazon Elastic Container Registry.
80+
81+
**The repository must already exist before you push to it.**
82+
Create a repository using the [Amazon ECR Console](https://docs.aws.amazon.com/AmazonECR/latest/userguide/repository-create.html).
83+
84+
```
85+
swift package --swift-sdk x86_64-swift-linux-musl build-container-image \
86+
--repository 123456789012.dkr.ecr.us-west-2.amazonaws.com/example/test
87+
```

Sources/swift-container-plugin/Documentation.docc/run.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,20 @@ When the service has started, we can access it with a web browser or `curl`:
1818
```
1919
% curl localhost:8080
2020
Hello World, from Hummingbird on Ubuntu 24.04.2 LTS
21-
```
21+
```
22+
23+
### Build and run in one step
24+
25+
Swift Container Plugin prints a reference to the newly built image on standard output.
26+
You can pipe the image reference to a deployment command or pass it as an argument using shell command substitution.
27+
28+
This allows you to build and deploy a container image in one shell command, using a convenient pattern offered by tools such as [ko.build](https://ko.build):
29+
30+
```
31+
% podman run -p 8080:8080 \
32+
$(swift package --swift-sdk x86_64-linux-swift-musl \
33+
build-container-image --repository registry.example.com/myservice)
34+
Trying to pull registry.example.com/myservice@sha256:a3f75d0932d052dd9d448a1c9040b16f9f2c2ed9190317147dee95a218faf1df...
35+
...
36+
2024-05-26T22:57:50+0000 info HummingBird : [HummingbirdCore] Server started and listening on 0.0.0.0:8080
37+
```

Sources/swift-container-plugin/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
# ContainerImageBuilder Plugin Documentation
1+
# Swift Container Plugin User Documentation
22

3-
`ContainerImageBuilderPluginDocumentation` is an otherwise empty target that includes high-level,
4-
user-facing documentation about using the ContainerImageBuilder plugin from the command-line.
3+
This is an otherwise empty target that includes high-level, user-facing documentation about using the plugin from the command-line.
54

65
To preview the documentation, run the following command:
76

0 commit comments

Comments
 (0)