🚀 Practical DevOps Tutorial: 01

Deploy a Node.js App with Docker & GitHub Actions CI/CD

Welcome to this step-by-step DevOps tutorial where we’ll build, Dockerize, and deploy a Node.js app with GitHub Actions CI/CD pipeline. Perfect for beginners who want hands-on practice with DevOps tools!

🌟 What You’ll Learn

  • ✅ Create a simple Node.js app
  • ✅ Write Dockerfile to containerize it
  • ✅ Build a GitHub Actions workflow for CI
  • ✅ Automatically build + push Docker image on every commit

⚙️ Prerequisites

  • Basic knowledge of Node.js & Git
  • Docker installed on your system
  • GitHub account
  • Docker Hub account

📝 Step 1: Create a Simple Node.js App

mkdir devops-tutorial && cd devops-tutorial
npm init -y
npm install express

Createapp.js:

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello from DevOps Tutorial 🚀');
});

app.listen(port, () => {
  console.log(`App running on http://localhost:${port}`);
});

Updatepackage.json:

"scripts": {
  "start": "node app.js"
}

🐳 Step 2: Dockerize the App

FROM node:18-alpine

WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .

EXPOSE 3000
CMD ["npm", "start"]
docker build -t devops-tutorial .
docker run -p 3000:3000 devops-tutorial

Open http://localhost:3000 — you should see your app running!

🔗 Step 3: Push Code to GitHub

git init
git remote add origin https://github.com/YOUR_USERNAME/devops-tutorial.git
git add .
git commit -m "Initial commit"
git push -u origin master

🤖 Step 4: Set Up GitHub Actions CI

name: Build and Push Docker Image

on:
  push:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest

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

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@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
        uses: docker/build-push-action@v4
        with:
          push: true
          tags: yourdockerhubusername/devops-tutorial:latest

🚀 Final Step: Push Code to Trigger CI/CD

git add .
git commit -m "Add GitHub Actions CI"
git push

🎉 Result

https://hub.docker.com/repository/docker/yourdockerhubusername/devops-tutorial
docker pull yourdockerhubusername/devops-tutorial:latest
docker run -p 3000:3000 yourdockerhubusername/devops-tutorial

📌 Conclusion

  • ✅ How to Dockerize a Node.js app
  • ✅ Set up GitHub Actions for CI
  • ✅ Build and push Docker images automatically

This is a foundation for building a complete DevOps pipeline!