Day 2 of My Devops Journey: Automating Dockerized Application Deployment with GitHub Actions

arbythecoder

Arbythecoder

Posted on June 19, 2024

Day 2 of My Devops Journey: Automating Dockerized Application Deployment with GitHub Actions

Introduction:

Welcome back to Day 2 of my 90-day DevOps journey! Inspired by my experience in the @SheCodeAfrica mentorship program, today we'll dive into automating the deployment of a Dockerized web application using GitHub Actions. This guide aims to simplify CI/CD processes, making application deployments more efficient and reliable.

What You'll Learn:

  • Building a Dockerized Node.js application.
  • Setting up GitHub Actions for automating Docker image builds and deployments.
  • Overcoming challenges encountered during setup.

Prerequisites:

Before we begin, ensure you have:

  • Basic understanding of Docker and GitHub (refer to my previous articles if needed).
  • A GitHub account for repository hosting.
  • Docker installed on your local machine for testing.

Step-by-Step Guide: Automating Dockerized Application Deployment with GitHub Actions

Step 1: Prepare Your Dockerized Application

  1. Create Your Application:

    • Start by creating a simple Node.js application. Navigate to your terminal and execute:
     mkdir my-docker-app
     cd my-docker-app
    
  2. Initialize Git Repository:

    • Initialize Git for version control:
     git init
    
  3. Write Your Application Code:

    • Create a basic Node.js application. For example, create an app.js file:

    app.js:

     const http = require('http');
    
     const hostname = '0.0.0.0';
     const port = 3000;
    
     const server = http.createServer((req, res) => {
       res.statusCode = 200;
       res.setHeader('Content-Type', 'text/plain');
       res.end('Hello, World!\n');
     });
    
     server.listen(port, hostname, () => {
       console.log(`Server running at http://${hostname}:${port}/`);
     });
    
  4. Create a Dockerfile:

    • Define a Dockerfile in the project root:

    Dockerfile:

     FROM node:14
    
     WORKDIR /app
    
     COPY package*.json ./
     RUN npm install
    
     COPY . .
    
     EXPOSE 3000
    
     CMD ["node", "app.js"]
    
  5. Test Locally:

    • Build and run your Docker container locally:
     docker build -t my-docker-app .
     docker run -p 3000:3000 my-docker-app
    
  • Open http://localhost:3000 in your browser to verify the application works.

Step 2: Set Up GitHub Repository

  1. Create a GitHub Repository:

    • Visit GitHub and create a new repository (my-docker-app).
  2. Push Your Code:

    • Push your local Git repository to GitHub:
     git remote add origin <repository-url>
     git add .
     git commit -m "Initial commit"
     git branch -M main
     git push -u origin main
    

Step 3: Configure GitHub Actions Workflow

  1. Create a Workflow File:

    • Inside .github/workflows, create docker-build-deploy.yml:

    docker-build-deploy.yml:

     name: Docker Build & Deploy
    
     on:
       push:
         branches:
           - main
    
     jobs:
       build-and-deploy:
         runs-on: ubuntu-latest
         steps:
           - name: Checkout Repository
             uses: actions/checkout@v2
    
           - name: Login to Docker Hub
             uses: docker/login-action@v2
             with:
               username: ${{ secrets.DOCKER_USERNAME }}
               password: ${{ secrets.DOCKER_PASSWORD }}
    
           - name: Build and Push Docker Image
             run: |
               docker build -t ${{ secrets.DOCKER_USERNAME }}/my-docker-app:latest .
               docker push ${{ secrets.DOCKER_USERNAME }}/my-docker-app:latest
    
           - name: Deploy Docker Container
             run: |
               docker run -d -p 80:3000 --name my-docker-app ${{ secrets.DOCKER_USERNAME }}/my-docker-app:latest
    
  2. Add GitHub Secrets:

    • Go to your GitHub repository > Settings > Secrets.
    • Add Docker Hub credentials (DOCKER_USERNAME and DOCKER_PASSWORD).

Challenges Faced and Solutions:

  • Port Already Allocated: During testing, encountering a "port already allocated" error due to a previous Docker container using the same port. This was resolved by stopping the previous container (docker stop <container-id>) or specifying a different port during container deployment.

Recommended Resources for Beginners:

Conclusion:

Congratulations on completing Day 2 of our DevOps journey! You've automated Dockerized application deployment with GitHub Actions, improving your CI/CD pipeline skills. Stay tuned for more articles as we explore some more.

See you on Day 3!

💖 💪 🙅 🚩
arbythecoder
Arbythecoder

Posted on June 19, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related