gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/docs/executors/docker.md (about) 1 # The Docker executor 2 3 GitLab Runner can use Docker to run jobs on user provided images. This is 4 possible with the use of **Docker** executor. 5 6 The **Docker** executor when used with GitLab CI, connects to [Docker Engine] 7 and runs each build in a separate and isolated container using the predefined 8 image that is [set up in `.gitlab-ci.yml`][yaml] and in accordance in 9 [`config.toml`][toml]. 10 11 That way you can have a simple and reproducible build environment that can also 12 run on your workstation. The added benefit is that you can test all the 13 commands that we will explore later from your shell, rather than having to test 14 them on a dedicated CI server. 15 16 NOTE: **Note:** 17 GitLab Runner uses Docker Engine API 18 [v1.25](https://docs.docker.com/engine/api/v1.25/) to talk to the Docker 19 Engine. Refer to the [API version 20 matrix](https://docs.docker.com/develop/sdk/#api-version-matrix) for 21 compatible versions of Docker. 22 23 ## Using Windows containers 24 25 > [Introduced](https://gitlab.com/groups/gitlab-org/-/epics/535) in 11.11. 26 27 To use Windows containers with the Docker executor, note the following 28 information about limitations, supported Windows versions, and 29 configuring a Windows Docker executor. 30 31 ### Limitations 32 33 The following are some limitations of using Windows containers with 34 Docker executor: 35 36 - Services do not fully work (see 37 [#4186](https://gitlab.com/gitlab-org/gitlab-runner/issues/4186)). 38 - Nanoserver cannot be used because it requires PowerShell 6 but GitLab 39 requires PowerShell 5 (see 40 [#3291](https://gitlab.com/gitlab-org/gitlab-runner/issues/3291)). See 41 also the list of [supported Windows 42 versions](#supported-windows-versions). 43 - Docker-in-Docker is not supported, since it's [not 44 supported](https://github.com/docker-library/docker/issues/49) by 45 Docker itself. 46 - Interactive web terminals are not supported. 47 - Host device mounting not supported. 48 - When mounting a volume directory it has to exist, or Docker will fail 49 to start the container, see 50 [#3754](https://gitlab.com/gitlab-org/gitlab-runner/issues/3754) for 51 additional detail. 52 - `docker-windows` executor can be run only using GitLab Runner running 53 on Windows. 54 55 ### Supported Windows versions 56 57 GitLab Runner only supports the following versions of Windows: 58 59 - Windows Server 1809. 60 - Windows Server 1803. 61 62 You can only run containers based on the same OS version that the Docker 63 daemon is running on. For example, the following [`Windows Server 64 Core`](https://hub.docker.com/_/microsoft-windows-servercore) images can 65 be used: 66 67 - mcr.microsoft.com/windows/servercore:1809 68 - mcr.microsoft.com/windows/servercore:1809-amd64 69 - mcr.microsoft.com/windows/servercore:ltsc2019 70 - mcr.microsoft.com/windows/servercore:1803 71 72 ### Configuring a Windows Docker executor 73 74 When you register a new Runner in interactive mode, 75 you can't choose `docker-windows` as an executor and must instead 76 edit the `config.toml` that is created/updated. 77 78 In addition, an incorrect Windows path for the 79 [`volumes`](../configuration/advanced-configuration.md#the-runnersdocker-section) 80 key is defined in `config.toml` file. User must manually edit this key 81 until [#3915](https://gitlab.com/gitlab-org/gitlab-runner/issues/3915) 82 is resolved. 83 84 Below is an example of what the configuration for a simple Docker 85 executor running Windows 86 87 ``` 88 [[runners]] 89 name = "windows-docker-2019" 90 url = "https://gitlab.com/" 91 token = "xxxxxxx" 92 executor = "docker-windows" 93 [runners.docker] 94 image = "mcr.microsoft.com/windows/servercore:1809_amd64" 95 volumes = ["c:\\cache"] 96 ``` 97 98 For other configuration options for the Docker executor, see the 99 [advanced 100 configuration](../configuration/advanced-configuration.md#the-runnersdocker-section) 101 section. 102 103 ## Workflow 104 105 The Docker executor divides the job into multiple steps: 106 107 1. **Prepare**: Create and start the [services](https://docs.gitlab.com/ee/ci/yaml/#services). 108 1. **Pre-job**: Clone, restore [cache](https://docs.gitlab.com/ee/ci/yaml/#cache) 109 and download [artifacts](https://docs.gitlab.com/ee/ci/yaml/#artifacts) from previous 110 stages. This is run on a special Docker image. 111 1. **Job**: User build. This is run on the user-provided Docker image. 112 1. **Post-job**: Create cache, upload artifacts to GitLab. This is run on 113 a special Docker Image. 114 115 The special Docker image is based on [Alpine Linux] and contains all the tools 116 required to run the prepare, pre-job, and post-job steps, like the Git and the 117 Runner binaries for supporting caching and artifacts. You can find the definition of 118 this special image [in the official Runner repository][special-build]. 119 120 ## The `image` keyword 121 122 The `image` keyword is the name of the Docker image that is present in the 123 local Docker Engine (list all images with `docker images`) or any image that 124 can be found at [Docker Hub][hub]. For more information about images and Docker 125 Hub please read the [Docker Fundamentals][] documentation. 126 127 In short, with `image` we refer to the docker image, which will be used to 128 create a container on which your build will run. 129 130 If you don't specify the namespace, Docker implies `library` which includes all 131 [official images](https://hub.docker.com/u/library/). That's why you'll see 132 many times the `library` part omitted in `.gitlab-ci.yml` and `config.toml`. 133 For example you can define an image like `image: ruby:2.1`, which is a shortcut 134 for `image: library/ruby:2.1`. 135 136 Then, for each Docker image there are tags, denoting the version of the image. 137 These are defined with a colon (`:`) after the image name. For example, for 138 Ruby you can see the supported tags at <https://hub.docker.com/_/ruby/>. If you 139 don't specify a tag (like `image: ruby`), `latest` is implied. 140 141 ## The `services` keyword 142 143 The `services` keyword defines just another Docker image that is run during 144 your build and is linked to the Docker image that the `image` keyword defines. 145 This allows you to access the service image during build time. 146 147 The service image can run any application, but the most common use case is to 148 run a database container, e.g., `mysql`. It's easier and faster to use an 149 existing image and run it as an additional container than install `mysql` every 150 time the project is built. 151 152 You can see some widely used services examples in the relevant documentation of 153 [CI services examples](https://gitlab.com/gitlab-org/gitlab-ce/tree/master/doc/ci/services/README.md). 154 155 ### How is service linked to the build 156 157 To better understand how the container linking works, read 158 [Linking containers together](https://docs.docker.com/userguide/dockerlinks/). 159 160 To summarize, if you add `mysql` as service to your application, this image 161 will then be used to create a container that is linked to the build container. 162 According to the [workflow](#workflow) this is the first step that is performed 163 before running the actual builds. 164 165 The service container for MySQL will be accessible under the hostname `mysql`. 166 So, in order to access your database service you have to connect to the host 167 named `mysql` instead of a socket or `localhost`. 168 169 ## Define image and services from `.gitlab-ci.yml` 170 171 You can simply define an image that will be used for all jobs and a list of 172 services that you want to use during build time. 173 174 ```yaml 175 image: ruby:2.2 176 177 services: 178 - postgres:9.3 179 180 before_script: 181 - bundle install 182 183 test: 184 script: 185 - bundle exec rake spec 186 ``` 187 188 It is also possible to define different images and services per job: 189 190 ```yaml 191 before_script: 192 - bundle install 193 194 test:2.1: 195 image: ruby:2.1 196 services: 197 - postgres:9.3 198 script: 199 - bundle exec rake spec 200 201 test:2.2: 202 image: ruby:2.2 203 services: 204 - postgres:9.4 205 script: 206 - bundle exec rake spec 207 ``` 208 209 ## Define image and services in `config.toml` 210 211 Look for the `[runners.docker]` section: 212 213 ``` 214 [runners.docker] 215 image = "ruby:2.1" 216 services = ["mysql:latest", "postgres:latest"] 217 ``` 218 219 The image and services defined this way will be added to all builds run by 220 that Runner, so even if you don't define an `image` inside `.gitlab-ci.yml`, 221 the one defined in `config.toml` will be used. 222 223 ## Define an image from a private Docker registry 224 225 Starting with GitLab Runner 0.6.0, you are able to define images located to 226 private registries that could also require authentication. 227 228 All you have to do is be explicit on the image definition in `.gitlab-ci.yml`. 229 230 ```yaml 231 image: my.registry.tld:5000/namepace/image:tag 232 ``` 233 234 In the example above, GitLab Runner will look at `my.registry.tld:5000` for the 235 image `namespace/image:tag`. 236 237 If the repository is private you need to authenticate your GitLab Runner in the 238 registry. Read more on [using a private Docker registry][runner-priv-reg]. 239 240 ## Accessing the services 241 242 Let's say that you need a Wordpress instance to test some API integration with 243 your application. 244 245 You can then use for example the [tutum/wordpress][] as a service image in your 246 `.gitlab-ci.yml`: 247 248 ```yaml 249 services: 250 - tutum/wordpress:latest 251 ``` 252 253 When the build is run, `tutum/wordpress` will be started first and you will have 254 access to it from your build container under the hostname `tutum__wordpress` 255 and `tutum-wordpress`. 256 257 The GitLab Runner creates two alias hostnames for the service that you can use 258 alternatively. The aliases are taken from the image name following these rules: 259 260 1. Everything after `:` is stripped 261 2. For the first alias, the slash (`/`) is replaced with double underscores (`__`) 262 2. For the second alias, the slash (`/`) is replaced with a single dash (`-`) 263 264 Using a private service image will strip any port given and apply the rules as 265 described above. A service `registry.gitlab-wp.com:4999/tutum/wordpress` will 266 result in hostname `registry.gitlab-wp.com__tutum__wordpress` and 267 `registry.gitlab-wp.com-tutum-wordpress`. 268 269 ## Configuring services 270 271 Many services accept environment variables which allow you to easily change 272 database names or set account names depending on the environment. 273 274 GitLab Runner 0.5.0 and up passes all YAML-defined variables to the created 275 service containers. 276 277 For all possible configuration variables check the documentation of each image 278 provided in their corresponding Docker hub page. 279 280 > **Note**: 281 > All variables will be passed to all services containers. It's not designed to 282 distinguish which variable should go where. 283 Secure variables are only passed to the build container. 284 285 ## Mounting a directory in RAM 286 287 You can mount a path in RAM using tmpfs. This can speed up the time required to test if there is a lot of I/O related work, such as with databases. 288 If you use the `tmpfs` and `services_tmpfs` options in the runner configuration, you can specify multiple paths, each with its own options. See the [docker reference](https://docs.docker.com/engine/reference/commandline/run/#mount-tmpfs-tmpfs) for details. 289 This is an example `config.toml` to mount the data directory for the official Mysql container in RAM. 290 291 ``` 292 [runners.docker] 293 # For the main container 294 [runners.docker.tmpfs] 295 "/var/lib/mysql" = "rw,noexec" 296 297 # For services 298 [runners.docker.services_tmpfs] 299 "/var/lib/mysql" = "rw,noexec" 300 ``` 301 302 ## Build directory in service 303 304 Since version 1.5 GitLab Runner mounts a `/builds` directory to all shared services. 305 306 See an issue: https://gitlab.com/gitlab-org/gitlab-runner/issues/1520 307 308 ### PostgreSQL service example 309 310 See the specific documentation for 311 [using PostgreSQL as a service](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/ci/services/postgres.md). 312 313 ### MySQL service example 314 315 See the specific documentation for 316 [using MySQL as a service](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/ci/services/mysql.md). 317 318 ### The services health check 319 320 After the service is started, GitLab Runner waits some time for the service to 321 be responsive. Currently, the Docker executor tries to open a TCP connection to 322 the first exposed service in the service container. 323 324 You can see how it is implemented by checking [this script][service-file]. 325 326 ## The builds and cache storage 327 328 The Docker executor by default stores all builds in 329 `/builds/<namespace>/<project-name>` and all caches in `/cache` (inside the 330 container). 331 332 You can overwrite the `/builds` and `/cache` directories by defining the 333 `builds_dir` and `cache_dir` options under the `[[runners]]` section in 334 `config.toml`. This will modify where the data are stored inside the container. 335 336 If you modify the `/cache` storage path, you also need to make sure to mark this 337 directory as persistent by defining it in `volumes = ["/my/cache/"]` under the 338 `[runners.docker]` section in `config.toml`. 339 340 Read the next section of persistent storage for more information. 341 342 ## The persistent storage 343 344 The Docker executor can provide a persistent storage when running the containers. 345 All directories defined under `volumes =` will be persistent between builds. 346 347 The `volumes` directive supports 2 types of storage: 348 349 1. `<path>` - **the dynamic storage**. The `<path>` is persistent between subsequent 350 runs of the same concurrent job for that project. The data is attached to a 351 custom cache container: `runner-<short-token>-project-<id>-concurrent-<job-id>-cache-<unique-id>`. 352 2. `<host-path>:<path>[:<mode>]` - **the host-bound storage**. The `<path>` is 353 bind to `<host-path>` on the host system. The optional `<mode>` can specify 354 that this storage is read-only or read-write (default). 355 356 ## The persistent storage for builds 357 358 If you make the `/builds` to be **the host-bound storage**, your builds will be stored in: 359 `/builds/<short-token>/<concurrent-id>/<namespace>/<project-name>`, where: 360 361 - `<short-token>` is a shortened version of the Runner's token (first 8 letters) 362 - `<concurrent-id>` is a unique number, identifying the local job ID on the 363 particular Runner in context of the project 364 365 ## The privileged mode 366 367 The Docker executor supports a number of options that allows to fine tune the 368 build container. One of these options is the [`privileged` mode][privileged]. 369 370 ### Use docker-in-docker with privileged mode 371 372 The configured `privileged` flag is passed to the build container and all 373 services, thus allowing to easily use the docker-in-docker approach. 374 375 First, configure your Runner (config.toml) to run in `privileged` mode: 376 377 ```toml 378 [[runners]] 379 executor = "docker" 380 [runners.docker] 381 privileged = true 382 ``` 383 384 Then, make your build script (`.gitlab-ci.yml`) to use Docker-in-Docker 385 container: 386 387 ```bash 388 image: docker:git 389 services: 390 - docker:dind 391 392 build: 393 script: 394 - docker build -t my-image . 395 - docker push my-image 396 ``` 397 398 ## The ENTRYPOINT 399 400 The Docker executor doesn't overwrite the [`ENTRYPOINT` of a Docker image][entry]. 401 402 That means that if your image defines the `ENTRYPOINT` and doesn't allow to run 403 scripts with `CMD`, the image will not work with the Docker executor. 404 405 With the use of `ENTRYPOINT` it is possible to create special Docker image that 406 would run the build script in a custom environment, or in secure mode. 407 408 You may think of creating a Docker image that uses an `ENTRYPOINT` that doesn't 409 execute the build script, but does execute a predefined set of commands, for 410 example to build the docker image from your directory. In that case, you can 411 run the build container in [privileged mode](#the-privileged-mode), and make 412 the build environment of the Runner secure. 413 414 Consider the following example: 415 416 1. Create a new Dockerfile: 417 418 ```bash 419 FROM docker:dind 420 ADD / /entrypoint.sh 421 ENTRYPOINT ["/bin/sh", "/entrypoint.sh"] 422 ``` 423 424 2. Create a bash script (`entrypoint.sh`) that will be used as the `ENTRYPOINT`: 425 426 ```bash 427 #!/bin/sh 428 429 dind docker daemon 430 --host=unix:///var/run/docker.sock \ 431 --host=tcp://0.0.0.0:2375 \ 432 --storage-driver=vf & 433 434 docker build -t "$BUILD_IMAGE" . 435 docker push "$BUILD_IMAGE" 436 ``` 437 438 3. Push the image to the Docker registry. 439 440 4. Run Docker executor in `privileged` mode. In `config.toml` define: 441 442 ```toml 443 [[runners]] 444 executor = "docker" 445 [runners.docker] 446 privileged = true 447 ``` 448 449 5. In your project use the following `.gitlab-ci.yml`: 450 451 ```yaml 452 variables: 453 BUILD_IMAGE: my.image 454 build: 455 image: my/docker-build:image 456 script: 457 - Dummy Script 458 ``` 459 460 This is just one of the examples. With this approach the possibilities are 461 limitless. 462 463 ## How pull policies work 464 465 When using the `docker` or `docker+machine` executors, you can set the 466 `pull_policy` parameter which defines how the Runner will work when pulling 467 Docker images (for both `image` and `services` keywords). 468 469 > **Note:** 470 If you don't set any value for the `pull_policy` parameter, then 471 Runner will use the `always` pull policy as the default value. 472 473 Now let's see how these policies work. 474 475 ### Using the `never` pull policy 476 477 The `never` pull policy disables images pulling completely. If you set the 478 `pull_policy` parameter of a Runner to `never`, then users will be able 479 to use only the images that have been manually pulled on the docker host 480 the Runner runs on. 481 482 If an image cannot be found locally, then the Runner will fail the build 483 with an error similar to: 484 485 ``` 486 Pulling docker image local_image:latest ... 487 ERROR: Build failed: Error: image local_image:latest not found 488 ``` 489 490 **When to use this pull policy?** 491 492 This pull policy should be used if you want or need to have a full 493 control on which images are used by the Runner's users. It is a good choice 494 for private Runners that are dedicated to a project where only specific images 495 can be used (not publicly available on any registries). 496 497 **When not to use this pull policy?** 498 499 This pull policy will not work properly with most of [auto-scaled](../configuration/autoscale.md) 500 Docker executor use cases. Because of how auto-scaling works, the `never` 501 pull policy may be usable only when using a pre-defined cloud instance 502 images for chosen cloud provider. The image needs to contain installed 503 Docker Engine and local copy of used images. 504 505 ### Using the `if-not-present` pull policy 506 507 When the `if-not-present` pull policy is used, the Runner will first check 508 if the image is present locally. If it is, then the local version of 509 image will be used. Otherwise, the Runner will try to pull the image. 510 511 **When to use this pull policy?** 512 513 This pull policy is a good choice if you want to use images pulled from 514 remote registries but you want to reduce time spent on analyzing image 515 layers difference, when using heavy and rarely updated images. 516 In that case, you will need once in a while to manually remove the image 517 from the local Docker Engine store to force the update of the image. 518 519 It is also the good choice if you need to use images that are built 520 and available only locally, but on the other hand, also need to allow to 521 pull images from remote registries. 522 523 **When not to use this pull policy?** 524 525 This pull policy should not be used if your builds use images that 526 are updated frequently and need to be used in most recent versions. 527 In such situation, the network load reduction created by this policy may 528 be less worthy than the necessity of the very frequent deletion of local 529 copies of images. 530 531 This pull policy should also not be used if your Runner can be used by 532 different users which should not have access to private images used 533 by each other. Especially do not use this pull policy for shared Runners. 534 535 To understand why the `if-not-present` pull policy creates security issues 536 when used with private images, read the 537 [security considerations documentation][secpull]. 538 539 ### Using the `always` pull policy 540 541 The `always` pull policy will ensure that the image is **always** pulled. 542 When `always` is used, the Runner will try to pull the image even if a local 543 copy is available. If the image is not found, then the build will 544 fail with an error similar to: 545 546 ``` 547 Pulling docker image registry.tld/my/image:latest ... 548 ERROR: Build failed: Error: image registry.tld/my/image:latest not found 549 ``` 550 551 > **Note:** 552 For versions prior to `v1.8`, when using the `always` pull policy, it could 553 fall back to local copy of an image and print a warning: 554 > 555 > ``` 556 > Pulling docker image registry.tld/my/image:latest ... 557 > WARNING: Cannot pull the latest version of image registry.tld/my/image:latest : Error: image registry.tld/my/image:latest not found 558 > WARNING: Locally found image will be used instead. 559 > ``` 560 > 561 That is changed in version `v1.8`. To understand why we changed this and 562 how incorrect usage of may be revealed please look into issue 563 [#1905](https://gitlab.com/gitlab-org/gitlab-runner/issues/1905). 564 565 **When to use this pull policy?** 566 567 This pull policy should be used if your Runner is publicly available 568 and configured as a shared Runner in your GitLab instance. It is the 569 only pull policy that can be considered as secure when the Runner will 570 be used with private images. 571 572 This is also a good choice if you want to force users to always use 573 the newest images. 574 575 Also, this will be the best solution for an [auto-scaled](../configuration/autoscale.md) 576 configuration of the Runner. 577 578 **When not to use this pull policy?** 579 580 This pull policy will definitely not work if you need to use locally 581 stored images. In this case, the Runner will skip the local copy of the image 582 and try to pull it from the remote registry. If the image was build locally 583 and doesn't exist in any public registry (and especially in the default 584 Docker registry), the build will fail with: 585 586 ``` 587 Pulling docker image local_image:latest ... 588 ERROR: Build failed: Error: image local_image:latest not found 589 ``` 590 591 ## Docker vs Docker-SSH (and Docker+Machine vs Docker-SSH+Machine) 592 593 > **Note**: 594 Starting with GitLab Runner 10.0, both docker-ssh and docker-ssh+machine executors 595 are **deprecated** and will be removed in one of the upcoming releases. 596 597 We provided a support for a special type of Docker executor, namely Docker-SSH 598 (and the autoscaled version: Docker-SSH+Machine). Docker-SSH uses the same logic 599 as the Docker executor, but instead of executing the script directly, it uses an 600 SSH client to connect to the build container. 601 602 Docker-ssh then connects to the SSH server that is running inside the container 603 using its internal IP. 604 605 This executor is no longer maintained and will be removed in the near future. 606 607 [Docker Fundamentals]: https://docs.docker.com/engine/understanding-docker/ 608 [docker engine]: https://www.docker.com/products/docker-engine 609 [hub]: https://hub.docker.com/ 610 [linking-containers]: https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/ 611 [tutum/wordpress]: https://registry.hub.docker.com/u/tutum/wordpress/ 612 [postgres-hub]: https://registry.hub.docker.com/u/library/postgres/ 613 [mysql-hub]: https://registry.hub.docker.com/u/library/mysql/ 614 [runner-priv-reg]: ../configuration/advanced-configuration.md#using-a-private-container-registry 615 [yaml]: http://doc.gitlab.com/ce/ci/yaml/README.html 616 [toml]: ../commands/README.md#configuration-file 617 [alpine linux]: https://alpinelinux.org/ 618 [special-build]: https://gitlab.com/gitlab-org/gitlab-runner/tree/master/dockerfiles/build 619 [service-file]: https://gitlab.com/gitlab-org/gitlab-runner/blob/master/dockerfiles/build/scripts/gitlab-runner-service 620 [privileged]: https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities 621 [entry]: https://docs.docker.com/engine/reference/run/#entrypoint-default-command-to-execute-at-runtime 622 [secpull]: ../security/index.md#usage-of-private-docker-images-with-if-not-present-pull-policy