PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/doc/docker/using_docker_images.md

https://gitlab.com/nagoyamavps/gitlab-ci
Markdown | 203 lines | 168 code | 35 blank | 0 comment | 0 complexity | 6165b93c5567289d4e68b14c0b9260df MD5 | raw file
  1. # Using Docker Images
  2. GitLab CI can use [Docker Engine](https://www.docker.com/) to build projects.
  3. Docker is an open-source project that allows to use predefined images to run applications
  4. in independent "containers" that are run within a single Linux instance.
  5. [Docker Hub](https://registry.hub.docker.com/) have rich database of built images that can be used to build applications.
  6. Docker when used with GitLab CI runs each build in separate and isolated container using predefined image and always from scratch.
  7. It makes it easier to have simple and reproducible build environment that can also be run on your workstation.
  8. This allows you to test all commands from your shell, rather than having to test them on a CI server.
  9. ### Register Docker runner
  10. To use GitLab Runner with Docker you need to register new runner to use `docker` executor:
  11. ```bash
  12. gitlab-ci-multi-runner register \
  13. --url "https://ci.gitlab.com/" \
  14. --registration-token "PROJECT_REGISTRATION_TOKEN" \
  15. --description "docker-ruby-2.1" \
  16. --executor "docker" \
  17. --docker-image ruby:2.1 \
  18. --docker-postgres latest \
  19. --docker-mysql latest
  20. ```
  21. **The registered runner will use `ruby:2.1` image and will run two services (`postgres:latest` and `mysql:latest`) that will be accessible for time of the build.**
  22. ### What is image?
  23. The image is the name of any repository that is present in local Docker Engine or any repository that can be found at [Docker Hub](https://registry.hub.docker.com/).
  24. For more information about the image and Docker Hub please read the [Docker Fundamentals](https://docs.docker.com/introduction/understanding-docker/).
  25. ### What is service?
  26. Service is just another image that is run for time of your build and is linked to your build. This allows you to access the service image during build time.
  27. The service image can run any application, but most common use case is to run some database container, ie.: `mysql`.
  28. It's easier and faster to use existing image, run it as additional container than install `mysql` every time project is built.
  29. #### How is service linked to the build?
  30. There's good document that describes how Docker linking works: [Linking containers together](https://docs.docker.com/userguide/dockerlinks/).
  31. To summarize: if you add `mysql` as service to your application, the image will be used to create container that is linked to build container.
  32. The service container for MySQL will be accessible under hostname `mysql`.
  33. So, **to access your database service you have to connect to host: `mysql` instead of socket or `localhost`**.
  34. ### How to use other images as services?
  35. You are not limited to have only database services.
  36. You can hand modify `config.toml` to add any image as service found at [Docker Hub](https://registry.hub.docker.com/).
  37. Look for `[runners.docker]` section:
  38. ```
  39. [runners.docker]
  40. image = "ruby:2.1"
  41. services = ["mysql:latest", "postgres:latest"]
  42. ```
  43. For example you need `wordpress` instance to test some API integration with `Wordpress`.
  44. You can for example use this image: [tutum/wordpress](https://registry.hub.docker.com/u/tutum/wordpress/).
  45. This is image that have fully preconfigured `wordpress` and have `MySQL` server built-in:
  46. ```
  47. [runners.docker]
  48. image = "ruby:2.1"
  49. services = ["mysql:latest", "postgres:latest", "tutum/wordpress:latest"]
  50. ```
  51. Next time when you run your application the `tutum/wordpress` will be started
  52. and you will have access to it from your build container under hostname: `tutum_wordpress`.
  53. Alias hostname for the service is made from the image name:
  54. 1. Everything after `:` is stripped,
  55. 2. '/' is replaced to `_`.
  56. ### Configuring services
  57. Many services accept environment variables, which allow you to easily change database names or set account names depending on the environment.
  58. GitLab Runner 0.5.0 and up passes all YAML-defined variables to created service containers.
  59. 1. To configure database name for [postgres](https://registry.hub.docker.com/u/library/postgres/) service,
  60. you need to set POSTGRES_DB.
  61. ```yaml
  62. services:
  63. - postgres
  64. variables:
  65. POSTGRES_DB: gitlab
  66. ```
  67. 1. To use [mysql](https://registry.hub.docker.com/u/library/mysql/) service with empty password for time of build,
  68. you need to set MYSQL_ALLOW_EMPTY_PASSWORD.
  69. ```yaml
  70. services:
  71. - mysql
  72. variables:
  73. MYSQL_ALLOW_EMPTY_PASSWORD: yes
  74. ```
  75. For other possible configuration variables check the
  76. https://registry.hub.docker.com/u/library/mysql/ or https://registry.hub.docker.com/u/library/postgres/
  77. or README page for any other Docker image.
  78. **Note: All variables will passed to all service containers. It's not designed to distinguish which variable should go where.**
  79. ### Overwrite image and services
  80. It's possible to overwrite `docker-image` and specify services from `.gitlab-ci.yml`.
  81. If you add to your YAML the `image` and the `services` these parameters
  82. be used instead of the ones that were specified during runner's registration.
  83. ```
  84. image: ruby:2.2
  85. services:
  86. - postgres:9.3
  87. before_install:
  88. - bundle install
  89. test:
  90. script:
  91. - bundle exec rake spec
  92. ```
  93. It's possible to define image and service per-job:
  94. ```
  95. before_install:
  96. - bundle install
  97. test:2.1:
  98. image: ruby:2.1
  99. services:
  100. - postgres:9.3
  101. script:
  102. - bundle exec rake spec
  103. test:2.2:
  104. image: ruby:2.2
  105. services:
  106. - postgres:9.4
  107. script:
  108. - bundle exec rake spec
  109. ```
  110. #### How to enable overwriting?
  111. To enable overwriting you have to **enable it first** (it's disabled by default for security reasons).
  112. You can do that by hand modifying runner configuration: `config.toml`.
  113. Please go to section where is `[runners.docker]` definition for your runner.
  114. Add `allowed_images` and `allowed_services` to specify what images are allowed to be picked from `.gitlab-ci.yml`:
  115. ```
  116. [runners.docker]
  117. image = "ruby:2.1"
  118. allowed_images = ["ruby:*", "python:*"]
  119. allowed_services = ["mysql:*", "redis:*"]
  120. ```
  121. This enables you to use in your `.gitlab-ci.yml` any image that matches above wildcards.
  122. You will be able to pick only `ruby` and `python` images.
  123. The same rule can be applied to limit services.
  124. If you are courageous enough, you can make it fully open and accept everything:
  125. ```
  126. [runners.docker]
  127. image = "ruby:2.1"
  128. allowed_images = ["*", "*/*", "*/*/*"]
  129. allowed_services = ["*", "*/*"]
  130. ```
  131. **It the feature is not enabled, or image isn't allowed the error message will be put into the build log.**
  132. ### How Docker integration works
  133. 1. Create any service container: `mysql`, `postgresql`, `mongodb`, `redis`.
  134. 1. Create cache container to store all volumes as defined in `config.toml` and `Dockerfile` of build image (`ruby:2.1` as in above example).
  135. 1. Create build container and link any service container to build container.
  136. 1. Start build container and send build script to the container.
  137. 1. Run build script.
  138. 1. Checkout code in: `/builds/group-name/project-name/`.
  139. 1. Run any step defined in `.gitlab-ci.yml`.
  140. 1. Check exit status of build script.
  141. 1. Remove build container and all created service containers.
  142. ### How to debug a build locally
  143. 1. Create a file with build script:
  144. ```bash
  145. $ cat <<EOF > build_script
  146. git clone https://gitlab.com/gitlab-org/gitlab-ci-multi-runner.git /builds/gitlab-org/gitlab-ci-multi-runner
  147. cd /builds/gitlab-org/gitlab-ci-multi-runner
  148. make <- or any other build step
  149. EOF
  150. ```
  151. 1. Create service containers:
  152. ```
  153. $ docker run -d -n service-mysql mysql:latest
  154. $ docker run -d -n service-postgres postgres:latest
  155. ```
  156. This will create two service containers (MySQL and PostgreSQL).
  157. 1. Create a build container and execute script in its context:
  158. ```
  159. $ cat build_script | docker run -n build -i -l mysql:service-mysql -l postgres:service-postgres ruby:2.1 /bin/bash
  160. ```
  161. This will create build container that has two service containers linked.
  162. The build_script is piped using STDIN to bash interpreter which executes the build script in container.
  163. 1. At the end remove all containers:
  164. ```
  165. docker rm -f -v build service-mysql service-postgres
  166. ```
  167. This will forcefully (the `-f` switch) remove build container and service containers
  168. and all volumes (the `-v` switch) that were created with the container creation.