<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>The Ops Community ⚙️: Madhucheran R</title>
    <description>The latest articles on The Ops Community ⚙️ by Madhucheran R (@madhucheran).</description>
    <link>https://community.ops.io/madhucheran</link>
    <image>
      <url>https://community.ops.io/images/F8EeaswVlqL6Et5TH9oS-SOY2zQVCA0H2GbCqDI6log/rs:fill:90:90/g:sm/mb:500000/ar:1/aHR0cHM6Ly9jb21t/dW5pdHkub3BzLmlv/L3JlbW90ZWltYWdl/cy91cGxvYWRzL3Vz/ZXIvcHJvZmlsZV9p/bWFnZS8zOTAxLzUy/ZTczNDM2LTc4YTIt/NDZkMi1hN2M0LWEw/MDY2NDc3NzIzOC5w/bmc</url>
      <title>The Ops Community ⚙️: Madhucheran R</title>
      <link>https://community.ops.io/madhucheran</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://community.ops.io/feed/madhucheran"/>
    <language>en</language>
    <item>
      <title>Using GitHub Container Registry (GHCR) to Host Your Docker Images</title>
      <dc:creator>Madhucheran R</dc:creator>
      <pubDate>Sun, 07 Jul 2024 08:32:46 +0000</pubDate>
      <link>https://community.ops.io/madhucheran/using-github-container-registry-ghcr-to-host-your-docker-images-3iag</link>
      <guid>https://community.ops.io/madhucheran/using-github-container-registry-ghcr-to-host-your-docker-images-3iag</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Tools Used&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Docker
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Github
&lt;/h3&gt;

&lt;h3&gt;
  
  
  GHCR(GitHub Container Registry)
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Github Action
&lt;/h3&gt;

&lt;h2&gt;
  
  
  Tools outline
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Docker
&lt;/h3&gt;

&lt;p&gt;Docker is a containerization tool. It is widely used in many companies today because it makes it easy to build, test, and deploy applications quickly. It can run in any environment.&lt;/p&gt;

&lt;p&gt;Docker contains the source code of the application you develop and an OS with built-in libraries.&lt;/p&gt;

&lt;p&gt;It acts like a virtual machine, which is why it includes an OS. We can choose the OS images based on our needs.&lt;/p&gt;

&lt;p&gt;In simple terms, it's like we're packing up our entire system, slapping a shipping label on it, and sending it straight to the client's doorstep!&lt;/p&gt;

&lt;h3&gt;
  
  
  GitHub
&lt;/h3&gt;

&lt;p&gt;GitHub is a version control system where we upload and manage our code. It keeps track of changes made to the files.&lt;/p&gt;

&lt;p&gt;For example, if I upload an image file to a repository on GitHub and later decide to replace it with a new version, I can commit the new image file to the repository.&lt;/p&gt;

&lt;p&gt;This action creates a new version of the file while preserving the history of changes. If, at any point, people decide that they prefer the original image over the new one, I can easily revert to the previous version using GitHub's version control features.&lt;/p&gt;

&lt;p&gt;This process ensures that all changes are tracked and reversible, providing a robust way to manage and maintain different versions of files over time.&lt;/p&gt;

&lt;p&gt;Additionally, GitHub allows for collaborative work, so multiple people can contribute to the project, review changes, and suggest improvements, making it an invaluable tool for software development and project management.&lt;/p&gt;

&lt;h3&gt;
  
  
  GHCR
&lt;/h3&gt;

&lt;p&gt;GHCR stands for GITHUB CONTAINER REGISTRY&lt;/p&gt;

&lt;p&gt;GitHub Container Registry (GHCR) is a registry that allows users to host and manage Docker container images in their GitHub account.&lt;/p&gt;

&lt;h3&gt;
  
  
  Github Action
&lt;/h3&gt;

&lt;p&gt;GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;Lets see how i build my projects&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One day, while learning Docker, I had an idea: "Why not create a Dockerfile, run it on GitHub, and host it there?"&lt;/p&gt;

&lt;p&gt;So, I decided to write a Dockerfile that contains the 2048 game. I had already tested the Dockerfile locally, and it ran smoothly. Lets move it to GHCR.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Use a lightweight base image
FROM alpine:3.18

# Install dependencies (Nginx, zip, curl)
RUN apk add --no-cache nginx zip curl &amp;amp;&amp;amp; \
    mkdir -p /var/www/html

# Download and extract 2048 game files
RUN curl -o /var/www/html/master.zip -L https://codeload.github.com/gabrielecirulli/2048/zip/master &amp;amp;&amp;amp; \
    cd /var/www/html/ &amp;amp;&amp;amp; unzip master.zip &amp;amp;&amp;amp; mv 2048-master/* . &amp;amp;&amp;amp; rm -rf 2048-master master.zip

# Copy Nginx configuration file
COPY default.conf /etc/nginx/http.d/default.conf

# Expose port 80 for Nginx
EXPOSE 80

# Start Nginx in foreground
CMD ["nginx", "-g", "daemon off;"]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I created a Docker image and pushed it to GitHub. After that, I wrote a CI/CD pipeline using GitHub Actions to connect to GHCR.&lt;/p&gt;

&lt;p&gt;Because GitHub cannot directly build, run, and host itself, we need a container registry like Docker Hub or alternatives like GHCR.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;GitHub can only host a static site.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By using these tools, we can run this Dockerfile. I decided to choose GHCR because it is a product of GitHub and is also easy to configure.&lt;/p&gt;

&lt;p&gt;So, the GitHub Action will trigger automatically if there is any change in the main branch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: Build, Push, and Deploy Docker Image for 2048

on:
  push:
    branches:
      - main

jobs:
  build_and_publish:
    runs-on: ubuntu-latest

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

      - name: Log in to GitHub Container Registry
        run: echo "${{ secrets.GH_PAT }}" | docker login ghcr.io -u madhucheran --password-stdin

      - name: Build and push the Docker image
        run: |
          docker build . --tag ghcr.io/madhucheran/2048-ghcr:latest
          docker push ghcr.io/madhucheran/2048-ghcr:latest

  deploy:
    needs: build_and_publish
    runs-on: ubuntu-latest

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

      - name: Log in to GitHub Container Registry
        run: echo "${{ secrets.GH_PAT }}" | docker login ghcr.io -u madhucheran --password-stdin

      - name: Pull Docker image
        run: docker pull ghcr.io/madhucheran/2048-ghcr:latest

      - name: Create a container and extract static files
        run: |
          mkdir -p ./public
          docker run --rm -d --name 2048-container ghcr.io/madhucheran/2048-ghcr:latest
          sleep 10 # Wait for the container to start
          docker cp 2048-container:/var/www/html ./public
          docker stop 2048-container
          ls -la ./public # List files to ensure index.html is present

      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GIT_TOKEN }}
          publish_dir: ./public/html  # Adjusted path to the html folder
          publish_branch: gh-pages

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This CI/CD process will build, run, and deploy the files inside the Dockerfile.&lt;/p&gt;

&lt;p&gt;It builds the file in GHCR, runs it in GHCR, and then pulls it back to GitHub. It will automatically create a new branch in GitHub.&lt;/p&gt;

&lt;p&gt;In GitHub, we need to host it, so I used gh-pages for hosting.&lt;/p&gt;




&lt;h3&gt;
  
  
  CI/CD part
&lt;/h3&gt;

&lt;p&gt;Any changes in github main branch the CI/CD part will automatically trigger and the commands&lt;/p&gt;

&lt;p&gt;After the trigger&lt;/p&gt;

&lt;p&gt;It will login to the github container registry&lt;/p&gt;

&lt;p&gt;Then it will build and push the files inside the dockerfile&lt;/p&gt;

&lt;p&gt;Deployment&lt;/p&gt;

&lt;p&gt;Now again it will login to the GHCR&lt;/p&gt;

&lt;p&gt;Now it will pull the files that are all running by the dockerfile by docker&lt;/p&gt;

&lt;p&gt;Copy the files inside the running container to the respected folder&lt;/p&gt;

&lt;p&gt;./public is used to show the github that here is the index.html is stored to host&lt;/p&gt;

&lt;p&gt;Then it will deploy this files form the container to gh-pages&lt;/p&gt;

&lt;p&gt;and if it successfully run and deployed the gh-pages will host itself&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;In my experience i'm saying that "If you are facing lots of error while implementing any of the things while doing anything then you are blessed"&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;This article explains how to build, test, and deploy a Dockerized version of the 2048 game using Docker, GitHub, GHCR (GitHub Container Registry), and GitHub Actions for CI/CD automation. We'll create a Dockerfile, push the Docker image to GHCR, and set up a CI/CD pipeline with GitHub Actions that automatically builds, pushes, and deploys the Docker image whenever changes are made to the main branch. Finally, we'll host the static site with gh-pages, enabling seamless integration and deployment of the 2048 game.&lt;/p&gt;




&lt;p&gt;if there is any error in this blog let me know&lt;/p&gt;

&lt;p&gt;❝𝐋𝐞𝐭𝐬-𝐋𝐞𝐚𝐫𝐧-𝐓𝐨𝐠𝐞𝐭𝐡𝐞𝐫❞&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/madhucheran/"&gt;Linked In&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://x.com/madhucheranr"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.reddit.com/user/madhucheran/"&gt;Reddit&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Baked with 🔥 By Madhucheran&lt;/p&gt;

</description>
      <category>devops</category>
      <category>automation</category>
      <category>github</category>
      <category>docker</category>
    </item>
  </channel>
</rss>
