<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>container on Andrew Beaton</title>
    <link>https://andrewbeaton.net/tags/container/</link>
    <description>Recent content in container on Andrew Beaton</description>
    <image>
      <title>Andrew Beaton</title>
      <url>https://andrewbeaton.net/me.jpeg</url>
      <link>https://andrewbeaton.net/me.jpeg</link>
    </image>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <lastBuildDate>Fri, 09 Jun 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://andrewbeaton.net/tags/container/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Create and Publish a Docker Image to GitHub&#39;s Container Registry using GitHub Actions.</title>
      <link>https://andrewbeaton.net/posts/2023/06/docker-github-registry-actions/</link>
      <pubDate>Fri, 09 Jun 2023 00:00:00 +0000</pubDate>
      
      <guid>https://andrewbeaton.net/posts/2023/06/docker-github-registry-actions/</guid>
      <description>A guide on how to publish a Docker image to GitHub&amp;#39;s Container Registry using GitHub Actions.</description>
      <content:encoded><![CDATA[<h2 id="introduction">Introduction</h2>
<p>In this tutorial, I will walk you through the process of publishing a Docker image to GitHub&rsquo;s Container Registry using GitHub Actions. By using GitHub Actions, you can automate the build and publish process, making it easier to distribute your Docker images.</p>
<h2 id="prerequisites">Prerequisites</h2>
<p>Before getting started, make sure you have the following:</p>
<ol>
<li>A GitHub account.</li>
<li>A Dockerfile that describes your application&rsquo;s dependencies and build instructions.</li>
<li>A GitHub repository where you want to publish your Docker image.</li>
</ol>
<h2 id="step-1-set-up-github-actions-workflow">Step 1: Set up GitHub Actions workflow</h2>
<p>GitHub Actions allow you to automate tasks by creating workflows.</p>
<p>To publish a Docker image, we&rsquo;ll create a workflow file named <code>publish.yml</code>.</p>
<p>Create a new directory called <code>.github/workflows</code> in the root of your repository, if it doesn&rsquo;t already exist.</p>
<p>Inside this directory, create a new file named <code>publish.yml</code> and add the following content:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">name</span>: <span style="color:#ae81ff">Publish Docker Image</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">on</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">push</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">branches</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">main</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">jobs</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">build-and-publish</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">runs-on</span>: <span style="color:#ae81ff">ubuntu-latest</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">steps</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#f92672">name</span>: <span style="color:#ae81ff">Checkout code</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">uses</span>: <span style="color:#ae81ff">actions/checkout@v2</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>      - <span style="color:#f92672">name</span>: <span style="color:#ae81ff">Login to GitHub Container Registry</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">uses</span>: <span style="color:#ae81ff">docker/login-action@v1</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">with</span>:
</span></span><span style="display:flex;"><span>          <span style="color:#f92672">registry</span>: <span style="color:#ae81ff">ghcr.io</span>
</span></span><span style="display:flex;"><span>          <span style="color:#f92672">username</span>: <span style="color:#ae81ff">${{ github.actor }}</span>
</span></span><span style="display:flex;"><span>          <span style="color:#f92672">password</span>: <span style="color:#ae81ff">${{ secrets.GITHUB_TOKEN }}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>      - <span style="color:#f92672">name</span>: <span style="color:#ae81ff">Build and push Docker image</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">uses</span>: <span style="color:#ae81ff">docker/build-push-action@v2</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">with</span>:
</span></span><span style="display:flex;"><span>          <span style="color:#f92672">context</span>: <span style="color:#ae81ff">.</span>
</span></span><span style="display:flex;"><span>          <span style="color:#f92672">push</span>: <span style="color:#66d9ef">true</span>
</span></span><span style="display:flex;"><span>          <span style="color:#f92672">tags</span>: <span style="color:#ae81ff">ghcr.io/${{ github.repository_owner }}/${{ github.repository }}:${{ github.sha }}</span>
</span></span></code></pre></div><p>This workflow will trigger whenever you push changes to the <code>main</code> branch. It checks out the code, logs in to GitHub&rsquo;s Container Registry using your GitHub credentials, and builds and pushes the Docker image to the registry.</p>
<h2 id="step-2-configure-the-dockerfile">Step 2: Configure the Dockerfile</h2>
<p>In your repository, create a file named <code>Dockerfile</code> and add the necessary instructions to build your Docker image. Here&rsquo;s an example <code>Dockerfile</code> for a Node.js application:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Dockerfile" data-lang="Dockerfile"><span style="display:flex;"><span><span style="color:#66d9ef">FROM</span><span style="color:#e6db74"> node:14</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">WORKDIR</span><span style="color:#e6db74"> /app</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">COPY</span> package.json package-lock.json ./<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">RUN</span> npm ci --only<span style="color:#f92672">=</span>production<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">COPY</span> app.js .<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">CMD</span> [<span style="color:#e6db74">&#34;node&#34;</span>, <span style="color:#e6db74">&#34;app.js&#34;</span>]<span style="color:#960050;background-color:#1e0010">
</span></span></span></code></pre></div><p>Make sure to modify the <code>Dockerfile</code> to suit your application&rsquo;s requirements.</p>
<h2 id="step-3-push-changes-to-your-repository">Step 3: Push changes to your repository</h2>
<p>Commit and push the <code>Dockerfile</code>, <code>publish.yml</code>, and any other files you may have modified to your repository.</p>
<h2 id="step-4-monitor-the-workflow-execution">Step 4: Monitor the workflow execution</h2>
<p>After pushing the changes, navigate to the &ldquo;Actions&rdquo; tab in your GitHub repository. You should see the workflow you created (&ldquo;Publish Docker Image&rdquo;) listed there. Click on it to monitor the execution.</p>
<h2 id="step-5-verify-the-docker-image-in-github-container-registry">Step 5: Verify the Docker image in GitHub Container Registry</h2>
<p>Once the workflow execution is complete, go to the &ldquo;Packages&rdquo; tab in your GitHub repository. You should see your Docker image listed there. Click on it to view the details.</p>
<h2 id="summary">Summary</h2>
<p>You have successfully published a Docker image to GitHub&rsquo;s Container Registry using GitHub Actions. By automating this process, you can easily distribute your Docker images and ensure consistent deployment across different environments. GitHub Actions provides a powerful platform for automating various tasks in your development workflow, saving you time and effort.</p>
]]></content:encoded>
    </item>
    
  </channel>
</rss>
