Search code examples
gitlab-ciamazon-linux

Prepare Amazon Linux image for GitLab CI


I am running self-hosted GitLab CE with a GitLab-runner and a Docker executor. I want to build a binary for AWS Lambda, so I'm using the amazonlinux:latest image for my Docker executor.

Of course, not all packages that I need for building are available in the base amazonlinux image, so I install them via yum. Unfortunately, cmake is not available for Amazon Linux, so I build it from source.

At the moment, this takes place every time the pipeline runs, which is not optimal because cmake takes a relatively long time to build (compared to the binary I actually want to build).

My general question is: is there a clean and reproducible way to prepare an image for building, which is then used as base image for GitLab CI? Since I'm relatively new to Docker and friends, is the correct way to go to create an image locally on the runner host and use that in my gitlab-ci.yml? Or should I put it in a registry (probably even GitLab's own container registry?)


Solution

  • Yes there is.

    Nothing is stopping you from creating an image through a Dockerfile that does all the yum installs and then pushes the image you build to a (private) Docker registry. Look at it as 'extending' the Amazon image and save it for future usage.

    Since I don't expect it to be to exiting (it will not yet contain any application code) you can also store it for free on Docker Hub.

    Custom image

    So an example Dockerfile:

    FROM amazonlinux:latest
    
    RUN yum install <packages>
    RUN <commands for cmake>
    

    Then you build your custom amazonlinux-custom image with:

    docker build -t mydockerhubuser/amazonlinux-custom:latest .
    

    And push it to Docker Hub (after docker login):

    docker push mydockerhubuser/amazonlinux-custom:latest
    

    Gitlab CI usage

    In your .gitlab-ci.yml you replace the image: amazonlinux:latest part that defines your job image with image: mydockerhubuser/amazonlinux-custom:latest so you don't have to install all your deps.

    Note

    Amazon will often rebuild its amazonlinux:latest image and push it to Docker Hub. Using a custom image based on theirs you will have to take in account the following:

    • You will need to rebuild your image often too, to stay up to date with patches etc.
    • It may be smarter to use e.g. a fixed version like FROM: amazonlinux:2017.09 to avoid major version changes you don't expect.