1. 减少不必要的操作
目前的 CI 配置在 before_script 中执行了很多步骤,包括下载和解压 Docker 二进制文件。考虑到现代 CI 环境中通常已经安装好了 Docker,这些步骤可以省略:
yaml
复制代码
before_script: - docker --version - dockerd & - sleep 5 - echo "${ALIYUN_PASSWORD}" | docker login --username="${ALIYUN_USERNAME}" --password-stdin registry.cn-hangzhou.aliyuncs.com
2. 使用缓存来加速构建
Docker 构建时,缓存可以显著加速。确保 Dockerfile 中的缓存步骤合理使用,尤其是避免缓存失效。例如:
- 把频繁变动的部分(如
COPY . .
和RUN
指令)放在 Dockerfile 的最后。 - 使用 GitLab 提供的缓存机制保存已构建的 Docker 镜像层。
yaml
复制代码
build_image: stage: build image: docker:19.03.12 script: - docker build -t registry.cn-hangzhou.aliyuncs.com/lemonth/${CI_COMMIT_SHORT_SHA} -f _ci/Dockerfile . - docker push registry.cn-hangzhou.aliyuncs.com/lemonth/gitlab-cicd:${CI_COMMIT_SHORT_SHA} cache: paths: - /var/lib/docker only: - web
3. 使用 Docker 缓存加速
通过 GitLab 缓存机制保存 Docker 的缓存层:
yaml
复制代码
variables: DOCKER_DRIVER: overlay2 before_script: - docker --version - dockerd & - sleep 5 - echo "${ALIYUN_PASSWORD}" | docker login --username="${ALIYUN_USERNAME}" --password-stdin registry.cn-hangzhou.aliyuncs.com build_image: stage: build image: docker:19.03.12 cache: key: ${CI_COMMIT_REF_SLUG} paths: - /var/lib/docker script: - docker build --cache-from registry.cn-hangzhou.aliyuncs.com/lemonth/gitlab-cicd:cache -t registry.cn-hangzhou.aliyuncs.com/lemonth/${CI_COMMIT_SHORT_SHA} -f _ci/Dockerfile . - docker push registry.cn-hangzhou.aliyuncs.com/lemonth/gitlab-cicd:${CI_COMMIT_SHORT_SHA} - docker tag registry.cn-hangzhou.aliyuncs.com/lemonth/gitlab-cicd:${CI_COMMIT_SHORT_SHA} registry.cn-hangzhou.aliyuncs.com/lemonth/gitlab-cicd:cache - docker push registry.cn-hangzhou.aliyuncs.com/lemonth/gitlab-cicd:cache only: - web
4. 考虑替代 Docker-in-Docker 的方案
4.1 使用 Docker 兄弟模式
Docker 兄弟模式使用共享宿主机的 Docker 进程,而不是启动新的 Docker 服务。它通过使用 Docker 套接字来共享 Docker 守护进程:
yaml
复制代码
variables: DOCKER_HOST: "tcp://docker:2375/" DOCKER_DRIVER: overlay2 services: - name: docker:19.03.12-dind alias: docker build_image: stage: build image: docker:19.03.12 script: - docker login --username="${ALIYUN_USERNAME}" --password="${ALIYUN_PASSWORD}" registry.cn-hangzhou.aliyuncs.com - docker build -t registry.cn-hangzhou.aliyuncs.com/lemonth/${CI_COMMIT_SHORT_SHA} -f _ci/Dockerfile . - docker push registry.cn-hangzhou.aliyuncs.com/lemonth/gitlab-cicd:${CI_COMMIT_SHORT_SHA} only: - web
4.2 使用 Kaniko 进行构建
Kaniko 是一个可以在 Kubernetes 集群中运行的容器构建工具,它不需要 Docker 守护进程,适用于构建 Docker 镜像:
yaml
复制代码
variables: DOCKER_DRIVER: overlay2 build_image: stage: build image: gcr.io/kaniko-project/executor:latest script: - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile _ci/Dockerfile --destination registry.cn-hangzhou.aliyuncs.com/lemonth/${CI_COMMIT_SHORT_SHA} - docker push registry.cn-hangzhou.aliyuncs.com/lemonth/gitlab-cicd:${CI_COMMIT_SHORT_SHA} only: - web
5. 使用 GitLab 的内置镜像构建支持
GitLab 提供了直接构建 Docker 镜像的支持,这种方法可以避免使用 DinD,大幅提高构建效率:
yaml
复制代码
variables: DOCKER_DRIVER: overlay2 build_image: stage: build image: docker:latest services: - docker:19.03.12-dind script: - docker login --username="${ALIYUN_USERNAME}" --password="${ALIYUN_PASSWORD}" registry.cn-hangzhou.aliyuncs.com - docker build -t registry.cn-hangzhou.aliyuncs.com/lemonth/${CI_COMMIT_SHORT_SHA} -f _ci/Dockerfile . - docker push registry.cn-hangzhou.aliyuncs.com/lemonth/gitlab-cicd:${CI_COMMIT_SHORT_SHA} only: - web
通过上述几种优化和替代方案,你可以显著提高构建速度,避免 Docker-in-Docker 方式的性能瓶颈,同时减少 CI/CD 任务的复杂度和运行时间。