Skip to content
Bonus Episode: Building and deploying a Docker container to Github Packages

Episode

Bonus Episode: Building and deploying a Docker container to Github Packages

Teaching 40 min
Estimated 40 min

Questions

  • How to build a Docker container for Python packages?
  • How to share Docker images?

Objectives

  • To be able to build a Docker container and share it via GitHub Packages
Prerequisites
For this lesson, you should already be familiar with Docker images. Head over to our training on Docker if you aren’t already!

Docker Container for Python Packages

Python packages can be installed using a Docker image. The following example illustrates how to write a Dockerfile for building an image containing Python Packages.

FROM ubuntu:20.04

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update \
  && apt-get install wget -y \
  && apt-get install dpkg-dev cmake g++ gcc binutils libx11-dev libxpm-dev \
    libxft-dev libxext-dev python3 libssl-dev libgsl0-dev libtiff-dev \
    python3-pip -y

RUN pip3 install numpy awkward uproot4 particle hepunits matplotlib \
  mplhep vector fastjet iminuit

As we see, several packages are installed.

Publish Docker images with GitHub/Gitea Packages and share them!

It is possible to publish Docker images with GitHub Packages or with Gitea’s built-in container registry. To do so, one needs to use GitHub/Gitea CI/CD. A step-by-step guide is presented here.

  • Step 1: Create a GitHub/Gitea repository and clone it locally.
  • Step 2: In the empty repository, make a folder called .github/workflows. In this folder, we will store the file containing the YAML script for a workflow, named Docker-build-deploy.yml (the name doesn’t really matter).
  • Step 3: In the top directory of your repository, create a file named Dockerfile.
  • Step 4: Copy-paste the content above and add it to the Dockerfile. (In principle, it is possible to build this image locally, but we will not do that here, as we wish to build it with CI/CD).
  • Step 5: In the Docker-build-deploy.yml file, add the content below.
  • Step 6: Add LICENSE and README as recommended in the SW Carpentry Git-Novice Lesson, and then the repository is good to go.
name: Create and publish a Docker image

on:
  pull_request:
  push:
    branches: main

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build-and-push-image:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Log in to the Container registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Docker Metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

Note that on Gitea, the automatic secrets.GITHUB_TOKEN cannot write to the package registry, so generate a personal access token with read and write permissions on package (Settings → Applications) and add it to the repository as the secret REGISTRY_TOKEN.

Key Points

  • Python packages can be installed in Docker images along with Ubuntu packages.
  • It is possible to publish and share Docker images over GitHub Packages.