2015-08-25 21:42:46 -04:00
|
|
|
# Using Docker Images
|
|
|
|
|
2015-12-17 01:53:00 -05:00
|
|
|
GitLab CI in conjunction with [GitLab Runner](../runners/README.md) can use
|
2015-12-08 16:29:49 -05:00
|
|
|
[Docker Engine](https://www.docker.com/) to test and build any application.
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
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
|
2015-12-17 01:53:00 -05:00
|
|
|
instance. [Docker Hub][hub] has a rich database of pre-built images that can be
|
2015-12-02 17:36:45 -05:00
|
|
|
used to test and build your applications.
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-12-02 17:36:45 -05:00
|
|
|
Docker, when used with GitLab CI, runs each build in a separate and isolated
|
2015-12-08 16:29:49 -05:00
|
|
|
container using the predefined image that is set up in
|
|
|
|
[`.gitlab-ci.yml`](../yaml/README.md).
|
2015-12-02 17:36:45 -05:00
|
|
|
|
|
|
|
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
|
2015-12-08 16:29:49 -05:00
|
|
|
test them on a dedicated CI server.
|
2015-12-02 17:36:45 -05:00
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
## Register docker runner
|
2015-12-02 17:36:45 -05:00
|
|
|
|
|
|
|
To use GitLab Runner with docker you need to register a new runner to use the
|
|
|
|
`docker` executor:
|
2015-08-25 21:42:46 -04:00
|
|
|
|
|
|
|
```bash
|
2016-06-08 03:28:23 -04:00
|
|
|
gitlab-ci-multi-runner register \
|
2015-09-15 18:28:59 -04:00
|
|
|
--url "https://gitlab.com/" \
|
2015-08-25 21:42:46 -04:00
|
|
|
--registration-token "PROJECT_REGISTRATION_TOKEN" \
|
|
|
|
--description "docker-ruby-2.1" \
|
|
|
|
--executor "docker" \
|
|
|
|
--docker-image ruby:2.1 \
|
|
|
|
--docker-postgres latest \
|
|
|
|
--docker-mysql latest
|
|
|
|
```
|
|
|
|
|
2015-12-02 17:36:45 -05:00
|
|
|
The registered runner will use the `ruby:2.1` docker image and will run two
|
|
|
|
services, `postgres:latest` and `mysql:latest`, both of which will be
|
|
|
|
accessible during the build process.
|
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
## What is image
|
2015-12-02 17:36:45 -05:00
|
|
|
|
|
|
|
The `image` keyword is the name of the docker image that is present in the
|
|
|
|
local Docker Engine (list all images with `docker images`) or any image that
|
|
|
|
can be found at [Docker Hub][hub]. For more information about images and Docker
|
|
|
|
Hub please read the [Docker Fundamentals][] documentation.
|
|
|
|
|
|
|
|
In short, with `image` we refer to the docker image, which will be used to
|
|
|
|
create a container on which your build will run.
|
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
## What is service
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
The `services` keyword defines just another docker image that is run during
|
2015-12-02 17:36:45 -05:00
|
|
|
your build and is linked to the docker image that the `image` keyword defines.
|
|
|
|
This allows you to access the service image during build time.
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-12-02 17:36:45 -05:00
|
|
|
The service image can run any application, but the most common use case is to
|
|
|
|
run a database container, eg. `mysql`. It's easier and faster to use an
|
|
|
|
existing image and run it as an additional container than install `mysql` every
|
|
|
|
time the project is built.
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
You can see some widely used services examples in the relevant documentation of
|
|
|
|
[CI services examples](../services/README.md).
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
### How is service linked to the build
|
|
|
|
|
|
|
|
To better understand how the container linking works, read
|
2016-05-07 01:54:36 -04:00
|
|
|
[Linking containers together][linking-containers].
|
2015-12-02 17:36:45 -05:00
|
|
|
|
|
|
|
To summarize, if you add `mysql` as service to your application, the image will
|
|
|
|
then be used to create a container that is linked to the build container.
|
|
|
|
|
|
|
|
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
|
|
|
|
named `mysql` instead of a socket or `localhost`.
|
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
## Overwrite image and services
|
2015-12-03 08:43:06 -05:00
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
See [How to use other images as services](#how-to-use-other-images-as-services).
|
2015-12-03 08:43:06 -05:00
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
## How to use other images as services
|
2015-12-02 17:36:45 -05:00
|
|
|
|
|
|
|
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] can be used as a service.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
## Define image and services from `.gitlab-ci.yml`
|
2015-12-02 17:36:45 -05:00
|
|
|
|
|
|
|
You can simply define an image that will be used for all jobs and a list of
|
|
|
|
services that you want to use during build time.
|
|
|
|
|
|
|
|
```yaml
|
2015-11-25 08:41:14 -05:00
|
|
|
image: ruby:2.2
|
2015-12-08 16:29:49 -05:00
|
|
|
|
2015-11-25 08:41:14 -05:00
|
|
|
services:
|
|
|
|
- postgres:9.3
|
2015-12-08 16:29:49 -05:00
|
|
|
|
2015-12-02 17:36:45 -05:00
|
|
|
before_script:
|
2015-11-25 08:41:14 -05:00
|
|
|
- bundle install
|
2015-12-02 17:36:45 -05:00
|
|
|
|
2015-11-25 08:41:14 -05:00
|
|
|
test:
|
|
|
|
script:
|
|
|
|
- bundle exec rake spec
|
|
|
|
```
|
|
|
|
|
2015-12-02 17:36:45 -05:00
|
|
|
It is also possible to define different images and services per job:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
before_script:
|
2015-11-25 08:41:14 -05:00
|
|
|
- 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
|
|
|
|
```
|
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
## Define image and services in `config.toml`
|
2015-12-02 17:36:45 -05:00
|
|
|
|
|
|
|
Look for the `[runners.docker]` section:
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
```
|
|
|
|
[runners.docker]
|
|
|
|
image = "ruby:2.1"
|
|
|
|
services = ["mysql:latest", "postgres:latest"]
|
|
|
|
```
|
|
|
|
|
2015-12-02 17:36:45 -05:00
|
|
|
The image and services defined this way will be added to all builds run by
|
|
|
|
that runner.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-17 01:53:00 -05:00
|
|
|
## Define an image from a private Docker registry
|
|
|
|
|
|
|
|
Starting with GitLab Runner 0.6.0, you are able to define images located to
|
|
|
|
private registries that could also require authentication.
|
|
|
|
|
|
|
|
All you have to do is be explicit on the image definition in `.gitlab-ci.yml`.
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
image: my.registry.tld:5000/namepace/image:tag
|
|
|
|
```
|
|
|
|
|
|
|
|
In the example above, GitLab Runner will look at `my.registry.tld:5000` for the
|
|
|
|
image `namespace/image:tag`.
|
|
|
|
|
|
|
|
If the repository is private you need to authenticate your GitLab Runner in the
|
|
|
|
registry. Learn how to do that on
|
|
|
|
[GitLab Runner's documentation][runner-priv-reg].
|
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
## Accessing the services
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-02 17:36:45 -05:00
|
|
|
Let's say that you need a Wordpress instance to test some API integration with
|
|
|
|
your application.
|
|
|
|
|
|
|
|
You can then use for example the [tutum/wordpress][] image in your
|
|
|
|
`.gitlab-ci.yml`:
|
|
|
|
|
|
|
|
```yaml
|
2015-11-25 08:41:14 -05:00
|
|
|
services:
|
|
|
|
- tutum/wordpress:latest
|
2015-08-25 21:42:46 -04:00
|
|
|
```
|
|
|
|
|
2015-12-02 17:36:45 -05:00
|
|
|
When the build is run, `tutum/wordpress` will be started and you will have
|
2015-12-03 08:43:29 -05:00
|
|
|
access to it from your build container under the hostname `tutum__wordpress`.
|
2015-12-02 17:36:45 -05:00
|
|
|
|
|
|
|
The alias hostname for the service is made from the image name following these
|
|
|
|
rules:
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-12-02 17:36:45 -05:00
|
|
|
1. Everything after `:` is stripped
|
2016-01-11 12:02:39 -05:00
|
|
|
2. Slash (`/`) is replaced with double underscores (`__`)
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
## Configuring services
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-12-02 17:36:45 -05:00
|
|
|
Many services accept environment variables which allow you to easily change
|
|
|
|
database names or set account names depending on the environment.
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-12-02 17:36:45 -05:00
|
|
|
GitLab Runner 0.5.0 and up passes all YAML-defined variables to the created
|
|
|
|
service containers.
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-12-02 17:36:45 -05:00
|
|
|
For all possible configuration variables check the documentation of each image
|
|
|
|
provided in their corresponding Docker hub page.
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
*Note: All variables will be passed to all services containers. It's not
|
|
|
|
designed to distinguish which variable should go where.*
|
2015-12-02 17:36:45 -05:00
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
### PostgreSQL service example
|
2015-12-02 17:36:45 -05:00
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
See the specific documentation for
|
|
|
|
[using PostgreSQL as a service](../services/postgres.md).
|
2015-12-02 17:36:45 -05:00
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
### MySQL service example
|
2015-12-02 17:36:45 -05:00
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
See the specific documentation for
|
|
|
|
[using MySQL as a service](../services/mysql.md).
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
## How Docker integration works
|
2015-12-02 17:36:45 -05:00
|
|
|
|
|
|
|
Below is a high level overview of the steps performed by docker during build
|
|
|
|
time.
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
1. Create any service container: `mysql`, `postgresql`, `mongodb`, `redis`.
|
2015-12-02 17:36:45 -05:00
|
|
|
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).
|
2015-08-25 21:42:46 -04:00
|
|
|
1. Create build container and link any service container to build container.
|
|
|
|
1. Start build container and send build script to the container.
|
|
|
|
1. Run build script.
|
|
|
|
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.
|
|
|
|
|
2015-12-08 16:29:49 -05:00
|
|
|
## How to debug a build locally
|
2015-12-02 17:36:45 -05:00
|
|
|
|
|
|
|
*Note: The following commands are run without root privileges. You should be
|
|
|
|
able to run docker with your regular user account.*
|
|
|
|
|
|
|
|
First start with creating a file named `build script`:
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
```bash
|
2015-12-02 17:36:45 -05:00
|
|
|
cat <<EOF > build_script
|
2015-08-25 21:42:46 -04:00
|
|
|
git clone https://gitlab.com/gitlab-org/gitlab-ci-multi-runner.git /builds/gitlab-org/gitlab-ci-multi-runner
|
|
|
|
cd /builds/gitlab-org/gitlab-ci-multi-runner
|
2015-12-02 17:36:45 -05:00
|
|
|
make
|
2015-08-25 21:42:46 -04:00
|
|
|
EOF
|
|
|
|
```
|
|
|
|
|
2015-12-02 17:36:45 -05:00
|
|
|
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:
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
```
|
2016-04-25 06:10:56 -04:00
|
|
|
docker run -d --name service-mysql mysql:latest
|
|
|
|
docker run -d --name service-postgres postgres:latest
|
2015-08-25 21:42:46 -04:00
|
|
|
```
|
|
|
|
|
2015-12-02 17:36:45 -05:00
|
|
|
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:
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
```
|
2015-12-02 17:36:45 -05:00
|
|
|
docker run --name build -i --link=service-mysql:mysql --link=service-postgres:postgres ruby:2.1 /bin/bash < build_script
|
2015-08-25 21:42:46 -04:00
|
|
|
```
|
|
|
|
|
2015-12-02 17:36:45 -05:00
|
|
|
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:
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
```
|
|
|
|
docker rm -f -v build service-mysql service-postgres
|
|
|
|
```
|
2015-12-02 17:36:45 -05:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2016-02-11 11:03:31 -05:00
|
|
|
[Docker Fundamentals]: https://docs.docker.com/engine/understanding-docker/
|
2015-12-02 17:36:45 -05:00
|
|
|
[hub]: https://hub.docker.com/
|
|
|
|
[linking-containers]: https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/
|
2016-05-07 00:29:24 -04:00
|
|
|
[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/
|
2015-12-17 01:53:00 -05:00
|
|
|
[runner-priv-reg]: https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/docs/configuration/advanced-configuration.md#using-a-private-docker-registry
|