You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
7
+
一旦应用程序构建用于生产,它仍然需要打包才能部署到服务器上。打包 Swift 应用程序以进行部署有多种策略。
8
8
9
9
## Docker
10
10
11
-
One of the most popular ways to package applications these days is using container technologies such as [Docker](https://www.docker.com).
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.
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.
See [Docker's documentation](https://docs.docker.com/engine/reference/commandline/) for more complete information about Docker.
61
63
62
64
### Distroless
63
65
64
-
[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.
66
+
[Distroless](https://github.com/GoogleContainerTools/distroless) 是 Google 的一个项目,旨在创建仅包含应用程序及其运行时依赖项的最小镜像。它们不包含包管理器、shell 或任何其他您期望在标准 Linux 发行版中找到的程序。
67
+
65
68
66
-
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:
69
+
由于 distroless 支持 Docker 并基于 Debian,将 Swift 应用程序打包到其中的过程与上述 Docker 过程十分相似。下面是一个在 distroless 的 C++ 基础镜像上构建和打包应用程序的 `Dockerfile`示例:
67
70
68
71
```Dockerfile
69
72
#------- build -------
70
-
#Building using Ubuntu Bionic since its compatible with Debian runtime
73
+
#使用 Ubuntu Bionic 构建,因为它与 Debian 运行时兼容
71
74
FROM swift:bionic as builder
72
75
73
-
#set up the workspace
76
+
#设置工作区
74
77
RUN mkdir /workspace
75
78
WORKDIR /workspace
76
79
77
-
#copy the source to the docker image
80
+
#将源代码复制到 docker 镜像
78
81
COPY . /workspace
79
82
80
83
RUN swift build -c release --static-swift-stdlib
81
84
82
85
#------- package -------
83
-
#Running on distroless C++ since it includes
84
-
#all(*) the runtime dependencies Swift programs need
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.
96
+
注意,上述使用 `gcr.io/distroless/cc-debian10`作为运行时镜像,这对于不使用 `FoundationNetworking`或`FoundationXML` 的 Swift 程序应该有效。为了提供更完整的支持,我们(社区)可以向 distroless 提交 PR,引入一个包含 `libcurl`和`libxml`的 Swift 基础镜像,分别用于 `FoundationNetworking`和`FoundationXML`。
94
97
95
-
## Archive (Tarball, ZIP file, etc.)
96
98
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.
99
+
## 归档(Tarball、 ZIP 文件等)
98
100
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.
101
+
由于在 Mac 或 Windows 上不(尚未)支持交叉编译 Swift 到 Linux,我们需要使用虚拟化技术(如 Docker)来编译我们目标运行在 Linux 上的应用程序。
100
102
101
-
Here is an example of using Docker and `tar` to build and package the application for deployment on Ubuntu servers:
103
+
这并不意味着我们必须将应用程序也打包为 Docker 镜像才能部署。虽然使用 Docker 镜像进行部署方便且流行,但应用程序也可以使用简单且轻量的归档格式(如 tarball 或 ZIP 文件)打包,然后上传到服务器,在服务器上解压并运行。
102
104
103
-
First, use the `docker run` command from the application's source location to build it:
$ docker run -v "$PWD:/app" -w /app bionic ./<executable-name>
140
145
```
141
146
142
-
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.
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.
To build Swift applications directly on the server, the server must have the correct Swift toolchain installed. [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.
154
+
要直接在服务器上构建 Swift 应用程序,服务器必须安装正确的 Swift 工具链。[Swift.org](https://www.swift.org/download/#linux)发布了适用于各种 Linux 发行版的工具链,请确保使用与您的服务器 Linux 版本和所需 Swift 版本匹配的工具链。
150
155
151
-
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.
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.
In most cases, source distribution is not advised due to these security concerns.
160
+
在大多数情况下,由于这些安全问题,不建议使用源代码分发。
156
161
157
-
## Static linking and Curl/XML
162
+
## 静态链接和 Curl/XML
158
163
159
-
**Note:**if you are compiling with `-static-stdlib`and using Curl with FoundationNetworking or XML with FoundationXML you must have libcurl and/or libxml2 installed on the target system for it to work.
0 commit comments