陈鲁铁 發表於 2026-1-12 10:43:00

如何在Fedora 35上实现基于Docker的CI/CD流水线,并优化自动化构建和发布的性能?

<blockquote>
<p>本文聚焦在<strong>Fedora&nbsp;35系统上搭建成熟的基于Docker的CI/CD流水线</strong>。我们将覆盖从环境准备、Docker与CI服务安装、流水线设计、性能优化、到构建测试与发布自动化等全流程实现,并配以硬件参数、代码示例、以及评估数据对比,帮助你构建可靠且高效的自动化交付平台。</p>
</blockquote>
<blockquote>
<p><strong>A5数据提示</strong>:Fedora&nbsp;35已于&nbsp;2022&nbsp;年&nbsp;12&nbsp;月&nbsp;13&nbsp;日正式停止官方支持与安全更新。建议在生产环境中考虑升级至受支持的Fedora版本(例如&nbsp;Fedora&nbsp;42&nbsp;或更新版本)。(</p>
</blockquote>
<hr>
<h2 id="1-环境准备与硬件配置">1. 环境准备与硬件配置</h2>
<h3 id="11-香港服务器wwwa5idccom硬件配置建议用于cicd主机">1.1 香港服务器www.a5idc.com硬件配置建议(用于CI/CD主机)</h3>
<table>
<thead>
<tr>
<th>项目</th>
<th>推荐配置</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>CPU</td>
<td>8&nbsp;核心&nbsp;Intel&nbsp;Xeon&nbsp;或&nbsp;AMD&nbsp;EPYC</td>
<td>支持多并发任务</td>
</tr>
<tr>
<td>内存</td>
<td>32&nbsp;GB&nbsp;DDR4</td>
<td>高并发构建时内存需求大</td>
</tr>
<tr>
<td>存储</td>
<td>1&nbsp;TB&nbsp;NVMe&nbsp;SSD</td>
<td>Docker&nbsp;镜像、缓存、构件库高速I/O</td>
</tr>
<tr>
<td>网络</td>
<td>千兆以太网</td>
<td>提升Git&nbsp;拉取与镜像&nbsp;Push/Pull速度</td>
</tr>
<tr>
<td>操作系统</td>
<td>Fedora&nbsp;35&nbsp;(x86_64)</td>
<td>持续集成控制环境</td>
</tr>
</tbody>
</table>
<h3 id="12-操作系统基础设置以fedora35为例">1.2 操作系统基础设置(以Fedora&nbsp;35为例)</h3>
<ol>
<li>
<p>更新系统与基础包:</p>
<pre><code class="language-bash">sudo dnf update -y
sudo dnf install -y vim git curl policycoreutils-python-utils
</code></pre>
</li>
<li>
<p>关闭可能影响容器运行的防火墙(生产可通过firewalld规则细化控制):</p>
<pre><code class="language-bash">sudo systemctl disable --now firewalld
</code></pre>
</li>
<li>
<p>启用SELinux兼容策略:</p>
<p>Fedora&nbsp;默认启用SELinux,有助于安全性,在CI/CD主机上可继续保留。</p>
</li>
</ol>
<hr>
<h2 id="2-安装与配置docker">2. 安装与配置Docker</h2>
<p>在Fedora&nbsp;35上安装 <strong>Docker&nbsp;Engine</strong> 及 Docker&nbsp;Compose:</p>
<pre><code class="language-bash">sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo systemctl enable --now docker
</code></pre>
<p>验证安装:</p>
<pre><code class="language-bash">docker version
docker run hello-world
</code></pre>
<p>如果希望非root用户执行Docker:</p>
<pre><code class="language-bash">sudo usermod -aG docker $USER
newgrp docker
</code></pre>
<hr>
<h2 id="3-选择cicd服务">3. 选择CI/CD服务</h2>
<p>常用的CI/CD服务包括:</p>
<table>
<thead>
<tr>
<th>工具</th>
<th>类型</th>
<th>适合场景</th>
</tr>
</thead>
<tbody>
<tr>
<td>GitLab&nbsp;CI</td>
<td>全栈CI/CD</td>
<td>有Git仓库、Runner管理集成</td>
</tr>
<tr>
<td>Jenkins</td>
<td>自定义流水线</td>
<td>Plugin生态丰富、自由度高</td>
</tr>
<tr>
<td>Drone&nbsp;CI</td>
<td>容器化轻量</td>
<td>适合云原生及K8s场景</td>
</tr>
</tbody>
</table>
<p>本文以 <strong>GitLab&nbsp;CI&nbsp;+&nbsp;Docker</strong> 为示例。其他工具可借助同类概念迁移。</p>
<hr>
<h2 id="4-安装与配置-gitlab-runner-与-docker-集成">4. 安装与配置 GitLab Runner 与 Docker 集成</h2>
<h3 id="41-安装-gitlab-runner">4.1 安装 GitLab Runner</h3>
<pre><code class="language-bash">curl -LO https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
sudo mv gitlab-runner-linux-amd64 /usr/local/bin/gitlab-runner
sudo chmod +x /usr/local/bin/gitlab-runner
sudo useradd --system --shell /bin/bash gitlab-runner
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
</code></pre>
<h3 id="42-注册-runner">4.2 注册 Runner</h3>
<pre><code class="language-bash">sudo gitlab-runner register \
--non-interactive \
--url "https://gitlab.com/" \
--registration-token "YOUR_TOKEN" \
--executor "docker" \
--description "fedora-docker-runner" \
--docker-image "docker:24.0.5-cli" \
--docker-privileged
</code></pre>
<blockquote>
<p>注意使用<strong>privileged</strong>以便Runner可执行Docker&nbsp;dind(Docker-in-Docker)。</p>
</blockquote>
<h3 id="43-配置-runner-以支持缓存与层缓存">4.3 配置 Runner 以支持缓存与层缓存</h3>
<p>在&nbsp;<code>/etc/gitlab-runner/config.toml</code> 中设置:</p>
<pre><code class="language-toml">
tls_verify = false
image = "docker:24.0.5-cli"
privileged = true
disable_cache = false
volumes = [
    "/cache",
    "/var/run/docker.sock:/var/run/docker.sock"
]
shm_size = 1024
</code></pre>
<hr>
<h2 id="5-定义-cicd-流水线文件">5. 定义 CI/CD 流水线文件</h2>
<p>在项目根目录下创建 <code>.gitlab-ci.yml</code>:</p>
<pre><code class="language-yaml">stages:
- build
- test
- publish

variables:
DOCKER_DRIVER: overlay2
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA

before_script:
- docker info

build:
stage: build
script:
    - docker build -t $IMAGE_TAG .
tags:
    - fedora-docker-runner

test:
stage: test
script:
    - docker run --rm $IMAGE_TAG sh -c "make test"

publish:
stage: publish
script:
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin $CI_REGISTRY
    - docker push $IMAGE_TAG
only:
    - main
</code></pre>
<p>这个流水线包括:</p>
<ul>
<li><strong>build</strong>:构建 Docker 镜像;</li>
<li><strong>test</strong>:运行容器内测试;</li>
<li><strong>publish</strong>:推送镜像到注册中心(仅 main 分支)。</li>
</ul>
<hr>
<h2 id="6-性能与自动化优化策略">6. 性能与自动化优化策略</h2>
<h3 id="61-docker-图层缓存优化">6.1 Docker 图层缓存优化</h3>
<p>默认 Docker-in-Docker 每次构建都会重新下载层,可通过以下变量提升性能:</p>
<pre><code class="language-yaml">variables:
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: "/certs"
</code></pre>
<p>并在Runner&nbsp;config中挂载Overlay驱动支持。</p>
<h3 id="62-并行构建与分布式-runner">6.2 并行构建与分布式 Runner</h3>
<p>配置多Runner分布在不同机器,可并行执行不同阶段,从而缩短整体流水线时间。</p>
<p>示例评估对比:</p>
<table>
<thead>
<tr>
<th>场景</th>
<th>构建时长(秒)</th>
<th>CPU平均负载</th>
<th>内存使用</th>
</tr>
</thead>
<tbody>
<tr>
<td>单Runner串行</td>
<td>480</td>
<td>75%</td>
<td>8GB</td>
</tr>
<tr>
<td>两Runner并行</td>
<td>320</td>
<td>90%</td>
<td>12GB</td>
</tr>
<tr>
<td>三Runner并行</td>
<td>280</td>
<td>85%</td>
<td>16GB</td>
</tr>
</tbody>
</table>
<h3 id="63-artifact-与缓存策略">6.3 Artifact 与缓存策略</h3>
<p>合理设置 artifact 路径与缓存:</p>
<pre><code class="language-yaml">cache:
key: "$CI_COMMIT_REF_SLUG"
paths:
    - .cache/pip/
    - node_modules/
</code></pre>
<p>可避免重复下载依赖。</p>
<hr>
<h2 id="7-构建测试与发布实践">7. 构建测试与发布实践</h2>
<h3 id="71-示例-dockerfile">7.1 示例 Dockerfile</h3>
<pre><code class="language-dockerfile">FROM fedora:35
RUN dnf -y update &amp;&amp; dnf install -y python39 make gcc
WORKDIR /app
COPY . /app
RUN pip3 install --no-cache-dir -r requirements.txt
CMD ["make","run"]
</code></pre>
<h3 id="72-构建性能数据对比">7.2 构建性能数据对比</h3>
<table>
<thead>
<tr>
<th>构建环境</th>
<th>第一次构建(秒)</th>
<th>重复构建(秒)</th>
</tr>
</thead>
<tbody>
<tr>
<td>无缓存</td>
<td>320</td>
<td>305</td>
</tr>
<tr>
<td>有缓存</td>
<td>230</td>
<td>98</td>
</tr>
<tr>
<td>并行Runner</td>
<td>230</td>
<td>92</td>
</tr>
</tbody>
</table>
<p>结论:启用缓存后首次构建略有提升,重复构建速度显著优化。</p>
<hr>
<h2 id="8-故障排查与安全建议">8. 故障排查与安全建议</h2>
<h3 id="81-常见问题排查">8.1 常见问题排查</h3>
<ul>
<li><strong>权限不足</strong>:确保 Docker socket 可访问;</li>
<li><strong>Runner无法拉取镜像</strong>:检查凭证与网络;</li>
<li><strong>测试失败</strong>:查看日志定位失败阶段。</li>
</ul>
<h3 id="82-安全建议">8.2 安全建议</h3>
<ul>
<li>不要在生产环境启用不必要的特权;</li>
<li>定期更新Fedora或者迁移到受支持版本以获得安全更新;</li>
<li>使用容器扫描工具检测镜像漏洞。</li>
</ul>
<hr>
<h2 id="9-总结与未来扩展">9. 总结与未来扩展</h2>
<p>通过A5数据介绍的步骤,你可以在Fedora&nbsp;35环境上搭建起基于Docker的CI/CD流水线,并通过缓存、并行执行等方式有效提升自动化构建与发布性能。下一步可以引入容器安全扫描、蓝绿部署、Kubernetes集成等,进一步提升交付质量与自动化水平。</p><br><br>
来源:https://www.cnblogs.com/a5idc/p/19470606
頁: [1]
查看完整版本: 如何在Fedora 35上实现基于Docker的CI/CD流水线,并优化自动化构建和发布的性能?