loading...

Automagically build and push Docker images to a registry using Gitlab.

imichael profile image ✨iMichael✨ Updated on ・1 min read

This is an example configuration for how to automatically build Docker images and push to your own Docker registry on Gitlab (free). In this example, I'm building and testing a Docker image that runs Django, but the same basic idea will work for any app.

image: docker:latest

services:
  - docker:dind

stages:
- build
- test
- push

variables:
  DJANGO_TAG_NAME: registry.gitlab.com/<username>/<repo>/<image>:$CI_COMMIT_REF_NAME
  DJANGO_SETTINGS_MODULE: path.to.your.settings
  DATABASE_URL: postgres://postgres:xxxxxxx@postgres/postgres

before_script:
  - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.gitlab.com

build:django:
  artifacts:
    paths:
      - docker-images
  stage: build
  script:
    - mkdir docker-images
    - docker build --pull -t $DJANGO_TAG_NAME -f path/to/your/Dockerfile .
    - docker save $DJANGO_TAG_NAME > docker-images/app.tar
  only:
    - master
test:django:
  stage: test
  script:
    - docker load -i docker-images/app.tar
    - docker run --name test-postgres -e POSTGRES_PASSWORD=xxxxxxx -d postgres
    - docker run --rm --link test-postgres:postgres -v $(pwd):/<DOCKER WORKDIR> -e DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE -e DATABASE_URL=$DATABASE_URL $DJANGO_TAG_NAME python manage.py test -v 2

push:django:
  stage: push
  script:
    - docker load -i docker-images/app.tar
    - docker push $DJANGO_TAG_NAME
  only:
    - master

Posted on Aug 5 '18 by:

imichael profile

✨iMichael✨

@imichael

Founder/CTO & Lead Software Engineer

Discussion

markdown guide