docker in docker
的方式,即在具備特權模式的 Docker 中使用 Docker,經過 gitlab-runner 使用 docker 的來構建項目,完成打包,測試,發佈等任務。這樣 gitlab-runner 只是須要 Dockerfile 便可,可方便往後使用其餘 CI/CD 工具。同時本地調試也十分方便docker run -d --name gitlab-runner --restart always \ -v /srv/gitlab-runner/config:/etc/gitlab-runner \ -v /var/run/docker.sock:/var/run/docker.sock \ gitlab/gitlab-runner:latest
docker exec -it gitlab-runner bash
gitlab-runner register
進行註冊,按照提示和文檔說明一步步填寫便可sudo gitlab-runner register \ --non-interactive \ --url "https://gitlab.com/" \ --registration-token "PROJECT_REGISTRATION_TOKEN" \ --executor "docker" \ --docker-image alpine:latest \ --description "docker-runner" \ --tag-list "docker,aws" \ --run-untagged="true" \ --locked="false" \ --access-level="not_protected"
/etc/gitlab-runner/config.toml
中添加 privileged 標記,這樣能夠在容器中使用 docker 的一些功能[[runners]] executor = "docker" [runners.docker] privileged = true
[[runners]] name = "docker-runner" url = "http://192.168.99.100:8900/" token = "zLBu8yXEFPGKaaasZopn" executor = "docker" clone_url = "http://192.168.99.100:8900/" [runners.custom_build_dir] [runners.docker] tls_verify = false image = "apline:latest" privileged = true disable_entrypoint_overwrite = false oom_kill_disable = false disable_cache = false volumes = ["/cache"] shm_size = 0
在.gitlab-ci.yml
文件中使用方式html
Shell | Usage |
---|---|
bash/sh | $variable |
windows batch | %variable% |
PowerShell | $env:variable |
Dockerfile 中使用變量參考:https://docs.docker.com/engine/reference/builder/#arggit
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch ARG AppKey ARG Source WORKDIR /src COPY ["A1/A1.csproj", "A1/"] RUN dotnet restore "A1/A1.csproj" COPY . . WORKDIR /src/A1 RUN dotnet pack "A1" -c Release -o /app WORKDIR /app RUN dotnet nuget push *.nupkg -k ${AppKey} -s ${Source}
# This file is a template, and might need editing before it works on your project. build-master: # Official docker image. image: docker:latest stage: build services: - docker:dind before_script: # - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY - docker --version - echo $NugetAppKey - echo $NugetSource script: - docker build --build-arg AppKey=$NugetAppKey --build-arg Source=$NugetSource -t A1:latest . only: - master
time="2019-07-19T01:20:27Z" level=error msg="failed to dial gRPC: cannot connect to the Docker daemon. Is 'docker daemon' running on this host?: dial tcp: lookup docker on 100.100.2.136:53: no such host"
,這個須要配置 runner,讓容器跑在privileged
模式,參考 https://docs.gitlab.com/runner/executors/docker.html#use-docker-in-docker-with-privileged-modeclone_url
。文檔:https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runners-section 示例:[[runners]] name = "ruby-2.1-docker" url = "https://CI/" token = "TOKEN" limit = 0 executor = "docker" builds_dir = "" shell = "" environment = ["ENV=value", "LC_ALL=en_US.UTF-8"] clone_url = "http://gitlab.example.local"