Reducing a tiny bit of build time

Introduction

After I got schooled with a tiny bit of knowledge of Docker, the Gitlab registry got my attention. Building this little blog takes around 25 sec. Can I shorten that?

Prerequisite: Gitlab Personal Access Token

I’m using 2FA for Gitlab, and it blocks me from logging in Gitlab from the CLI. And I need PAT (Personal Access Token) to solve this.

Gitlab’s doc is quite well written. You can get your PAT via …

  1. on the sidebar, click your avatar
  2. select Edit Profile
  3. on the left sidebar, select Access Tokens

When you create a PAT, don’t forget to check registry related scopes.

Once you get the PAT, test it on your CLI with the following command.

docker login registry.gitlab.com

Dockerfile

The Dockerfile is pretty simple.

FROM python:slim        # This is for the latest stable python

ENV PYTHONUNBUFFERED=1  # To make python stdout/stderr not buffered

WORKDIR /app            # A target dir in the container

COPY . .                # Copy everything

# Most time consuming part
RUN pip install --no-cache-dir -r requirements.txt

There is no CMD nor ENTRYPOINT. I will execute some command at .gitlab-ci.yml.

Build and Push

Once the Dockerfile is ready, now it’s time to build and push it.

docker build -t registry.gitlab.com/<your_id>/<your_repo> .
docker push registry.gitlab.com/<your_id>/<your_repo>

Gitlab is kind enough to provide the actual command on your repo’s registry page. You can get via Deploy > Container registry. When you don’t have any registry yet, the commands are shown on the main section. When you already have something, then it’s shown when you click CLI commands button on the right top corner.

Update CI script

-image: python:3.12-alpine
+image: registry.gitlab.com/<your_id>/<your_repo>

 pages:
   stage: deploy
   script:
-  - apk add --update --no-cache make subversion
-  - pip install -r requirements.txt
-  - make publish
+  - pelican content -o public -s publishconf.py
   artifacts:
     paths:
     - public/

I’m using python-slim and it doesn’t come with build-essential which contains make command. Thus, I just copied what’s in make publish and paste it hard-coded. It’s not extendable, but it does its job.

Conclusion

I saved around 5 sec. I expected more than that, but it’s already 20% of saving. Also, I got more familiar with the Docker thing. Yay.

By @soundlake in
Tags : #Docker, #Gitlab, #deployment,