Fundamentals of Docker Executor in Gitlab | DevOps Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to Korean boots so in today's video we're going to discuss about gitlab Docker executor okay so in the last video uh we have discussed about gitlab runners okay and with uh we have a demonstrated a pipeline which runs on the shell okay so in this video we're going to execute I mean discuss about how we can use leverage Dockers uh capability in gitlab okay so let's get back to some of the fundamentals so in gitlab we have runners okay in the gitlab CI pipelines we have we need Runners to execute the pipelines okay so what will happen so the git lab okay will basically execute the steps the stages in the CI pipeline in the runner node okay so if you are using shell executor shell execute her you know like I mean by the word itself it is it has a meaning like what the commands are basically the stages the the steps that we defined inside the stages are going to directly execute on the shell of that machine okay the runner machine so it could be like a Linux machine so that mean or Windows machine so what will happen is the shell the commands will directly get executed on the runner machine so uh this has problems because if you are using shared Runners so what will happen is let's say if someone as part of a pipeline makes something and if they are not not doing the cleanup properly okay what will happen the the changes or the package installations or whatever modifications that they are doing as part of the stage will permanently have an impact in the machine okay so in this cases uh and again another thing is that is problem number one problem number two is let's say like I mean if your step or your stage needs some other packages okay or is series of installations okay so if you want to do it again again on the shell so what will happen is you are you have to basically do complex installations on the runner machine to make it capable of executing that particular stage but with the capability of Docker we can have pre-build images with all that capability all the installations everything and we can just execute the required the necessary step on top of the existing Docker image so we can speed up the pipeline execution using uh that is the second advantage using the docker executor so let's get back to uh the gitlab okay and let's start into the action so uh I'm going to add a runner okay so the runner so in the settings in the project settings go to cacd so runners okay I'm going to host a runner here so let's click on this it will provide the runner installations so what I'm going to do is I'm going to basically copy all these steps I have my Ubuntu machine this is the machine where I'm going to install the runner so I copied and pasted the commands to execute so now let's see the status of Runner so it will show us the status of the gitlab runner it is running now now it is not at registered okay so what we need is we need to pass the registration token okay so make this up and running and get registered with our git lab so the registration Docker is available here we just need to copy it and then paste it so okay so then it'll ask a set of questions the base URL so I'm using the the online version of gitlab so base URL is going to be gitlab.com registration token I have already given description of the runner so it's like I mean this is an easy to instance so it's giving the hostname of that easy to instance by default let it be okay tax I am putting self hosted you can use any tag so this is basically required to select the runner okay this is something that we use in the pipeline optional maintenance not I am keeping empty okay then it is asking for the executor so I am going to use docker okay so I'm going to choose docker so default Docker image it is asking I am going to choose Ubuntu okay so Ubuntu colon latest so this is the docker image if I am not specifying in a Docker image by default what will happen is it will use this Ubuntu as the default image you can use you can select your own uh or I mean if you have any custom images or if you have any images of your choice you can use that so Ubuntu is comparatively like I mean larger in size if you want like lightweight uh Docker images you can use something like Alpine okay so in this example I am going to use Ubuntu okay now the configurations are ready now let's see so whether this Runner gets registered got registered or not so let me refresh this page okay so expand the runners now we can see there is a runner which is a hostname and self-hosted so now let's go to there is one step missing but let's see like I mean I'll really complete that stuff during the execution of this pipeline so this is a sample gitlab pipeline okay and I wanted to so one more thing let's go back to the runner again okay so we will we will make that change while doing the demo so let's edit this yaml file okay so this is something that gets Auto generated okay I'm going to remove or comment The Unwanted I'm not going to show everything I'm just going to keep uh just one stage to demonstrate this pipeline okay so I have I'm going to keep only the build stage test and deploy I'll keep it commanded and I'm going to comment this tags also Okay so then it is just doing compiling it is just doing an echo compiling the code compile complete so here what we need is uh so let's try to execute this so what it has is it is defining just a build stage build okay it has only one stage and we have the definition for the build stage okay the job okay the job that corresponds to the build stage let's execute this one commit this change okay the configuration is valid now it should ideally trigger a pipeline so let's see what happens so here if you see it is pending okay pending means it has not started the execution it is it is still showing pending let's see why it is pending so the job is stuck because the project does not have any runners online assigned to it okay so what is the reason because it does not we have not specified any tag to pick up the runner okay and there is no open Runner available to this project okay to schedule this job so what we have to do is we have to go to settings cicd settings okay and let's go to Runners where we have configured and there is an edit option here okay click on the edit option in the runner so there in this uh there are a set of properties okay so in this you will see one is active Okay then pose Runners don't accept uh post runners okay if you are basically like I mean disabling this one what will happen it won't accept any new jobs protector protected okay if you are checking this phone this will accept only lead by planes run on pipelines triggered by protected branches okay so it will be we are basically restricting into specific branches okay if you click on protected it will restrict you to protected branches then third is run untagged job okay if I select this what will happen is it will pick up any job that is part of this project okay so this Runner will pick that is what we need right now because I have not tagged this Runner okay I have not tagged the runner in the pipeline so because of that what it is waiting for an active Runner to be available for the pipeline to run so if I check this option what will happen this wheel this Runner will be available for the pipeline to run so let's check this and save it okay and let's go to cicd pipelines and see what happens now now if you see it picked up okay the the postage job which was not having any resource run it picked up okay started the execution but you see it's showing errors okay cannot connect a Docker Daemon Unix this one cannot connector basically we have configured the docker owner here okay but we don't have a Docker in this machine Docker PS if you see there is no Docker in this machine we need to install Docker okay so for that what I'm going to do is I'm going to do a simple installation of Docker snap install Docker this is a Rubidoux machine I am going to install Docker in this machine okay snap install Docker if you do it will quickly installed this is Ubuntu 22.04 LTS so let's see whether Docker gets installed or not okay Docker is ready now let's say type Docker PS okay it will basically list all the running containers in the machine if you do if you type Docker PS so okay there are no running active containers in the machine okay but Docker is up now let's rerun the job so on the top right there is a retry option let's try okay so this time it should run so this was the pending step that we discussed okay so you'll have you'll need if you are configuring Docker executor then you will need a Docker service running in the machine now you see it is downloading a Docker basically an Ubuntu image okay and it is executing the steps on top of the Ubuntu image you see compiling code okay complete compile complete okay basically it executed the complete pipeline okay so what happened is on top of this machine if you see Docker PS now the job is complete but let's re-run it again okay let's do Docker PS okay so it will see now you see it dynamically created a Docker container okay this is the container where this Runner is executing okay and as soon as the job completes the container will die okay so now if you check Docker AP images you will see here I mean it has pulled it has pulled some images as part of this setup okay so it pulled the Ubuntu image okay and there is one more rocker gitlab Runner helper also these two images it pulled to run this particular pipeline okay now let's go back to the pipeline again so now what happens happened as part of the docker uh so if you see we have not specified any image here okay but it used the default image so as part of configuring the runner as part of configuring the docker executor have configured a default image that's why it picked up that Ubuntu image itself from the default configuration and did the installation now let me let me configure a different image okay I am configuring Alpine image for this particular job okay so in this case I have in the job definition I have configured I have specified image column a specific image now let's see what happens in this case okay so validating it left CI it is fine the pipeline is valid okay so now it's running let's see the logs so job got completed quickly so this job got completed pretty quick compared to the other one now this time you see instead of Ubuntu image it pulled the Alpine image okay so this time I have specified an image name in the job so because of that it picked up it used that image instead of the default image and if you see Docker images okay you can see the one the size of the Ubuntu image was around 77.8 MB okay and the size of this Alpine image is 7 MB okay so Alpine image that's why the reason the pipeline execution got completed very quickly in case of Alpine it completed everything in nine seconds okay so if you see the other one we see the previous execution it took more time okay it took almost like I mean 16 seconds basically uh like almost 16 seconds to complete the job okay so uh that's why like you have to choose the image wisely so if you are choosing something you choose only the required image okay don't pick up something heavy okay so use the right one so that you will be able to complete the job efficiently okay so this is uh I think I mean with this I have explained how to use Docker how to configure a runner with Docker executor and how to run a pipeline gitlab CI Pipeline with uh on top of like a minute docker with Custom Image okay so in this script you can actually like you do whatever steps you want you can have complex complex steps complex complex pipelines in this one defined on top of this the advantage is if you see in this random machine we are not going to make any changes directly on this render machine okay the runner machine will get untouched the only changes as part of the steps or the stages will happen on top of the images Docker images and after the job what will happen the images will get basically the containers will get destroyed so the runner machine will be always clean okay so this is an efficient approach compared to the Shell executor approach so I hope this video is useful okay so I will be uploading more interesting videos more interesting videos about gitlab and other Technologies in this channel so subscribe to this channel if you have any questions please comment below this video I'll definitely respond back to you so thank you thank you very much for your time
Info
Channel: Coder In Boots
Views: 2,761
Rating: undefined out of 5
Keywords: how to configure docker executor in gitlab, how to run gitlab pipelines on docker, devops on docker, gitlab devops, gitlab devops tutorial, coderinboots
Id: xVL5Io98ORs
Channel Id: undefined
Length: 15min 2sec (902 seconds)
Published: Thu Feb 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.