GitLab CI in conjunction with GitLab Runner can use Docker Engine to test and build any application.
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 instance. Docker Hub has a rich database of pre-built images that can be used to test and build your applications.
Docker, when used with GitLab CI, runs each job in a separate and isolated
container using the predefined image that is set up in
.gitlab-ci.yml
.
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 test them on a dedicated CI server.
To use GitLab Runner with Docker you need to register a new Runner
to use the docker
executor.
A one-line example can be seen below:
sudo gitlab-runner register \
--url "https://gitlab.example.com/" \
--registration-token "PROJECT_REGISTRATION_TOKEN" \
--description "docker-ruby-2.1" \
--executor "docker" \
--docker-image ruby:2.1 \
--docker-postgres latest \
--docker-mysql latest
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.
The image
keyword is the name of the Docker image the Docker executor
will run to perform the CI tasks.
By default, the executor will only pull images from Docker Hub,
but this can be configured in the gitlab-runner/config.toml
by setting
the Docker pull policy to allow using local images.
For more information about images and Docker Hub please read the Docker Fundamentals documentation.
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.
This allows you to access the service image during build time.
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.
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 or your private Container Registry can be
used as a service.
You can see some widely used services examples in the relevant documentation of CI services examples.
To better understand how the container linking works, read Linking containers together.
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 job 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
. Read more in accessing the
services.
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
:
services:
- tutum/wordpress:latest
If you don't specify a service alias,
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:
tutum-wordpress
tutum__wordpress
Note: 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:
:
) is stripped/
) is replaced with double underscores (__
) and the primary alias
is created/
) is replaced with a single dash (-
) and the secondary alias is
created (requires GitLab Runner v1.1.0 or higher)To override the default behavior, you can specify a service alias.
image
and services
from .gitlab-ci.yml
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:
image: ruby:2.2
services:
- postgres:9.3
before_script:
- bundle install
test:
script:
- bundle exec rake spec
It is also possible to define different images and services per job:
before_script:
- 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
Or you can pass some extended configuration options
for image
and services
:
image:
name: ruby:2.2
entrypoint: ["/bin/bash"]
services:
- name: my-postgres:9.4
alias: db-postgres
entrypoint: ["/usr/local/bin/db-postgres"]
command: ["start"]
before_script:
- bundle install
test:
script:
- bundle exec rake spec
Note: This feature requires GitLab 9.4 and GitLab Runner 9.4 or higher.
When configuring the image
or services
entries, you can use a string or a map as
options:
name
option, which is the same name of the image as used for the string settingFor example, the following two definitions are equal:
Using a string as an option to image
and services
:
image: "registry.example.com/my/image:latest"
services:
- postgresql:9.4
- redis:latest
Using a map as an option to image
and services
. The use of image:name
is
required:
image:
name: "registry.example.com/my/image:latest"
services:
- name: postgresql:9.4
- name: redis:latest
image
Note: This feature requires GitLab 9.4 and GitLab Runner 9.4 or higher.
Setting | Required | Description |
---|---|---|
name |
yes, when used with any other option | Full name of the image that should be used. It should contain the Registry part if needed. |
entrypoint |
no | 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 directive, where each shell token is a separate string in the array. |
services
Note: This feature requires GitLab 9.4 and GitLab Runner 9.4 or higher.
Setting | Required | Description |
---|---|---|
name |
yes, when used with any other option | Full name of the image that should be used. It should contain the Registry part if needed. |
entrypoint |
no | 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 directive, where each shell token is a separate string in the array. |
command |
no | 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 directive, where each shell token is a separate string in the array. |
alias |
no | Additional alias that can be used to access the service from the job's container. Read Accessing the services for more information. |
Before the new extended Docker configuration options, the following configuration would not work properly:
services:
- mysql:latest
- mysql:latest
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. This would end with one
of the services not being accessible.
After the new extended Docker configuration options, the above example would look like:
services:
- name: mysql:latest
alias: mysql-1
- name: mysql:latest
alias: mysql-2
The Runner will still start two containers using the mysql:latest
image,
but now each of them will also be accessible with the alias configured
in .gitlab-ci.yml
file.
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.
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:
# my-super-sql:latest image's Dockerfile
FROM super/sql:latest
CMD ["/usr/bin/super-sql", "run"]
# .gitlab-ci.yml
services:
- my-super-sql:latest
After the new extended Docker configuration options, you can now simply
set a command
in .gitlab-ci.yml
, like:
# .gitlab-ci.yml
services:
- name: super/sql:latest
command: ["/usr/bin/super-sql", "run"]
As you can see, the syntax of command
is similar to Dockerfile's CMD
.
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
means, that when starting the container without additional options, it will run
the database's process, while Runner expects that the image will have no
entrypoint or at least will start with a shell as its entrypoint.
Previously we would need to create our own image based on the
super/sql:experimental
image, set the entrypoint to a shell, and then use
it in job's configuration, e.g.:
Before the new extended Docker configuration options, you would need to create
your own image based on the super/sql:experimental
image, set the entrypoint
to a shell and then use it in job's configuration, like:
# my-super-sql:experimental image's Dockerfile
FROM super/sql:experimental
ENTRYPOINT ["/bin/sh"]
# .gitlab-ci.yml
image: my-super-sql:experimental
After the new extended Docker configuration options, you can now simply
set an entrypoint
in .gitlab-ci.yml
, like:
# .gitlab-ci.yml
image:
name: super/sql:experimental
entrypoint: ["/bin/sh"]
As you can see the syntax of entrypoint
is similar to
Dockerfile's ENTRYPOINT
.
config.toml
Look for the [runners.docker]
section:
[runners.docker]
image = "ruby:2.1"
services = ["mysql:latest", "postgres:latest"]
The image and services defined this way will be added to all job run by that runner.
Notes:
As an example, let's assume that you want to use the registry.example.com/private/image:latest
image which is private and requires you to login into a private container registry.
To configure access for registry.example.com
, follow these steps:
Do a docker login
on your computer:
docker login registry.example.com --username my_username --password my_password
Copy the content of ~/.docker/config.json
Create a secret variable DOCKER_AUTH_CONFIG
with the content of the
Docker configuration file as the value:
{
"auths": {
"registry.example.com": {
"auth": "bXlfdXNlcm5hbWU6bXlfcGFzc3dvcmQ="
}
}
}
Do a docker logout
on your computer if you don't need access to the
registry from it:
docker logout registry.example.com
You can now use any private image from registry.example.com
defined in
image
and/or services
in your [.gitlab-ci.yml
file][yaml-priv-reg]:
image: my.registry.tld:5000/namespace/image:tag
In the example above, GitLab Runner will look at my.registry.tld:5000
for the
image namespace/image:tag
.
You can add configuration for as many registries as you want, adding more
registries to the "auths"
hash as described above.
Many services accept environment variables which allow you to easily change database names or set account names depending on the environment.
GitLab Runner 0.5.0 and up passes all YAML-defined variables to the created service containers.
For all possible configuration variables check the documentation of each image provided in their corresponding Docker hub page.
Note: All variables will be passed to all services containers. It's not designed to distinguish which variable should go where.
See the specific documentation for using PostgreSQL as a service.
See the specific documentation for using MySQL as a service.
Below is a high level overview of the steps performed by Docker during job time.
mysql
, postgresql
, mongodb
, redis
.config.toml
and
Dockerfile
of build image (ruby:2.1
as in above example)./builds/group-name/project-name/
..gitlab-ci.yml
.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
:
cat <<EOF > build_script
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
make
EOF
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:
docker run -d --name service-mysql mysql:latest
docker run -d --name service-postgres postgres:latest
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:
docker run --name build -i --link=service-mysql:mysql --link=service-postgres:postgres ruby:2.1 /bin/bash < build_script
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:
docker rm -f -v build service-mysql service-postgres
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.
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Опубликовать ( 0 )