在 Ubuntu VPS 上快速编译 Swift Vapor Project:修订间差异
来自md5.pw
更多语言
更多操作
创建页面,内容为“前置说明:<blockquote>正常情况下,使用 Docker 编译 Vapor 项目,Swift 的编译缓存是无法使用的,也就是说每次编译 Vapor 项目都需要在 Docker 中将 swift-package、whole compile 全走一遍,我的云服务器在未优化流程之前,每次可能需要耗费十分钟以上。</blockquote>测试编译的机器配置: '''Ubuntu 24.04, 2GB RAM, 2 vCPU''' == 原理 == Vapor 提供的 Dockerfile 文件是将项目的编…” |
小无编辑摘要 |
||
| 第19行: | 第19行: | ||
== 更多披露 == | == 更多披露 == | ||
我在 Vapor 项目中新建了一个 <code>fastDockerfile</code>, 内容如下:<syntaxhighlight lang="bash | 我在 Vapor 项目中新建了一个 <code>fastDockerfile</code>, 内容如下:<syntaxhighlight lang="bash"> | ||
# ================================ | # ================================ | ||
# Copy Resources Image | # Copy Resources Image | ||
| 第69行: | 第69行: | ||
ENTRYPOINT ["./Run"] | ENTRYPOINT ["./Run"] | ||
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"] | CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"] | ||
</syntaxhighlight> | |||
==== '''然后编写了一个脚本 <code>fast_build.sh</code>:''' ==== | |||
<syntaxhighlight lang="bash"> | |||
#!/bin/bash | #!/bin/bash | ||
| 第106行: | 第108行: | ||
echo "🚀 > 🎉🎉🎉 all of done ~" | echo "🚀 > 🎉🎉🎉 all of done ~" | ||
</syntaxhighlight> | </syntaxhighlight>🎉 OK,最后在服务器上快速编译时,使用 `bash fast_build.sh` 即可使用上次编译的缓存,将编译时间缩短至 1 分钟以内。 | ||
2026年4月15日 (三) 11:51的版本
前置说明:
正常情况下,使用 Docker 编译 Vapor 项目,Swift 的编译缓存是无法使用的,也就是说每次编译 Vapor 项目都需要在 Docker 中将 swift-package、whole compile 全走一遍,我的云服务器在未优化流程之前,每次可能需要耗费十分钟以上。
测试编译的机器配置:
Ubuntu 24.04, 2GB RAM, 2 vCPU
原理
Vapor 提供的 Dockerfile 文件是将项目的编译、复制等操作都放到了 Docker 容器中进行,而 Docker 容器无法缓存编译信息(意思是下次编译时无法获取到上次的编译缓存进行增量编译),所以每次都会全量一遍。
而 Vapor 运行,只需要编译后的 Run 产物和一些必要的 Public、Resource 资源。
依照这个思路,我们可以将 Vapor 的编译放置到机器上进行,编译完成后,将 Run 和 Public、Resource 资源拷贝进 Docker 中即可。
⚠️ 注意
编译机器的 Swift 安装路径最好与 Docker 中的 swift 安装路径保持一致,不然可能会导致 Docker 中运行时找不到动态库之类的情况。
安装 Swift 可参考: 在搬瓦工VPS上安装 Swift 和他的依赖
该安装方式与 Swift Docker 保持一致,我正在使用该方式。
更多披露
我在 Vapor 项目中新建了一个 fastDockerfile, 内容如下:
# ================================
# Copy Resources Image
# ================================
FROM ubuntu:24.04 AS build
# Set up a build area
WORKDIR /build
# Copy entire repo into container
COPY . .
# Switch to the staging area
WORKDIR /staging
# Copy main executable to staging area
COPY ./Run ./
# Copy any resources from the public directory and views directory if the directories exist
# Ensure that by default, neither the directory nor any of its contents are writable.
RUN [ -d /build/Public ] && { mv /build/Public ./Public && chmod -R a-w ./Public; } || true
RUN [ -d /build/Resources ] && { mv /build/Resources ./Resources && chmod -R a-w ./Resources; } || true
# ================================
# Run image
# ================================
FROM swift:6.2.1-focal-slim
# Create a vapor user and group with /app as its home directory
RUN useradd --user-group --create-home --system --skel /dev/null --home-dir /app vapor
# Switch to the new home directory
WORKDIR /app
# Copy built executable and any staged resources from builder
COPY --from=build --chown=vapor:vapor /staging /app
# Ensure all further commands run as the vapor user
USER vapor:vapor
# Let Docker bind to port 8080
EXPOSE 8080
# Set Language Encoding
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
# Start the Vapor service when the image is run, default to listening on 8080 in production environment
ENTRYPOINT ["./Run"]
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]
然后编写了一个脚本 fast_build.sh:
#!/bin/bash
echo "🚀 fast building start"
# stop when happen error
set -e
echo "🚀 > git pull"
git pull
echo "🚀 > git submodule update --remote"
git submodule update --remote
echo "🚀 > swift build"
swift build -c release
# move run to .
echo "🚀 > copy {Run} to ./Run"
cp .build/x86_64-unknown-linux-gnu/release/Run ./Run
echo "🚀 > docker build"
docker build -f fastDockerfile . -t your-vapor-project-name-on-docker:latest
echo "🚀 > rm local ./Run"
rm -rf ./Run
# redirect
echo "🚀 > sh redirect.main.sh"
docker-compose -f docker-compose.yml up -d
# force clean <none> docker images
echo "🚀 > clean invalid images"
docker image rm -f $(docker images -f dangling=true -q)
echo "🚀 > 🎉🎉🎉 all of done ~"
🎉 OK,最后在服务器上快速编译时,使用 `bash fast_build.sh` 即可使用上次编译的缓存,将编译时间缩短至 1 分钟以内。