DEV Community

✨iMichael✨
✨iMichael✨

Posted on • Edited on

6 3

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

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

Enter fullscreen mode Exit fullscreen mode

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay