how to set the image volume in gitlab-runner with docker executor

Okay, so I've researched all over for this and can't seem to find a definitive answer.

I want to know how gitlab-runner docker executor sets the volumes for the image. I see from the second URL I posted you can put the directory on the host to be used by the image, the same way you would with the -v option in docker run, however it is an absolute path which can't work if running parallel jobs since the directory will change. However, I can't find if I can use $PWD for the build directory, or if that will work since the config.toml file is in a different location. The documentation from what I read does not specify how that would work, or if it could work. I'm trying to find some answers before I burn hours during the next sprint trying to get something to work that may or may not work. So hoping for some insight.

So my question is: has any one ran into this issue before and if so, how did you set it up so the build directory is the directory being passed to the docker image? The same way you would if running docker build with the '-v' option or docker compose using 'volume:'.

The reason I want to run the images in this fashion and not the shell executor, which is what I'm doing currently. Is because if you cancel a build, you have to go in and manually kill the images on the runner's server. As you may imagine this can be annoying and time consuming if there are multiple builds in progress.

I also wish to avoid creating a custom docker image, because I do not want to put the git repository in the docker image. I only want to make additional docker images, that are reusable across CI/CD repositories so I don't want to add that as a command in a shell script that will be copied into the image and hence make the image none reusable.

If the above is not possible, is there a way to 'export' the directory as a variable to the docker image? I don't think there is since docker has to pass the volume to the image at startup.

I also don't want to have to build the docker image every time I run a test in CI/CD since that can be time consuming and expensive. I'd rather pass the volume and run the commands needed to do the test.

The command if running in shell script: $ docker run -d -p 80:80 --name my-apache-php-app -v "$PWD":/var/www/html php:7.0-apache

Hopefully, this is the right forum for this question, wasn't sure if this fall under stackoverflow or serverfault.

After further research, it looks as if the runner actually runs the git commands in the docker container that is listed as the image and links the services to it. However, super confusing because they don't really spell this step out, so I still don't know if it is running the git commands in the build directory for the host or the container, if it's the container then I need to update the docker file.

references:

https://docs.gitlab.com/runner/executors/docker.html

https://docs.gitlab.com/runner/configuration/advanced-configuration.html#volumes-in-the-runners-docker-section

1
задан 20 December 2017 в 18:39
1 ответ

Ответ таков ... он клонирует каталог сборки в образе, создавая каталог и затем клонируя его, что очень похоже на вызов docker exec. Итак, когда вы запускаете команды, вы запускаете их в контейнере сборки, этот образ контейнера устанавливается с помощью ключа 'image:'. Итак, чтобы получить скрипты из хост-системы в контейнер сборки, вам нужно будет установить том с хоста на контейнер сборки. Итак, я поместил дополнительный каталог в свой образ, я назвал его tools_for_ci, затем я монтирую каталог со всеми этими сценариями к образу. Это прекрасно работает. Теперь возникает проблема, что образ докера не может общаться с сервисами взад и вперед. Это известная помеха с исполнителем докеров, без улучшения сайта, все проблемы, которые я видел, отмечены как невыполненные. Так что, к сожалению, не похоже, что исправление для этого будет реализовано в ближайшее время.

Короче говоря, образ получает все файлы, которые ему нужны, из репозитория во время выполнения, что означает, что вам не нужно устанавливать том , если вы не переносите скрипты с хоста на изображение. Все команды скрипта также выполняются в образе.

Однако докер в докере, когда он привязан к сокету докера, действительно сохраняет изображения, поэтому вам не нужно каждый раз повторно загружать его. Так что я могу попытаться обойти проблему с обменом данными между изображением и службами.

0
ответ дан 4 December 2019 в 04:18

Теги

Похожие вопросы