Thank you for the comment as I wanted to be sure, how you do it. Example would help too, but I'll be generic for now (using docker
).
To run multiple containers you need a
(The Docker executor
)
To quote the documentation on it:
The Docker executor when used with GitLab CI, connects to Docker
Engine and runs each build in a separate and isolated container using
the predefined image that is set up in .gitlab-ci.yml
and in
accordance in config.toml
.
Workflow
The Docker executor divides the job into multiple steps:
- Prepare: Create and start the services.
- Pre-job: Clone, restore cache and download artifacts from previous stages. This is run on a special Docker image.
- Job: User build. This is run on the user-provided Docker image.
- Post-job: Create cache, upload artifacts to GitLab. This is run on a special Docker Image.
Your config.toml
could look like this:
[runners.docker]
image = "rocker/verse:4.0.0"
builds_dir = /home/builds/rocker
[[runners.docker.services]]
name = "aergus/latex"
alias = "latex"
From above linked documentation:
The image
keyword
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. 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.
If you don’t specify the namespace
, Docker implies library which includes all official images. That’s why you’ll see many times the library part omitted in .gitlab-ci.yml and config.toml. For example you can define an image like image: ruby:2.6
, which is a shortcut for image: library/ruby:2.6
.
Then, for each Docker image there are tags, denoting the version of the image. These are defined with a colon (:) after the image name. For example, for Ruby you can see the supported tags at docker hub. If you don’t specify a tag (like image: ruby
), latest is implied.
The image
you choose to run your build in via image
directive must have a working shell in its operating system PATH
. Supported shells are sh
, bash
, and pwsh
(since 13.9) for Linux, and PowerShell for Windows. GitLab Runner cannot execute a command using the underlying OS system calls (such as exec).
The services
keyword
The services
keyword defines just another Docker image that is run during 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.
The service
image can run any application, but the most common use case is to run a database container, e.g., 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 can see some widely used services examples in the relevant documentation of CI services examples.
If needed, you can assign an alias
to each service.
As for your questions:
It should be possible to use artifacts to pass data between jobs
according to this answer and to this well explained forum post but
they use only one container for different jobs. It doesn't work in my
case. Probably because I use two different containers?
The builds and cache storage (from documentation)
The Docker executor by default stores all builds in /builds/<namespace>/<project-name>
and all caches in /cache
(inside the container). You can overwrite the /builds
and /cache
directories by defining the builds_dir
and cache_dir
options under the [[runners]]
section in config.toml
. This will modify where the data are stored inside the container.
If you modify the /cache
storage path, you also need to make sure to mark this directory as persistent by defining it in volumes = ["/my/cache/"]
under the [runners.docker]
section in config.toml
.
builds_dir
-> Absolute path to a directory where builds are stored in the context of the selected executor. For example, locally, Docker, or SSH.
The [[runners]] section documentation
As you may have noticed I have customized the build_dir
in your toml
file to /home/builds/rocker
, please adjust it to your own path.
How can I pass the artifacts from one job to the other?
You can use the build_dir
directive. Second option would to use Job Artifacts API.
Should I use cache as explained in docs.gitlab.com / caching?
Yes, You should use cache
to store project dependencies. The advantage is that you fetch the dependencies only once from internet and then subsequent runs are much faster as they can skip this step. Artifacts
are used to share results between build stages.
I hope it is now clearer and I have pointed you into right direction.