attheoaks.com

How to Effectively Utilize the GitHub Docker Registry and Import Images via Portainer

Written on

Chapter 1: Introduction to Docker Registries

Managing and distributing Docker images is vital for modern development processes. A Docker registry serves a key function, enabling you to upload various versions of your images to the cloud for convenient access and sharing. This guide will walk you through the steps to configure the GitHub Container Registry and subsequently deploy and execute an image using Portainer.

Let's dive in!

To start, we need to establish authentication for the GitHub Container Registry.

Section 1.1: Generating Your Authentication Token

Simple Method

The straightforward method involves clicking on this link and selecting "Generate token."

Detailed Approach

For those who prefer a more manual route, here's how to navigate to the link above while addressing potential pitfalls. Log into GitHub and click on your profile icon in the upper-right corner:

Next, select Settings:

On the left sidebar, scroll down and click on Developer Settings:

Then, navigate to Personal access tokens > Tokens (classic)

Now, choose Generate new token > Generate new token (classic)

To allow registry access, ensure you enable the "write:packages" scope. Note that enabling this also grants repository access, unlike the simpler method (the link) which limits the scope to what’s necessary.

Retrieving the Token

After clicking "Generate token," you'll receive your token. Remember to keep this token secure and do not disclose it to anyone:

(For security reasons, part of the token is concealed in the image below, but you should keep your token completely private.)

Section 1.2: Uploading Images to the Repository

The next step involves uploading a Docker image to your repository. I prefer using Docker Compose for this purpose. Here’s an outline of the necessary code:

First, create your docker-compose.yml file, which should resemble the following:

version: '3'

services:

test-image:

container_name: test-image

build:

context: build

dockerfile: Dockerfile

image: ghcr.io/ferusandbeyond/test-image:latest

environment:

PYTHONUNBUFFERED: 1

It's important to highlight that the key part of this upload process is the line:

image: ghcr.io/ferusandbeyond/test-image:latest

This points to the GitHub Container Registry (ghcr.io) under my account (ferusandbeyond) with the desired image name, test-image. The environment variables are only utilized when running the image locally, as managed by up.sh (to start) and down.sh (to stop the container). The Dockerfile initiates a simple Python program:

FROM python:3.11-alpine

WORKDIR /app

COPY src .

CMD ["python", "main.py"]

The Python script (main.py) outputs "Hello World!" every five seconds:

import time

if __name__ == '__main__':

while True:

print('Hello World!')

time.sleep(5)

To execute it locally, you would run:

docker-compose -f docker-compose.yml up --build

And to stop it:

docker-compose -f docker-compose.yml down

Before pushing your image to the GitHub Container Registry, you must log in using your token. First, execute the following in your terminal (bash):

export CR_PAT=YOUR_TOKEN_HERE

Replace YOUR_TOKEN_HERE with the token you generated earlier. Then, authenticate using this command:

echo $CR_PAT | docker login ghcr.io -u ferusandbeyond --password-stdin

Ensure you substitute ferusandbeyond with your GitHub username.

To deploy your image, use the following command (this is what you would place in deploy.sh):

docker-compose -f docker-compose.yml build --push

Once the process completes, you should find your image listed under packages on your GitHub profile.

Chapter 2: Importing Images into Portainer

If you're utilizing Portainer, importing images and launching containers from the GitHub Container Registry is straightforward. The specifics may differ slightly depending on your Portainer version. Begin by clicking on Registries in the sidebar:

Then, click Add Registry on the right:

At this point, you'll have a range of registry providers to select from. If your version of Portainer includes GitHub as an option, choose that; otherwise, select Custom Registry.

Complete the required fields, noting that the password should be your token!

Afterward, you can easily choose the image when setting up a new container:

I also included the environment variable PYTHONUNBUFFERED: 1 to ensure print statements appear.

The "Hello World!" message should then be visible in the container logs:

That concludes our guide! Thank you for reading.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Embracing Life Lessons: Wisdom from a Dying Professor

Explore profound insights from a dying professor on life and love, inspired by the touching story in

Exploring the Future of Tattoo Art: Innovations and Trends

Dive into the evolving world of tattoos, exploring innovations, safety concerns, and the impact of technology on this age-old art form.

Samsung M8: A Better Alternative to Apple’s Studio Display?

Exploring the shortcomings of Apple’s Studio Display and how Samsung’s M8 monitor may be a more appealing option for casual users.