Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit d099c39

Browse files
tomerdyim-leektosoweissi
authored
add packaging guide (#12)
motivation: provide guidance around how an aplication can be packaged for deployments changes: add packaging.md wih instructions for docker, tarball and source distributions Co-authored-by: Yim Lee <yim_lee@apple.com> Co-authored-by: Konrad `ktoso` Malawski <konrad.malawski@project13.pl> Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
1 parent b087cc3 commit d099c39

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

packaging.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Packaging Applications for Deployment
2+
3+
Once an application is built for production, it still needs to be packaged before it can be deployed to servers. There are several strategies for packaging Swift applications for deployment.
4+
5+
## Docker
6+
7+
One of the most popular ways to package applications these days is using container technologies such as [Docker](https://www.docker.com).
8+
9+
Using Docker's tooling, we can build and package the application as a Docker image, publish it to a Docker repository, and later launch it directly on a server or on a platform that supports Docker deployments such as [Kubernetes](https://kubernetes.io). Many public cloud providers including AWS, GCP, Azure, IBM and others encourage this kind of deployment.
10+
11+
Here is an example `Dockerfile` that builds and packages the application on top of CentOS:
12+
13+
```Dockerfile
14+
#------- build -------
15+
FROM swiftlang/swift:nightly-centos8 as builder
16+
17+
# set up the workspace
18+
RUN mkdir /workspace
19+
WORKDIR /workspace
20+
21+
# copy the source to the docker image
22+
COPY . /workspace
23+
24+
RUN swift build -c release
25+
26+
#------- package -------
27+
FROM centos:8
28+
# copy executables
29+
COPY --from=builder /workspace/.build/release/<executable-name> /
30+
# copy Swift's dynamic libraries dependencies
31+
COPY --from=builder /usr/lib/swift/linux/lib*so* /
32+
33+
# set the entry point (application name)
34+
CMD ["<executable-name>"]
35+
```
36+
37+
To create a local Docker image from the `Dockerfile` use the `docker build` command from the application's source location, e.g.:
38+
39+
```bash
40+
$ docker build . -t <my-app>:<my-app-version>
41+
```
42+
43+
To test the local image use the `docker run` command, e.g.:
44+
45+
```bash
46+
$ docker run <my-app>:<my-app-version>
47+
```
48+
49+
Finally, use the `docker push` command to publish the application's Docker image to a Docker repository of your choice, e.g.:
50+
51+
```bash
52+
$ docker tag <my-app>:<my-app-version> <docker-hub-user>/<my-app>:<my-app-version>
53+
$ docker push <docker-hub-user>/<my-app>:<my-app-version>
54+
```
55+
56+
At this point, the application's Docker image is ready to be deployed to the server hosts (which need to run docker), or to one of the platforms that supports Docker deployments.
57+
58+
See [Docker's documentation](https://docs.docker.com/engine/reference/commandline/) for more complete information about Docker.
59+
60+
### Distroless
61+
62+
[Distroless](https://github.com/GoogleContainerTools/distroless) is a project by Google that attempts to create minimal images containing only the application and its runtime dependencies. They do not contain package managers, shells or any other programs you would expect to find in a standard Linux distribution.
63+
64+
Since distroless supports Docker and is based on Debian, packaging a Swift application on it is fairly similar to the Docker process above. Here is an example `Dockerfile` that builds and packages the application on top of a distroless's C++ base image:
65+
66+
```Dockerfile
67+
#------- build -------
68+
# Building using Ubuntu Bionic since its compatible with Debian runtime
69+
FROM swiftlang/swift:nightly-bionic as builder
70+
71+
# set up the workspace
72+
RUN mkdir /workspace
73+
WORKDIR /workspace
74+
75+
# copy the source to the docker image
76+
COPY . /workspace
77+
78+
RUN swift build -c release
79+
80+
#------- package -------
81+
# Running on distroless C++ since it includes
82+
# all(*) the runtime dependencies Swift programs need
83+
FROM gcr.io/distroless/cc-debian10
84+
# copy executables
85+
COPY --from=builder /workspace/.build/release/<executable-name> /
86+
# copy Swift's dynamic libraries dependencies
87+
COPY --from=builder /usr/lib/swift/linux/lib*so* /
88+
89+
# set the entry point (application name)
90+
CMD ["<executable-name>"]
91+
```
92+
93+
Note the above uses `gcr.io/distroless/cc-debian10` as the runtime image which should work for Swift programs that do not use `FoundationNetworking` or `FoundationXML`. In order to provide more complete support we (the community) could put in a PR into distroless to introduce a base image for Swift that includes `libcurl` and `libxml` which are required for `FoundationNetworking` and `FoundationXML` respectively.
94+
95+
## Archive (Tarball, ZIP file, etc.)
96+
97+
Since cross-compiling Swift for Linux is not (yet) supported on Mac or Windows, we need to use virtualization technologies like Docker to compile applications we are targeting to run on Linux.
98+
99+
That said, this does not mean we must also package the applications as Docker images in order to deploy them. While using Docker images for deployment is convenient and popular, an application can also be packaged using a simple and lightweight archive format like tarball or ZIP file, then uploaded to the server where it can be extracted and run.
100+
101+
Here is an example of using Docker and `tar` to build and package the application for deployment on Ubuntu servers:
102+
103+
First, use the `docker run` command from the application's source location to build it:
104+
105+
```bash
106+
$ docker run --rm \
107+
-v "$PWD:/workspace" \
108+
-w /workspace \
109+
swift:5.2-bionic \
110+
/bin/bash -cl "swift build -c release"
111+
```
112+
113+
Note we are bind mounting the source directory so that the build writes the build artifacts to the local drive from which we will package them later.
114+
115+
Next we can create a staging area with the application's executables and Swift's dynamic libraries dependencies:
116+
117+
```bash
118+
$ docker run --rm \
119+
-v "$PWD:/workspace" \
120+
-w /workspace \
121+
swift:5.2-bionic \
122+
/bin/bash -cl ' \
123+
rm -rf .build/install && mkdir -p .build/install && \
124+
cp -P .build/release/<executable-name> .build/install/ && \
125+
cp -P /usr/lib/swift/linux/lib*so* .build/install/'
126+
```
127+
128+
Note this command could be combined with the build command above--we separated them to make the example more readable.
129+
130+
Finally, create a tarball from the staging directory:
131+
132+
```bash
133+
$ tar cvzf <my-app>-<my-app-version>.tar.gz -C .build/install .
134+
```
135+
136+
We can test the integrity of the tarball by extracting it to a directory and running the application in a Docker runtime container:
137+
138+
```bash
139+
$ cd <extracted directory>
140+
$ docker run -v "$PWD:/app" -w /app swift:5.2-bionic-slim ./<executable-name>
141+
```
142+
143+
Deploying the application's tarball to the target server can be done using utilities like `scp`, or in a more sophisticated setup using configuration management system like `chef`, `puppet`, `ansible`, etc.
144+
145+
146+
## Source Distribution
147+
148+
Another distribution technique popular with dynamic languages like Ruby or Javascript is distributing the source to the server, then compiling it on the server itself.
149+
150+
To build Swift applications directly on the server, the server must have the correct Swift toolchain installed. [Swift.org](https://swift.org/download/#linux) publishes toolchains for a variety of Linux distributions, make sure to use the one matching your server Linux version and desired Swift version.
151+
152+
The main advantage of this approach is that it is easy. Additional advantage is the server has the full toolchain (e.g. debugger) that can help troubleshoot issues "live" on the server.
153+
154+
The main disadvantage of this approach that the server has the full toolchain (e.g. compiler) which means a sophisticated attacker can potentially find ways to execute code. They can also potentially gain access to the source code which might be sensitive. If the application code needs to be cloned from a private or protected repository, the server needs access to credentials which adds additional attack surface area.
155+
156+
In most cases, source distribution is not advised due to these security concerns.

0 commit comments

Comments
 (0)