using_docker_images.md 27,3 КБ
Newer Older
Evan Read's avatar
Evan Read включено в состав коммита
1
2
3
4
---
type: concepts, howto
---

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
5
# Using Docker images
Douwe Maan's avatar
Douwe Maan включено в состав коммита
6

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
7
GitLab CI in conjunction with [GitLab Runner](../runners/README.md) can use
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
8
[Docker Engine](https://www.docker.com/) to test and build any application.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
9

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
10
11
Docker is an open-source project that allows you to use predefined images to
run applications in independent "containers" that are run within a single Linux
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
12
instance. [Docker Hub][hub] has a rich database of pre-built images that can be
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
13
used to test and build your applications.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
14

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
15
Docker, when used with GitLab CI, runs each job in a separate and isolated
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
16
17
container using the predefined image that is set up in
[`.gitlab-ci.yml`](../yaml/README.md).
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
18
19
20
21

This makes it easier to have a simple and reproducible build environment that
can also run on your workstation. The added benefit is that you can test all
the commands that we will explore later from your shell, rather than having to
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
22
test them on a dedicated CI server.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
23

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
24
## Register Docker Runner
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
25

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
26
27
28
29
To use GitLab Runner with Docker you need to [register a new Runner][register]
to use the `docker` executor.

A one-line example can be seen below:
Douwe Maan's avatar
Douwe Maan включено в состав коммита
30
31

```bash
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
32
33
sudo gitlab-runner register \
  --url "https://gitlab.example.com/" \
Douwe Maan's avatar
Douwe Maan включено в состав коммита
34
35
36
37
38
39
40
41
  --registration-token "PROJECT_REGISTRATION_TOKEN" \
  --description "docker-ruby-2.1" \
  --executor "docker" \
  --docker-image ruby:2.1 \
  --docker-postgres latest \
  --docker-mysql latest
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
42
The registered runner will use the `ruby:2.1` Docker image and will run two
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
43
44
45
services, `postgres:latest` and `mysql:latest`, both of which will be
accessible during the build process.

Wilfred Hughes's avatar
Wilfred Hughes включено в состав коммита
46
## What is an image
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
47

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
48
49
The `image` keyword is the name of the Docker image the Docker executor
will run to perform the CI tasks.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
50

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
51
By default, the executor will only pull images from [Docker Hub][hub],
Evan Read's avatar
Evan Read включено в состав коммита
52
however this can be configured in the `gitlab-runner/config.toml` by setting
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
53
the [Docker pull policy][] to allow using local images.
Adam Boseley's avatar
Adam Boseley включено в состав коммита
54

Evan Read's avatar
Evan Read включено в состав коммита
55
For more information about images and Docker Hub, please read
Adam Boseley's avatar
Adam Boseley включено в состав коммита
56
the [Docker Fundamentals][] documentation.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
57

Wilfred Hughes's avatar
Wilfred Hughes включено в состав коммита
58
## What is a service
Douwe Maan's avatar
Douwe Maan включено в состав коммита
59

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
60
61
The `services` keyword defines just another Docker image that is run during
your job and is linked to the Docker image that the `image` keyword defines.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
62
This allows you to access the service image during build time.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
63

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
64
The service image can run any application, but the most common use case is to
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
65
run a database container, e.g., `mysql`. It's easier and faster to use an
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
66
67
existing image and run it as an additional container than install `mysql` every
time the project is built.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
68

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
69
70
71
72
73
You are not limited to have only database services. You can add as many
services you need to `.gitlab-ci.yml` or manually modify `config.toml`.
Any image found at [Docker Hub][hub] or your private Container Registry can be
used as a service.

Florian Klink's avatar
Florian Klink включено в состав коммита
74
75
76
Services inherit the same DNS servers, search domains, and additional hosts as
the CI container itself.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
77
78
You can see some widely used services examples in the relevant documentation of
[CI services examples](../services/README.md).
Douwe Maan's avatar
Douwe Maan включено в состав коммита
79

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
80
### How services are linked to the job
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
81
82

To better understand how the container linking works, read
Takuya Noguchi's avatar
Takuya Noguchi включено в состав коммита
83
[Linking containers together][linking-containers].
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
84
85

To summarize, if you add `mysql` as service to your application, the image will
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
86
then be used to create a container that is linked to the job container.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
87
88
89

The service container for MySQL will be accessible under the hostname `mysql`.
So, in order to access your database service you have to connect to the host
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
90
91
92
named `mysql` instead of a socket or `localhost`. Read more in [accessing the
services](#accessing-the-services).

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
93
### How the health check of services works
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
94
95

Services are designed to provide additional functionality which is **network accessible**.
Philippe Lafoucrière's avatar
Philippe Lafoucrière включено в состав коммита
96
It may be a database like MySQL, or Redis, and even `docker:stable-dind` which
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
97
98
allows you to use Docker in Docker. It can be practically anything that is
required for the CI/CD job to proceed and is accessed by network.
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
99

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
100
To make sure this works, the Runner:
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
101

Evan Read's avatar
Evan Read включено в состав коммита
102
103
1. Checks which ports are exposed from the container by default.
1. Starts a special container that waits for these ports to be accessible.
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
104

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
105
106
107
When the second stage of the check fails, either because there is no opened port in the
service, or the service was not started properly before the timeout and the port is not
responding, it prints the warning: `*** WARNING: Service XYZ probably didn't start properly`.
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
108

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
109
110
In most cases it will affect the job, but there may be situations when the job
will still succeed even if that warning was printed. For example:
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
111

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
112
- The service was started a little after the warning was raised, and the job is
Evan Read's avatar
Evan Read включено в состав коммита
113
  not using the linked service from the beginning. In that case, when the
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
114
115
116
117
118
119
  job needed to access the service, it may have been already there waiting for
  connections.
- The service container is not providing any networking service, but it's doing
  something with the job's directory (all services have the job directory mounted
  as a volume under `/builds`). In that case, the service will do its job, and
  since the job is not trying to connect to it, it won't fail.
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
120
121
122
123

### What services are not for

As it was mentioned before, this feature is designed to provide **network accessible**
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
124
services. A database is the simplest example of such a service.
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
125

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
126
127
128
NOTE: **Note:**
The services feature is not designed to, and will not add any software from the
defined `services` image(s) to the job's container.
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
129

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
130
131
132
For example, if you have the following `services` defined in your job, the `php`,
`node` or `go` commands will **not** be available for your script, and thus
the job will fail:
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
133
134
135
136
137
138
139
140
141
142
143
144
145
146

```yaml
job:
  services:
  - php:7
  - node:latest
  - golang:1.10
  image: alpine:3.7
  script:
  - php -v
  - node -v
  - go version
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
147
148
If you need to have `php`, `node` and `go` available for your script, you should
either:
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
149

Evan Read's avatar
Evan Read включено в состав коммита
150
151
152
- Choose an existing Docker image that contains all required tools.
- Create your own Docker image, which will have all the required tools included
  and use that in your job.
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
153

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
154
155
156
157
### Accessing the services

Let's say that you need a Wordpress instance to test some API integration with
your application.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
158

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
159
160
You can then use for example the [tutum/wordpress][] image in your
`.gitlab-ci.yml`:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
161

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
162
163
164
165
```yaml
services:
- tutum/wordpress:latest
```
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
166

Johan Brandhorst's avatar
Johan Brandhorst включено в состав коммита
167
If you don't [specify a service alias](#available-settings-for-services),
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
168
169
when the job is run, `tutum/wordpress` will be started and you will have
access to it from your build container under two hostnames to choose from:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
170

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
171
172
- `tutum-wordpress`
- `tutum__wordpress`
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
173

Evan Read's avatar
Evan Read включено в состав коммита
174
NOTE: **Note:**
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
175
176
177
178
179
180
Hostnames with underscores are not RFC valid and may cause problems in 3rd party
applications.

The default aliases for the service's hostname are created from its image name
following these rules:

Evan Read's avatar
Evan Read включено в состав коммита
181
- Everything after the colon (`:`) is stripped.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
182
- Slash (`/`) is replaced with double underscores (`__`) and the primary alias
Evan Read's avatar
Evan Read включено в состав коммита
183
  is created.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
184
- Slash (`/`) is replaced with a single dash (`-`) and the secondary alias is
Evan Read's avatar
Evan Read включено в состав коммита
185
  created (requires GitLab Runner v1.1.0 or higher).
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
186
187

To override the default behavior, you can
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
188
[specify a service alias](#available-settings-for-services).
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
189
190

## Define `image` and `services` from `.gitlab-ci.yml`
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
191
192

You can simply define an image that will be used for all jobs and a list of
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
193
services that you want to use during build time:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
194
195

```yaml
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
196
image: ruby:2.2
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
197

Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
198
199
services:
  - postgres:9.3
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
200

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
201
before_script:
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
202
  - bundle install
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
203

Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
204
205
206
207
208
test:
  script:
  - bundle exec rake spec
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
209
210
211
212
It is also possible to define different images and services per job:

```yaml
before_script:
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  - bundle install

test:2.1:
  image: ruby:2.1
  services:
  - postgres:9.3
  script:
  - bundle exec rake spec

test:2.2:
  image: ruby:2.2
  services:
  - postgres:9.4
  script:
  - bundle exec rake spec
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
230
231
Or you can pass some [extended configuration options](#extended-docker-configuration-options)
for `image` and `services`:
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
232
233
234
235

```yaml
image:
  name: ruby:2.2
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
236
  entrypoint: ["/bin/bash"]
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
237
238
239
240

services:
- name: my-postgres:9.4
  alias: db-postgres
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
241
242
  entrypoint: ["/usr/local/bin/db-postgres"]
  command: ["start"]
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
243
244
245
246
247
248
249
250
251

before_script:
- bundle install

test:
  script:
  - bundle exec rake spec
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
252
## Extended Docker configuration options
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
253

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
254
> Introduced in GitLab and GitLab Runner 9.4.
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
255

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
256
257
When configuring the `image` or `services` entries, you can use a string or a map as
options:
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
258

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
259
260
261
262
263
- when using a string as an option, it must be the full name of the image to use
  (including the Registry part if you want to download the image from a Registry
  other than Docker Hub)
- when using a map as an option, then it must contain at least the `name`
  option, which is the same name of the image as used for the string setting
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
264

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
265
For example, the following two definitions are equal:
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
266

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
267
1. Using a string as an option to `image` and `services`:
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
268

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
    ```yaml
    image: "registry.example.com/my/image:latest"

    services:
    - postgresql:9.4
    - redis:latest
    ```

1. Using a map as an option to `image` and `services`. The use of `image:name` is
   required:

    ```yaml
    image:
      name: "registry.example.com/my/image:latest"

    services:
    - name: postgresql:9.4
    - name: redis:latest
    ```
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
288
289
290

### Available settings for `image`

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
291
> Introduced in GitLab and GitLab Runner 9.4.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
292

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
293
294
295
296
| Setting    | Required | GitLab version | Description |
|------------|----------|----------------| ----------- |
| `name`     | yes, when used with any other option      | 9.4 |Full name of the image that should be used. It should contain the Registry part if needed. |
| `entrypoint` | no     | 9.4 |Command or script that should be executed as the container's entrypoint. It will be translated to Docker's `--entrypoint` option while creating the container. The syntax is similar to [`Dockerfile`'s `ENTRYPOINT`][entrypoint] directive, where each shell token is a separate string in the array. |
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
297
298

### Available settings for `services`
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
299

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
300
> Introduced in GitLab and GitLab Runner 9.4.
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
301

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
302
303
304
305
306
307
| Setting    | Required | GitLab version | Description |
|------------|----------|----------------| ----------- |
| `name`       | yes, when used with any other option  | 9.4 | Full name of the image that should be used. It should contain the Registry part if needed. |
| `entrypoint` | no     | 9.4 |Command or script that should be executed as the container's entrypoint. It will be translated to Docker's `--entrypoint` option while creating the container. The syntax is similar to [`Dockerfile`'s `ENTRYPOINT`][entrypoint] directive, where each shell token is a separate string in the array. |
| `command`    | no       | 9.4 |Command or script that should be used as the container's command. It will be translated to arguments passed to Docker after the image's name. The syntax is similar to [`Dockerfile`'s `CMD`][cmd] directive, where each shell token is a separate string in the array. |
| `alias`      | no       | 9.4 |Additional alias that can be used to access the service from the job's container. Read [Accessing the services](#accessing-the-services) for more information. |
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
308

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
309
### Starting multiple services from the same image
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
310

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
311
312
313
> Introduced in GitLab and GitLab Runner 9.4. Read more about the [extended
configuration options](#extended-docker-configuration-options).

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
314
315
Before the new extended Docker configuration options, the following configuration
would not work properly:
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
316
317
318
319
320
321
322

```yaml
services:
- mysql:latest
- mysql:latest
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
323
324
325
326
The Runner would start two containers using the `mysql:latest` image, but both
of them would be added to the job's container with the `mysql` alias based on
the [default hostname naming](#accessing-the-services). This would end with one
of the services not being accessible.
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
327

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
328
329
After the new extended Docker configuration options, the above example would
look like:
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
330
331
332
333
334
335

```yaml
services:
- name: mysql:latest
  alias: mysql-1
- name: mysql:latest
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
336
  alias: mysql-2
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
337
338
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
339
The Runner will still start two containers using the `mysql:latest` image,
Evan Read's avatar
Evan Read включено в состав коммита
340
however now each of them will also be accessible with the alias configured
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
341
342
in `.gitlab-ci.yml` file.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
343
### Setting a command for the service
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
344

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
345
346
347
> Introduced in GitLab and GitLab Runner 9.4. Read more about the [extended
configuration options](#extended-docker-configuration-options).

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
348
349
350
351
352
Let's assume you have a `super/sql:latest` image with some SQL database
inside it and you would like to use it as a service for your job. Let's also
assume that this image doesn't start the database process while starting
the container and the user needs to manually use `/usr/bin/super-sql run` as
a command to start the database.
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
353

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
354
355
356
Before the new extended Docker configuration options, you would need to create
your own image based on the `super/sql:latest` image, add the default command,
and then use it in job's configuration, like:
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
357
358
359

```Dockerfile
# my-super-sql:latest image's Dockerfile
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
360

Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
361
362
363
364
365
366
367
368
369
370
371
FROM super/sql:latest
CMD ["/usr/bin/super-sql", "run"]
```

```yaml
# .gitlab-ci.yml

services:
- my-super-sql:latest
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
372
373
After the new extended Docker configuration options, you can now simply
set a `command` in `.gitlab-ci.yml`, like:
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
374
375
376
377
378
379

```yaml
# .gitlab-ci.yml

services:
- name: super/sql:latest
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
380
  command: ["/usr/bin/super-sql", "run"]
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
381
382
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
383
As you can see, the syntax of `command` is similar to [Dockerfile's `CMD`][cmd].
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
384

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
385
### Overriding the entrypoint of an image
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
386

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
387
388
389
> Introduced in GitLab and GitLab Runner 9.4. Read more about the [extended
configuration options](#extended-docker-configuration-options).

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
390
391
392
393
394
395
396
397
Before showing the available entrypoint override methods, let's describe shortly
how the Runner starts and uses a Docker image for the containers used in the
CI jobs:

1. The Runner starts a Docker container using the defined entrypoint (default
   from `Dockerfile` that may be overridden in `.gitlab-ci.yml`)
1. The Runner attaches itself to a running container.
1. The Runner prepares a script (the combination of
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
398
   [`before_script`](../yaml/README.md#before_script-and-after_script),
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
399
   [`script`](../yaml/README.md#script),
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
400
   and [`after_script`](../yaml/README.md#before_script-and-after_script)).
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
401
402
403
404
405
406
407
408
409
410
411
412
413
414
1. The Runner sends the script to the container's shell STDIN and receives the
   output.

To override the entrypoint of a Docker image, the recommended solution is to
define an empty `entrypoint` in `.gitlab-ci.yml`, so the Runner doesn't start
a useless shell layer. However, that will not work for all Docker versions, and
you should check which one your Runner is using. Specifically:

- If Docker 17.06 or later is used, the `entrypoint` can be set to an empty value.
- If Docker 17.03 or previous versions are used, the `entrypoint` can be set to
  `/bin/sh -c`, `/bin/bash -c` or an equivalent shell available in the image.

The syntax of `image:entrypoint` is similar to [Dockerfile's `ENTRYPOINT`][entrypoint].

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
415
416
417
418
Let's assume you have a `super/sql:experimental` image with some SQL database
inside it and you would like to use it as a base image for your job because you
want to execute some tests with this database binary. Let's also assume that
this image is configured with `/usr/bin/super-sql run` as an entrypoint. That
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
419
means that when starting the container without additional options, it will run
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
420
the database's process, while Runner expects that the image will have no
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
421
422
entrypoint or that the entrypoint is prepared to start a shell command.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
423
424
425
426
With the extended Docker configuration options, instead of creating your
own image based on `super/sql:experimental`, setting the `ENTRYPOINT`
to a shell, and then using the new image in your CI job, you can now simply
define an `entrypoint` in `.gitlab-ci.yml`.
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
427

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
428
**For Docker 17.06+:**
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
429
430
431
432

```yaml
image:
  name: super/sql:experimental
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
433
434
435
  entrypoint: [""]
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
436
437
438
**For Docker =< 17.03:**

```yaml
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
439
440
441
image:
  name: super/sql:experimental
  entrypoint: ["/bin/sh", "-c"]
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
442
443
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
444
## Define image and services in `config.toml`
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
445
446
447

Look for the `[runners.docker]` section:

Evan Read's avatar
Evan Read включено в состав коммита
448
```toml
Douwe Maan's avatar
Douwe Maan включено в состав коммита
449
450
451
452
453
[runners.docker]
  image = "ruby:2.1"
  services = ["mysql:latest", "postgres:latest"]
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
454
The image and services defined this way will be added to all job run by
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
455
that runner.
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
456

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
457
## Define an image from a private Container Registry
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
458

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
459
> **Notes:**
Brett Walker's avatar
Brett Walker включено в состав коммита
460
461
462
463
464
465
466
467
>
> - This feature requires GitLab Runner **1.8** or higher
> - For GitLab Runner versions **>= 0.6, <1.8** there was a partial
>   support for using private registries, which required manual configuration
>   of credentials on runner's host. We recommend to upgrade your Runner to
>   at least version **1.8** if you want to use private registries.
> - If the repository is private you need to authenticate your GitLab Runner in the
>   registry. Learn more about how [GitLab Runner works in this case][runner-priv-reg].
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
468

Andrei Burd's avatar
Andrei Burd включено в состав коммита
469
470
471
472
473
474
475
476
477
478
479
480
To access private container registries, the GitLab Runner process can use:

- [Statically defined credentials](#using-statically-defined-credentials). That is, a username and password for a specific registry.
- [Credentials Store](#using-credentials-store). For more information, see [the relevant Docker documentation](https://docs.docker.com/engine/reference/commandline/login/#credentials-store).
- [Credential Helpers](#using-credential-helpers). For more information, see [the relevant Docker documentation](https://docs.docker.com/engine/reference/commandline/login/#credential-helpers).

To define which should be used, the GitLab Runner process reads the configuration in the following order:

- `DOCKER_AUTH_CONFIG` variable provided as either:
  - A [variable](../variables/README.md#gitlab-cicd-environment-variables) in `.gitlab-ci.yml`.
  - A project's variables stored on the projects **Settings > CI/CD** page.
- `DOCKER_AUTH_CONFIG` variable provided as environment variable in `config.toml` of the Runner.
Per Lundberg's avatar
Per Lundberg включено в состав коммита
481
- `config.json` file placed in `$HOME/.docker` directory of the user running GitLab Runner process.
Andrei Burd's avatar
Andrei Burd включено в состав коммита
482
483
484
  If the `--user` flag is provided to run the GitLab Runner child processes as unprivileged user,
  the home directory of the main GitLab Runner process user will be used.

Evan Read's avatar
Evan Read включено в состав коммита
485
486
NOTE: **Note:**
GitLab Runner reads this configuration **only** from `config.toml` and ignores it if
Andrei Burd's avatar
Andrei Burd включено в состав коммита
487
488
489
490
491
it's provided as an environment variable. This is because GitLab Runnner uses **only**
`config.toml` configuration and doesn't interpolate **ANY** environment variables at
runtime.

### Using statically-defined credentials
Evan Read's avatar
Evan Read включено в состав коммита
492

Drew Blessing's avatar
Drew Blessing включено в состав коммита
493
As an example, let's assume that you want to use the `registry.example.com:5000/private/image:latest`
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
494
image which is private and requires you to login into a private container registry.
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
495
496
497

Let's also assume that these are the login credentials:

Evan Read's avatar
Evan Read включено в состав коммита
498
499
500
501
502
| Key      | Value                       |
|:---------|:----------------------------|
| registry | `registry.example.com:5000` |
| username | `my_username`               |
| password | `my_password`               |
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
503

Drew Blessing's avatar
Drew Blessing включено в состав коммита
504
To configure access for `registry.example.com:5000`, follow these steps:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
505

Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
506
507
1. Find what the value of `DOCKER_AUTH_CONFIG` should be. There are two ways to
   accomplish this:
Brett Walker's avatar
Brett Walker включено в состав коммита
508
   - **First way -** Do a `docker login` on your local machine:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
509

Brett Walker's avatar
Brett Walker включено в состав коммита
510
        ```bash
Drew Blessing's avatar
Drew Blessing включено в состав коммита
511
        docker login registry.example.com:5000 --username my_username --password my_password
Brett Walker's avatar
Brett Walker включено в состав коммита
512
        ```
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
513

Brett Walker's avatar
Brett Walker включено в состав коммита
514
515
        Then copy the content of `~/.docker/config.json`.
   - **Second way -** In some setups, it's possible that Docker client will use
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
516
517
518
519
520
521
       the available system keystore to store the result of `docker login`. In
       that case, it's impossible to read `~/.docker/config.json`, so you will
       need to prepare the required base64-encoded version of
       `${username}:${password}` manually. Open a terminal and execute the
       following command:

Brett Walker's avatar
Brett Walker включено в состав коммита
522
523
        ```bash
        echo -n "my_username:my_password" | base64
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
524

Brett Walker's avatar
Brett Walker включено в состав коммита
525
526
527
        # Example output to copy
        bXlfdXNlcm5hbWU6bXlfcGFzc3dvcmQ=
        ```
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
528

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
529
1. Create a [variable](../variables/README.md#gitlab-cicd-environment-variables) `DOCKER_AUTH_CONFIG` with the content of the
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
530
531
   Docker configuration file as the value:

Brett Walker's avatar
Brett Walker включено в состав коммита
532
533
534
    ```json
    {
        "auths": {
Drew Blessing's avatar
Drew Blessing включено в состав коммита
535
            "registry.example.com:5000": {
Brett Walker's avatar
Brett Walker включено в состав коммита
536
537
538
539
540
                "auth": "bXlfdXNlcm5hbWU6bXlfcGFzc3dvcmQ="
            }
        }
    }
    ```
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
541

Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
542
543
1. Optionally,if you followed the first way of finding the `DOCKER_AUTH_CONFIG`
   value, do a `docker logout` on your computer if you don't need access to the
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
544
545
   registry from it:

Brett Walker's avatar
Brett Walker включено в состав коммита
546
    ```bash
Drew Blessing's avatar
Drew Blessing включено в состав коммита
547
    docker logout registry.example.com:5000
Brett Walker's avatar
Brett Walker включено в состав коммита
548
    ```
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
549

Drew Blessing's avatar
Drew Blessing включено в состав коммита
550
1. You can now use any private image from `registry.example.com:5000` defined in
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
551
   `image` and/or `services` in your `.gitlab-ci.yml` file:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
552

Brett Walker's avatar
Brett Walker включено в состав коммита
553
    ```yaml
Drew Blessing's avatar
Drew Blessing включено в состав коммита
554
    image: registry.example.com:5000/namespace/image:tag
Brett Walker's avatar
Brett Walker включено в состав коммита
555
    ```
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
556

Drew Blessing's avatar
Drew Blessing включено в состав коммита
557
    In the example above, GitLab Runner will look at `registry.example.com:5000` for the
Brett Walker's avatar
Brett Walker включено в состав коммита
558
    image `namespace/image:tag`.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
559
560
561

You can add configuration for as many registries as you want, adding more
registries to the `"auths"` hash as described above.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
562

Evan Read's avatar
Evan Read включено в состав коммита
563
564
NOTE: **Note:**
The full `hostname:port` combination is required everywhere
Drew Blessing's avatar
Drew Blessing включено в состав коммита
565
566
567
568
569
for the Runner to match the `DOCKER_AUTH_CONFIG`. For example, if
`registry.example.com:5000/namespace/image:tag` is specified in `.gitlab-ci.yml`,
then the `DOCKER_AUTH_CONFIG` must also specify `registry.example.com:5000`.
Specifying only `registry.example.com` will not work.

Andrei Burd's avatar
Andrei Burd включено в состав коммита
570
571
572
573
574
575
576
### Using Credentials Store

> Support for using Credentials Store was added in GitLab Runner 9.5.

To configure credentials store, follow these steps:

1. To use a credentials store, you need an external helper program to interact with a specific keychain or external store.
Evan Read's avatar
Evan Read включено в состав коммита
577
   Make sure helper program is available in GitLab Runner `$PATH`.
Andrei Burd's avatar
Andrei Burd включено в состав коммита
578
579

1. Make GitLab Runner use it. There are two ways to accomplish this. Either:
Evan Read's avatar
Evan Read включено в состав коммита
580
   - Create a
Andrei Burd's avatar
Andrei Burd включено в состав коммита
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
     [variable](../variables/README.md#gitlab-cicd-environment-variables)
     `DOCKER_AUTH_CONFIG` with the content of the
   Docker configuration file as the value:

    ```json
      {
        "credsStore": "osxkeychain"
      }
    ```

   - Or, if you are running self-hosted Runners, add the above JSON to
     `${GITLAB_RUNNER_HOME}/.docker/config.json`. GitLab Runner will read this config file
     and will use the needed helper for this specific repository.

NOTE: **Note:** `credsStore` is used to access ALL the registries.
If you will want to use both images from private registry and public images from DockerHub,
pulling from DockerHub will fail, because Docker daemon will try to use the same credentials for **ALL** the registries.

### Using Credential Helpers

> Support for using Credential Helpers was added in GitLab Runner 12.0

As an example, let's assume that you want to use the `aws_account_id.dkr.ecr.region.amazonaws.com/private/image:latest`
image which is private and requires you to log in into a private container registry.

To configure access for `aws_account_id.dkr.ecr.region.amazonaws.com`, follow these steps:

1. Make sure `docker-credential-ecr-login` is available in GitLab Runner's `$PATH`.

1. Make GitLab Runner use it. There are two ways to accomplish this. Either:
   - Create a [variable](../variables/README.md#gitlab-cicd-environment-variables)
     `DOCKER_AUTH_CONFIG` with the content of the
   Docker configuration file as the value:

    ```json
    {
      "credHelpers": {
        "aws_account_id.dkr.ecr.region.amazonaws.com": "ecr-login"
      }
    }
    ```

   - Or, if you are running self-hosted Runners,
     add the above JSON to `${GITLAB_RUNNER_HOME}/.docker/config.json`.
     GitLab Runner will read this config file and will use the needed helper for this
     specific repository.

1. You can now use any private image from `aws_account_id.dkr.ecr.region.amazonaws.com` defined in
   `image` and/or `services` in your `.gitlab-ci.yml` file:

    ```yaml
    image: aws_account_id.dkr.ecr.region.amazonaws.com/private/image:latest
    ```

    In the example above, GitLab Runner will look at `aws_account_id.dkr.ecr.region.amazonaws.com` for the
    image `private/image:latest`.

You can add configuration for as many registries as you want, adding more
registries to the `"credHelpers"` hash as described above.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
641
## Configuring services
Douwe Maan's avatar
Douwe Maan включено в состав коммита
642

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
643
644
Many services accept environment variables which allow you to easily change
database names or set account names depending on the environment.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
645

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
646
647
GitLab Runner 0.5.0 and up passes all YAML-defined variables to the created
service containers.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
648

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
649
650
For all possible configuration variables check the documentation of each image
provided in their corresponding Docker hub page.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
651

Evan Read's avatar
Evan Read включено в состав коммита
652
653
654
NOTE: **Note:**
All variables will be passed to all services containers. It's not
designed to distinguish which variable should go where.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
655

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
656
### PostgreSQL service example
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
657

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
658
659
See the specific documentation for
[using PostgreSQL as a service](../services/postgres.md).
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
660

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
661
### MySQL service example
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
662

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
663
664
See the specific documentation for
[using MySQL as a service](../services/mysql.md).
Douwe Maan's avatar
Douwe Maan включено в состав коммита
665

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
666
## How Docker integration works
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
667

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
668
Below is a high level overview of the steps performed by Docker during job
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
669
670
time.

Douwe Maan's avatar
Douwe Maan включено в состав коммита
671
1. Create any service container: `mysql`, `postgresql`, `mongodb`, `redis`.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
672
673
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).
Douwe Maan's avatar
Douwe Maan включено в состав коммита
674
1. Create build container and link any service container to build container.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
675
676
1. Start build container and send job script to the container.
1. Run job script.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
677
678
679
680
681
1. Checkout code in: `/builds/group-name/project-name/`.
1. Run any step defined in `.gitlab-ci.yml`.
1. Check exit status of build script.
1. Remove build container and all created service containers.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
682
## How to debug a job locally
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
683

Evan Read's avatar
Evan Read включено в состав коммита
684
685
686
NOTE: **Note:**
The following commands are run without root privileges. You should be
able to run Docker with your regular user account.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
687

688
First start with creating a file named `build_script`:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
689

Douwe Maan's avatar
Douwe Maan включено в состав коммита
690
```bash
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
691
cat <<EOF > build_script
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
692
693
git clone https://gitlab.com/gitlab-org/gitlab-runner.git /builds/gitlab-org/gitlab-runner
cd /builds/gitlab-org/gitlab-runner
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
694
make
Douwe Maan's avatar
Douwe Maan включено в состав коммита
695
696
697
EOF
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
698
699
700
701
702
703
704
Here we use as an example the GitLab Runner repository which contains a
Makefile, so running `make` will execute the commands defined in the Makefile.
Your mileage may vary, so instead of `make` you could run the command which
is specific to your project.

Then create some service containers:

Evan Read's avatar
Evan Read включено в состав коммита
705
```sh
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
706
707
docker run -d --name service-mysql mysql:latest
docker run -d --name service-postgres postgres:latest
Douwe Maan's avatar
Douwe Maan включено в состав коммита
708
709
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
710
711
712
713
714
715
716
This will create two service containers, named `service-mysql` and
`service-postgres` which use the latest MySQL and PostgreSQL images
respectively. They will both run in the background (`-d`).

Finally, create a build container by executing the `build_script` file we
created earlier:

Evan Read's avatar
Evan Read включено в состав коммита
717
```sh
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
718
docker run --name build -i --link=service-mysql:mysql --link=service-postgres:postgres ruby:2.1 /bin/bash < build_script
Douwe Maan's avatar
Douwe Maan включено в состав коммита
719
720
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
721
722
723
724
725
726
727
728
The above command will create a container named `build` that is spawned from
the `ruby:2.1` image and has two services linked to it. The `build_script` is
piped using STDIN to the bash interpreter which in turn executes the
`build_script` in the `build` container.

When you finish testing and no longer need the containers, you can remove them
with:

Evan Read's avatar
Evan Read включено в состав коммита
729
```sh
Douwe Maan's avatar
Douwe Maan включено в состав коммита
730
731
docker rm -f -v build service-mysql service-postgres
```
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
732
733
734
735
736

This will forcefully (`-f`) remove the `build` container, the two service
containers as well as all volumes (`-v`) that were created with the container
creation.

Denis Meiswinkel's avatar
Denis Meiswinkel включено в состав коммита
737
[Docker Fundamentals]: https://docs.docker.com/engine/understanding-docker/
Adam Boseley's avatar
Adam Boseley включено в состав коммита
738
[docker pull policy]: https://docs.gitlab.com/runner/executors/docker.html#how-pull-policies-work
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
739
740
[hub]: https://hub.docker.com/
[linking-containers]: https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/
Takuya Noguchi's avatar
Takuya Noguchi включено в состав коммита
741
742
743
[tutum/wordpress]: https://hub.docker.com/r/tutum/wordpress/
[postgres-hub]: https://hub.docker.com/r/_/postgres/
[mysql-hub]: https://hub.docker.com/r/_/mysql/
Evan Read's avatar
Evan Read включено в состав коммита
744
[runner-priv-reg]: https://docs.gitlab.com/runner/configuration/advanced-configuration.html#using-a-private-container-registry
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
745
746
747
[entrypoint]: https://docs.docker.com/engine/reference/builder/#entrypoint
[cmd]: https://docs.docker.com/engine/reference/builder/#cmd
[register]: https://docs.gitlab.com/runner/register/