After the rise of cloud, and rise of microservices approaches, modular, plug-and-play and containerized applications are getting more attention.

So, literally hundreds of thousands software engineers are working on projects that built with multiple containerized pieces, all around the world.

If you're using GitHub and wanted a place to store and serve Docker Images for your project, you can easily go and use GitHub Packages, it's free (*) and super easy to use 🥳

You can have package registries for your;

  • Docker Images
  • npm packages
  • Maven packages
  • Gradle packages
  • NuGet packages
  • Ruby Gems

(*): Actually, it's not free (🤦‍♂️) if you want to store more than 500MBs

Plan Storage
Free 500 MB
Pro 2 GB
Team 2 GB
Enterprise 50 GB
Additional Storage $0.25 per GB

In this post you can find a guideline to publish, serve and pull a Docker Image to GitHub Packages.

Creating the repo and Dockerfile

Let's create a repo on GitHub first;

GitHub create repository screenshot

After creating the repo, clone it and open it with your favorite editor (mine is Visual Studio Code 😎)

git clone https://github.com/polatengin/copenhagen.git

code copenhagen/

Now we need to create a Dockerfile and build the image we need in the project. (Example Dockerfile can be found on Copenhagen repo, Dockerfile)

FROM ubuntu:latest

LABEL org.opencontainers.image.source=https://github.com/polatengin/copenhagen

Logging in to GitHub Container Registry (a.k.a. GitHub Packages)

Navigate to Personal Access Tokens under GitHub Account Settings, and create a new one, just like below;

GitHub create personal access token screenshot

* Don't forget to set write:packages permission

It'll display the PAT, copy it to the clipboard.

Run the following command to login via docker cli;

echo ${GITHUB_PAT} | docker login ghcr.io -u ${GITHUB_USERNAME} --password-stdin

* Don't forget to set your username and PAT to the GITHUB_USERNAME and GITHUB_PAT variables

Building and Pushing Docker Image to GitHub Container Registry (a.k.a. GitHub Packages)

It's time to build, tag and push the Docker Image to the GitHub Container Registry (a.k.a. GitHub Packages);

docker build -t copenhagen .

docker tag copenhagen ghcr.io/polatengin/copenhagen:latest

docker push ghcr.io/polatengin/copenhagen:latest

After the Docker Image has been pushed, we can consume it in our projects;

We can see the packages in out GitHub Account, by navigating to the Packages tab (like, https://github.com/polatengin?tab=packages)

GitHub Packages screenshot

Using images from GitHub Container Registry (a.k.a. GitHub Packages)

After login to GitHub Container Registry, we can easily pull and consume images;

echo ${GITHUB_PAT} | docker login ghcr.io -u ${GITHUB_USERNAME} --password-stdin

docker run -d -it ghcr.io/polatengin/copenhagen:latest

* Don't forget to set your username and PAT to the GITHUB_USERNAME and GITHUB_PAT variables

So, it's possible (and relatively easy) to use GitHub Packages, within your CI/CD orchestration platform (such as GitHub Actions), or Kubernetes platform (such as, Azure Kubernetes Service (AKS))

References