Build Docker images with GitLab CI on AWS ECR

GitLab CI is a great way to automate testing, building and deploying of almost any application. Recently we came across the requirement to setup a repository with several definitions of Docker container images. For sure we wanted to use GitLab CI to also automate the building of our images and to push them to our container registry. Although you can setup a private container registry on GitLab too, our project required us to use the AWS Elastic Container Registry. Given that fact, it will need to do some additional setup to install the AWS CLI and to login to the ECR service. All of that will be explained step by step.

Gitlab runner

First of all you should setup a dedicated GitLab runner on e.g. DigitalOcean, because you need to update the configuration of your runner. To do the setup you can follow the blog article How to set up GitLab Runner on DigitalOcean.

After installing the GitLab runner, head over to the config.toml and enable the privileged mode with privileged = true.

After that, disable shared runners for your GitLab repository and enable your own private runner instance. Now you are ready to setup your continuous integration configuration.

Linting Dockerfile

As an additional step to ensure that all Dockerfiles are syntactically correct, we decided to use dockerlint. This will test all Dockerfiles within the GitLab repository and prohibit any unexpected building errors due to syntax errors. This step is completely optional but we would suggest to consider it for your projects too.

The next section will provide you the complete GitLab CI configuration to build your images.

CI configuration

So in this step we will setup our GitLab CI configuration to enable it to build Docker images and push it to the AWS ECR. In short, our script will do the following:

  1. Use a basic Docker image
  2. Use Docker in Docker (DinD) as a service
  3. Install AWS CLI
  4. Login to AWS ECR
  5. Build and push our images

The script also installs some necessary packages (node, python) to get the AWS CLI up and running but I will not go into further details. As another requirement you will need to setup some environment variables within the GitLab CI settings page. For a successful authentication to AWS ECR set the following variables:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_REGION

Now checkout our complete configuration file at GitHub Gist. It should provide you with all necessary details to setup your build automation. Please notice, that the actual step of building your Docker images is up to you. We used a simple bash script to build all images within subfolders of our GitLab repository.

We hope you got some insights on how to easily automate building Docker images on GitLab. Feel free to further improve our CI configuration with additional steps or leave a comment for some additional topics.

Leave a Reply

Your email address will not be published. Required fields are marked *