<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Building Docker images with GitLab CI/CD > 原文:[https://docs.gitlab.com/ee/ci/docker/using_docker_build.html](https://docs.gitlab.com/ee/ci/docker/using_docker_build.html) * [Runner Configuration](#runner-configuration) * [Use shell executor](#use-shell-executor) * [Use Docker-in-Docker workflow with Docker executor](#use-docker-in-docker-workflow-with-docker-executor) * [TLS enabled](#tls-enabled) * [TLS disabled](#tls-disabled) * [Use Docker socket binding](#use-docker-socket-binding) * [Making Docker-in-Docker builds faster with Docker layer caching](#making-docker-in-docker-builds-faster-with-docker-layer-caching) * [How Docker caching works](#how-docker-caching-works) * [Using Docker caching](#using-docker-caching) * [Use the OverlayFS driver](#use-the-overlayfs-driver) * [Requirements](#requirements) * [Use the OverlayFS driver per project](#use-the-overlayfs-driver-per-project) * [Use the OverlayFS driver for every project](#use-the-overlayfs-driver-for-every-project) * [Using the GitLab Container Registry](#using-the-gitlab-container-registry) * [Troubleshooting](#troubleshooting) * [`docker: Cannot connect to the Docker daemon at tcp://docker:2375\. Is the docker daemon running?`](#docker-cannot-connect-to-the-docker-daemon-at-tcpdocker2375-is-the-docker-daemon-running) # Building Docker images with GitLab CI/CD[](#building-docker-images-with-gitlab-cicd "Permalink") GitLab CI / CD 允許您使用 Docker Engine 來構建和測試基于 Docker 的項目. 持續集成/部署中的新趨勢之一是: 1. Create an application image. 2. 針對創建的圖像運行測試. 3. 將映像推送到遠程注冊表. 4. 從推送的映像部署到服務器. 當您的應用程序已經具有可用于創建和測試映像的`Dockerfile` ,它也很有用: ``` docker build -t my-image dockerfiles/ docker run my-image /script/to/run/tests docker tag my-image my-registry:5000/my-image docker push my-registry:5000/my-image ``` 這需要對 GitLab Runner 進行特殊配置才能在作業期間啟用`docker`支持. ## Runner Configuration[](#runner-configuration "Permalink") 有三種方法可在作業期間啟用`docker build`和`docker run`的使用: 每個都有自己的權衡. [使用 docker](using_kaniko.html) `docker build`的替代方法是[使用 kaniko](using_kaniko.html) . 這避免了必須在特權模式下執行 Runner. **提示:**要了解如何在 GitLab.com 上為共享的 Runner 配置 Docker 和 Runner,請參閱[GitLab.com 的共享的 Runners](../../user/gitlab_com/index.html#shared-runners) . ### Use shell executor[](#use-shell-executor "Permalink") 最簡單的方法是在`shell`執行模式下安裝 GitLab Runner. 然后,GitLab Runner 以`gitlab-runner`用戶身份執行作業腳本. 1. Install [GitLab Runner](https://gitlab.com/gitlab-org/gitlab-runner/#installation). 2. 在 GitLab Runner 安裝過程中,選擇`shell`作為執行作業腳本的方法或使用命令: ``` sudo gitlab-runner register -n \ --url https://gitlab.com/ \ --registration-token REGISTRATION_TOKEN \ --executor shell \ --description "My Runner" ``` 3. 在服務器上安裝 Docker Engine. 有關如何在不同系統上安裝 Docker Engine 的更多信息,請查看[支持的安裝](https://s0docs0docker0com.icopy.site/engine/installation/) . 4. Add `gitlab-runner` user to `docker` group: ``` sudo usermod -aG docker gitlab-runner ``` 5. 確認`gitlab-runner`有權訪問 Docker: ``` sudo -u gitlab-runner -H docker info ``` 現在,您可以通過將`.gitlab-ci.yml` `docker info`添加到`.gitlab-ci.yml`來驗證一切正常: ``` before_script: - docker info build_image: script: - docker build -t my-docker-image . - docker run my-docker-image /script/to/run/tests ``` 6. 您現在可以使用`docker`命令(并在需要時**安裝** `docker-compose` ). **注:**通過添加`gitlab-runner`的`docker`您可以有效地授予組`gitlab-runner`完整的 root 權限. 有關更多信息,請閱讀[關于 Docker 安全性: `docker` group 認為是有害的](https://www.andreas-jung.com/contents/on-docker-security-docker-group-considered-harmful) . ### Use Docker-in-Docker workflow with Docker executor[](#use-docker-in-docker-workflow-with-docker-executor "Permalink") 第二種方法是使用特殊泊塢窗功能于泊塢(DIND) [泊塢窗圖像](https://hub.docker.com/_/docker/)安裝(所有工具`docker` ),并在特權模式圖像的上下文中運行作業腳本. **注意:** `docker-compose`不是 Docker-in-Docker(dind)的一部分. 要在 CI 構建中使用`docker-compose` ,請遵循`docker-compose` [安裝說明](https://s0docs0docker0com.icopy.site/compose/install/) .**危險:**通過啟用`--docker-privileged` ,可以有效地禁用容器的所有安全機制,并使主機暴露于特權升級之下,這可能導致容器突破. 有關更多信息,請查看有關[運行時特權和 Linux 功能](https://s0docs0docker0com.icopy.site/engine/reference/run/)的官方 Docker 文檔. Docker-in-Docker 運作良好,是推薦的配置,但并非沒有挑戰: * When using Docker-in-Docker, each job is in a clean environment without the past history. Concurrent jobs work fine because every build gets its own instance of Docker engine so they won’t conflict with each other. But this also means that jobs can be slower because there’s no caching of layers. * 默認情況下,Docker 17.09 及更高版本使用`--storage-driver overlay2` ,這是推薦的存儲驅動程序. 有關詳細信息,請參見[使用 overlayfs 驅動程序](#use-the-overlayfs-driver) . * 由于`docker:19.03.12-dind`容器和 Runner 容器不共享其根文件系統,因此作業的工作目錄可用作子容器的安裝點. 例如,如果您有要與子容器共享的文件,則可以在`/builds/$CI_PROJECT_PATH`下創建一個子目錄,并將其用作安裝點(有關更詳盡的解釋,請[參見問題#41227](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/41227) ): ``` variables: MOUNT_POINT: /builds/$CI_PROJECT_PATH/mnt script: - mkdir -p "$MOUNT_POINT" - docker run -v "$MOUNT_POINT:/mnt" my-docker-image ``` 可在以下位置找到使用此方法的示例項目: [https](https://gitlab.com/gitlab-examples/docker) : [//gitlab.com/gitlab-examples/docker](https://gitlab.com/gitlab-examples/docker) . 在以下示例中,我們使用 Docker images 標簽指定特定版本,例如`docker:19.03.12` . 如果使用了諸如`docker:stable`類的標簽,則您將無法控制要使用的版本,這可能導致無法預測的行為,尤其是在發布新版本時. #### TLS enabled[](#tls-enabled "Permalink") **注意:**需要 GitLab Runner 11.11 或更高版本,但是如果使用[Helm chart](https://docs.gitlab.com/runner/install/kubernetes.html)安裝了 GitLab Runner,則不支持. 有關詳細信息,請參見[相關問題](https://gitlab.com/gitlab-org/charts/gitlab-runner/-/issues/83) . Docker 守護程序支持通過 TLS 的連接,默認情況下,對于 Docker 19.03.12 或更高版本,它已完成. 這是使用 Docker-in-Docker 服務的**建議**方法, [GitLab.com 共享運行程序](../../user/gitlab_com/index.html#shared-runners)支持此方法. 1. Install [GitLab Runner](https://docs.gitlab.com/runner/install/). 2. 從命令行注冊 GitLab Runner 以使用`docker`和`privileged`模式: ``` sudo gitlab-runner register -n \ --url https://gitlab.com/ \ --registration-token REGISTRATION_TOKEN \ --executor docker \ --description "My Docker Runner" \ --docker-image "docker:19.03.12" \ --docker-privileged \ --docker-volumes "/certs/client" ``` 上面的命令將注冊一個新的 Runner 以使用由 Docker 提供的特殊`docker:19.03.12`映像. **注意,它使用`privileged`模式來啟動構建和服務容器.** 如果要使用[Docker-in-Docker](https://www.docker.com/blog/docker-can-now-run-within-docker/)模式,則始終必須在 Docker 容器中使用`privileged = true` . 這還將為服務安裝`/certs/client`并構建容器,這是 Docker 客戶端使用該目錄內的證書所必需的. 有關更多信息,請參閱[https://hub.docker.com/_/docker/#tls](https://hub.docker.com/_/docker/#tls) . 上面的命令將創建一個類似于以下內容的`config.toml`條目: ``` [[runners]] url = "https://gitlab.com/" token = TOKEN executor = "docker" [runners.docker] tls_verify = false image = "docker:19.03.12" privileged = true disable_cache = false volumes = ["/certs/client", "/cache"] [runners.cache] [runners.cache.s3] [runners.cache.gcs] ``` 3. 您現在可以使用`docker`在構建腳本(注意列入`docker:19.03.12-dind`服務): ``` image: docker:19.03.12 variables: # When using dind service, we need to instruct docker, to talk with # the daemon started inside of the service. The daemon is available # with a network connection instead of the default # /var/run/docker.sock socket. Docker 19.03 does this automatically # by setting the DOCKER_HOST in # https://github.com/docker-library/docker/blob/d45051476babc297257df490d22cbd806f1b11e4/19.03/docker-entrypoint.sh#L23-L29 # # The 'docker' hostname is the alias of the service container as described at # https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#accessing-the-services. # # Note that if you're using GitLab Runner 12.7 or earlier with the Kubernetes executor and Kubernetes 1.6 or earlier, # the variable must be set to tcp://localhost:2376 because of how the # Kubernetes executor connects services to the job container # DOCKER_HOST: tcp://localhost:2376 # # Specify to Docker where to create the certificates, Docker will # create them automatically on boot, and will create # `/certs/client` that will be shared between the service and job # container, thanks to volume mount from config.toml DOCKER_TLS_CERTDIR: "/certs" services: - docker:19.03.12-dind before_script: - docker info build: stage: build script: - docker build -t my-docker-image . - docker run my-docker-image /script/to/run/tests ``` #### TLS disabled[](#tls-disabled "Permalink") 有時出于某些合理原因,您可能想要禁用 TLS. 例如,您無法控制所使用的 GitLab Runner 配置. 假設 Runner `config.toml`類似于: ``` [[runners]] url = "https://gitlab.com/" token = TOKEN executor = "docker" [runners.docker] tls_verify = false image = "docker:19.03.12" privileged = true disable_cache = false volumes = ["/cache"] [runners.cache] [runners.cache.s3] [runners.cache.gcs] ``` 您現在可以使用`docker`在構建腳本(注意列入`docker:19.03.12-dind`服務): ``` image: docker:19.03.12 variables: # When using dind service we need to instruct docker, to talk with the # daemon started inside of the service. The daemon is available with # a network connection instead of the default /var/run/docker.sock socket. # # The 'docker' hostname is the alias of the service container as described at # https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#accessing-the-services # # Note that if you're using GitLab Runner 12.7 or earlier with the Kubernetes executor and Kubernetes 1.6 or earlier, # the variable must be set to tcp://localhost:2375 because of how the # Kubernetes executor connects services to the job container # DOCKER_HOST: tcp://localhost:2375 # DOCKER_HOST: tcp://docker:2375 # # This will instruct Docker not to start over TLS. DOCKER_TLS_CERTDIR: "" services: - docker:19.03.12-dind before_script: - docker info build: stage: build script: - docker build -t my-docker-image . - docker run my-docker-image /script/to/run/tests ``` ### Use Docker socket binding[](#use-docker-socket-binding "Permalink") 第三種方法是將`/var/run/docker.sock`綁定安裝到容器中,以便 Docker 在該映像的上下文中可用. **注意:**如果[在使用 GitLab Runner 11.11 或更高版本時](https://gitlab.com/gitlab-org/gitlab-runner/-/merge_requests/1261)綁定 Docker 套接字,則不能再將`docker:19.03.12-dind`用作服務,因為對服務也進行了卷綁定,從而使它們不兼容. 為此,請按照下列步驟操作: 1. Install [GitLab Runner](https://docs.gitlab.com/runner/install/). 2. 從命令行注冊 GitLab Runner 以使用`docker`并共享`/var/run/docker.sock` : ``` sudo gitlab-runner register -n \ --url https://gitlab.com/ \ --registration-token REGISTRATION_TOKEN \ --executor docker \ --description "My Docker Runner" \ --docker-image "docker:19.03.12" \ --docker-volumes /var/run/docker.sock:/var/run/docker.sock ``` 上面的命令將注冊一個新的 Runner 以使用由 Docker 提供的特殊`docker:19.03.12`映像. **請注意,它使用的是 Runner 本身的 Docker 守護進程,并且 Docker 命令產生的任何容器都將是 Runner 的兄弟,而不是 Runner 的子代.** 這可能會帶來一些不適合您的工作流程的復雜性和局限性. 上面的命令將創建一個類似于以下內容的`config.toml`條目: ``` [[runners]] url = "https://gitlab.com/" token = REGISTRATION_TOKEN executor = "docker" [runners.docker] tls_verify = false image = "docker:19.03.12" privileged = false disable_cache = false volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"] [runners.cache] Insecure = false ``` 3. 您現在可以使用`docker`在構建腳本(請注意,您不需要包括`docker:19.03.12-dind`服務泊塢執行使用泊塢時): ``` image: docker:19.03.12 before_script: - docker info build: stage: build script: - docker build -t my-docker-image . - docker run my-docker-image /script/to/run/tests ``` 盡管上述方法避免在特權模式下使用 Docker,但您應注意以下含義: * 通過共享 Docker 守護程序,您可以有效地禁用容器的所有安全機制,并使主機暴露于特權升級之下,這可能導致容器突破. 例如,如果一個項目運行`docker rm -f $(docker ps -a -q)` ,它將刪除 GitLab Runner 容器. * 并發工作可能無法正常工作; 如果您的測試創建了具有特定名稱的容器,則它們可能會相互沖突. * 將源倉庫中的文件和目錄共享到容器中可能無法正常工作,因為卷安裝是在主機而不是構建容器的上下文中完成的. 例如: ``` docker run --rm -t -i -v $(pwd)/src:/home/app/src test-image:latest run_app_tests ``` ## Making Docker-in-Docker builds faster with Docker layer caching[](#making-docker-in-docker-builds-faster-with-docker-layer-caching "Permalink") 在使用 Docker-in-Docker 時,每次創建構建時 Docker 都會下載映像的所有層. 最新版本的 Docker(Docker 1.13 及更高版本)可以在 Docker `docker build`步驟中使用預先存在的映像作為緩存,從而大大加快了構建過程. ### How Docker caching works[](#how-docker-caching-works "Permalink") 運行`Dockerfile` `docker build` , `Dockerfile`中的每個命令`Dockerfile`一個圖層. 這些層保留為高速緩存,如果沒有任何更改,可以重復使用. 一層中的更改將導致重新創建所有后續層. 您可以使用`--cache-from`參數指定標記的圖像用作`--cache-from` `docker build`命令的緩存源. 可以使用多個`--cache-from`參數將多個圖像指定為緩存源. 請記住,與`--cache-from`參數一起使用的任何映像都必須先被拉出(使用`docker pull` ),然后才能用作緩存源. ### Using Docker caching[](#using-docker-caching "Permalink") 這是一個`.gitlab-ci.yml`文件,顯示了如何使用 Docker 緩存: ``` image: docker:19.03.12 services: - docker:19.03.12-dind variables: # Use TLS https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#tls-enabled DOCKER_HOST: tcp://docker:2376 DOCKER_TLS_CERTDIR: "/certs" before_script: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY build: stage: build script: - docker pull $CI_REGISTRY_IMAGE:latest || true - docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --tag $CI_REGISTRY_IMAGE:latest . - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA - docker push $CI_REGISTRY_IMAGE:latest ``` `build`階段的`script`部分中的步驟可以總結為: 1. 第一個命令嘗試從注冊表中提取映像,以便將其用作`docker build`命令的緩存. 2. 第二個命令使用拉取的映像作為緩存來構建 Docker 映像(請注意`--cache-from $CI_REGISTRY_IMAGE:latest`參數),并對其進行標記. 3. 最后兩個命令將標記的 Docker 映像推送到容器注冊表,以便它們也可用作后續構建的緩存. ## Use the OverlayFS driver[](#use-the-overlayfs-driver "Permalink") **注意:**默認情況下,GitLab.com 上的共享 Runners 使用`overlay2`驅動程序. 默認情況下,使用`docker:dind` ,Docker 使用`vfs`存儲驅動程序,該驅動程序會在每次運行時復制文件系統. 這是磁盤密集型操作,如果使用其他驅動程序(例如`overlay2` ,則可以避免. ### Requirements[](#requirements "Permalink") 1. 確保使用最新內核,最好`>= 4.2` . 2. 檢查是否已加載`overlay`模塊: ``` sudo lsmod | grep overlay ``` 如果看不到任何結果,則說明未加載. 要加載它,請使用: ``` sudo modprobe overlay ``` 如果一切正常,則需要確保模塊在重新啟動時已加載. 在 Ubuntu 系統上,這是通過編輯`/etc/modules` . 只需將以下行添加到其中: ``` overlay ``` ### Use the OverlayFS driver per project[](#use-the-overlayfs-driver-per-project "Permalink") 您可以使用`.gitlab-ci.yml`的`DOCKER_DRIVER`環境[變量](../yaml/README.html#variables)分別為每個項目啟用驅動程序: ``` variables: DOCKER_DRIVER: overlay2 ``` ### Use the OverlayFS driver for every project[](#use-the-overlayfs-driver-for-every-project "Permalink") 如果使用自己的[GitLab Runners](https://docs.gitlab.com/runner/) ,則可以通過在[`config.toml`](https://docs.gitlab.com/runner/configuration/advanced-configuration.html)的[`[[runners]]`部分中](https://docs.gitlab.com/runner/configuration/advanced-configuration.html)設置`DOCKER_DRIVER`環境變量來為每個項目啟用驅動程序: ``` environment = ["DOCKER_DRIVER=overlay2"] ``` 如果您正在運行多個運行程序,則必須修改所有配置文件. **注意:**閱讀有關[Runner 配置](https://docs.gitlab.com/runner/configuration/)和[使用 OverlayFS 存儲驅動程序的更多信息](https://s0docs0docker0com.icopy.site/engine/userguide/storagedriver/overlayfs-driver/) . ## Using the GitLab Container Registry[](#using-the-gitlab-container-registry "Permalink") 構建 Docker 映像后,可以將其推送到內置的[GitLab Container Registry 中](../../user/packages/container_registry/index.html#build-and-push-images-using-gitlab-cicd) . ## Troubleshooting[](#troubleshooting "Permalink") ### `docker: Cannot connect to the Docker daemon at tcp://docker:2375\. Is the docker daemon running?`[](#docker-cannot-connect-to-the-docker-daemon-at-tcpdocker2375-is-the-docker-daemon-running "Permalink") [在 Docker](#use-docker-in-docker-workflow-with-docker-executor) v19.03 或更高版本中使用[Docker](#use-docker-in-docker-workflow-with-docker-executor)時,這是一個常見錯誤. 發生這種情況是因為 Docker 自動在 TLS 上啟動,因此您需要進行一些設置. 如果: * 這是第一次設置,請[在 Docker 工作流程中使用 Docker](#use-docker-in-docker-workflow-with-docker-executor)仔細閱讀. * 您要從 v18.09 或更早版本[升級](https://about.gitlab.com/releases/2019/07/31/docker-in-docker-with-docker-19-dot-03/) ,請閱讀我們的[升級指南](https://about.gitlab.com/releases/2019/07/31/docker-in-docker-with-docker-19-dot-03/) .
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看