Introduction

Docker has become an essential tool in modern software development, enabling easy and efficient containerisation of applications. GitHub’s Container Registry provides a convenient way to store and manage Docker images within the GitHub ecosystem.

In this tutorial, we will walk through the process of creating, editing, and publishing a Docker image to GitHub’s Container Registry.

Prerequisites

Before we begin, ensure that you have the following:

  1. A GitHub account
  2. Docker installed on your local machine
  3. Basic familiarity with Docker commands

Step 1: Create a Dockerfile

The first step is to create a Dockerfile, which defines the instructions to build your Docker image. Open a text editor and create a new file called Dockerfile. Here’s a basic example:

# Use a base image
FROM ubuntu:latest

# Set the working directory
WORKDIR /app

# Copy files to the container
COPY . /app

# Install dependencies
RUN apt-get update && apt-get install -y <your-package-name>

# Specify the command to run when the container starts
CMD ["<your-command>"]

Customise the FROM line with the base image you want to use and replace <your-package-name> with the required packages for your application. Also, update <your-command> with the command to start your application.

Step 2: Build the Docker Image

Once you have the Dockerfile ready, you can build the Docker image.

Open a terminal or command prompt and navigate to the directory containing the Dockerfile.

Run the following command:

docker build -t <image-name> .

Replace <image-name> with the desired name for your Docker image.

The . at the end specifies the build context, which includes the files needed for building the image.

Wait for the build process to complete.

Docker will execute the instructions in the Dockerfile and generate the image.

Step 3: Tag the Docker Image

To publish the Docker image to GitHub’s Container Registry, you need to tag it accordingly.

Run the following command:

docker tag <image-name> ghcr.io/<username>/<repository>:<tag>

Replace <username> with your GitHub username, <repository> with the name of the repository you want to push the image to, and <tag> with the desired tag (e.g., version number or latest).

Step 4: Log in to GitHub Container Registry

Before you can publish the image, you need to authenticate with GitHub’s Container Registry.

Run the following command:

docker login ghcr.io -u <username>

Replace <username> with your GitHub username. You will be prompted to enter your GitHub password or a personal access token.

Step 5: Publish the Docker Image

Now that you’re logged in, you can publish the Docker image to GitHub’s Container Registry.

Run the following command:

docker push ghcr.io/<username>/<repository>:<tag>

Replace <username>, <repository>, and <tag> with the values you used in Step 3.

Wait for the push process to complete. Once finished, your Docker image will be available in GitHub’s Container Registry, associated with your specified repository.

Step 6: Update and Re-Publish

If you make changes to your Docker image or need to update it with a new version, follow the steps again from Step 2 onwards.

Remember to tag the image with a new version number or an appropriate tag, and push it to GitHub’s Container Registry.

Related Posts