{"id":1108,"date":"2023-03-08T10:19:03","date_gmt":"2023-03-08T09:19:03","guid":{"rendered":"http:\/\/192.168.1.213:8088\/?p=1108"},"modified":"2023-10-13T05:40:11","modified_gmt":"2023-10-13T04:40:11","slug":"gitlab-ci-cd-pipeline-to-build-your-own-docker-container-and-upload-to-your-own-registry","status":"publish","type":"post","link":"http:\/\/192.168.1.213:8088\/gitlab-ci-cd-pipeline-to-build-your-own-docker-container-and-upload-to-your-own-registry\/","title":{"rendered":"GitLab CI\/CD pipeline to build your own Docker container and upload to your own registry \ud83d\udce6"},"content":{"rendered":"\t\t
The Docker containers<\/a> are standardized units that can be created on demand to deploy a defined set of applications for a dedicated environment. It will be used in the pipeline where jobs are executed.<\/p> Each job can be executed inside a separate Docker container, which provides an isolated environment for the job to run in. This allows for a more consistent and predictable environment across different projects.<\/p> My personal recommendation to you: There are only a few scenarios where using a Docker container doesn’t make sense. A major advantage is that all dependencies and requirements are defined in the Docker image that can be used in further pipeline jobs.<\/p> If you would like to use the GitLab Docker registry, one requirement is to configure GitLab with SSL and enable the Docker registry. I have been covering this topic in my previous Blog post, which you can check out here:<\/p> GitLab server with a self-signed certificate and embedded Docker registry<\/a><\/p> When it makes sense to use your own Docker images in GitLab pipelines:<\/strong><\/p> What to consider:<\/strong><\/p> In my following example, I am using a GitLab server with 2 GitLab runners (shell and docker executor).\u00a0<\/p> If you want to learn more about GitLab runners and executors, I can recommend checking out the GitLab documentation page:<\/p> https:\/\/docs.gitlab.com\/runner\/register\/<\/a><\/p> \u00a0<\/p> In my pipeline, I will use two different Docker images to execute jobs in two for two different use cases.\u00a0 One Docker container will be dedicated to an Ansible job and the second one is used for a Terraform job.<\/p> The Ansible Docker container is used to execute a playbook against Cisco DNA Center. First, create the following folder structure and files in your GitLab repository (the brackets are indicating if it is a Folder or a File):<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t Copy the following content into\u00a0docker > dnac > Dockerfile<\/strong>:<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t Copy the following content into\u00a0docker > aci > Dockerfile<\/strong>:<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t Validate the status of the pipeline:<\/strong><\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t Check if the Docker containers have been successfully pushed to your GitLab registry:<\/strong><\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t Now that you know that the Build stage works as expected, it would be good to validate, if the binaries (Ansible \/ Terraform) are working as well.\u00a0<\/p> For that, it is needed to create another stage in the pipeline which uses the Docker executor and runs a command within the Docker container.\u00a0<\/p> In the following example, the Ansible and Terraform version commands are executed and the output is saved as an Artifact.\u00a0<\/p> What is an Artifact?! \ud83d\ude33<\/strong><\/p> Check out the following link: https:\/\/docs.gitlab.com\/ee\/ci\/pipelines\/job_artifacts.html<\/a><\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t Validate the pipeline execution and check the dependencies which you configured in the pipeline.<\/p> Again a new pipeline feature… Yes! A very useful one.
<\/strong>You should try to leverage your own Docker environment for most of your GitLab projects.\u00a0<\/p>Requirements<\/h5>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
The Terraform Docker container is used to execute an IaC change against Cisco ACI.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\tDefine the Docker files<\/h5>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t
Create the pipeline with a Build stage<\/h5>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t
Extend the pipeline with an Execution stage<\/h5>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t
Check out the following link for more: https:\/\/docs.gitlab.com\/ee\/ci\/yaml\/index.html#needs<\/a><\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t