Beginner's Guide to Deploying with Docker and GitHub Actions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyrinNew
    Senior Member
    • Feb 2024
    • 5175

    #1

    Beginner's Guide to Deploying with Docker and GitHub Actions

    Hey, Dhruv, this side.


    Scared of DevOps?


    If you're a developer or someone just starting out in tech, you’ve probably heard the words Docker and GitHub Actions floating around. And you’ve probably nodded your head pretending to understand. I get it. It sounds scary.


    But here’s the thing. It’s not.

    This guide is for you if:
    • You’ve built something cool
    • You want to put it online
    • You want it to run on its own


    And you have no idea what any of that means


    No worries. I’ll walk you through it like a friend. Easy words. Real talk. Honest steps.


    What You’ll Learn
    • What Docker is and why it matters
    • What GitHub Actions does and how it helps
    • How to use both together to automate your app deployment
    • And how to feel like a DevOps hero without feeling like a total noob


    Let’s go step by step.

    What Is Docker?

    Let’s say you build an app that works perfectly on your machine.


    Now, someone else tries it on their computer. And boom — errors everywhere. Missing dependencies. Wrong version of Node. Chaos.


    That’s where Docker comes in.


    Docker lets you pack your app into a little container. That container has everything your app needs — code, environment, tools — all in one neat package.


    Now, anyone can run that container, and your app works the same everywhere like magic.


    Think of it as a shipping box. Whatever is inside stays the same, no matter where you send it.

    What Is GitHub Actions?

    GitHub Actions is like your personal robot assistant.


    It watches your code. And every time you push something new, it can do things for you.


    For example:
    • Run tests
    • Build your app
    • Deploy your app
    • Send a tweet (yes, really)
    • And you don’t even have to be there. It just works.


    You set up a file (called a workflow), and GitHub does the rest. Automatically. On every push.


    This is called CI/CD. That means Continuous Integration and Continuous Deployment.


    In short: GitHub Actions saves time. And makes you look like a genius.


    Why Use Docker with GitHub Actions?

    Because now you can do this:
    • Write code
    • Push to GitHub
    • GitHub builds your Docker container
    • It pushes that container to a server
    • Your app is live. No hands needed


    You get full control. Zero stress. Full automation.


    And it makes you more confident every time you ship code.


    Let’s Build Something Together

    Let’s build a simple Node.js app and deploy it using Docker and GitHub Actions.


    It’ll just say "Hello from Docker and GitHub Actions" — but it’s the journey that counts.

    Step 1: Create a Simple Node App

    Create a new folder and run:






    npm init -y








    Now make a file called index.js:






    const http = require('http');

    const server = http.createServer((req, res) => {
    res.end('Hello from Docker and GitHub Actions');
    });

    server.listen(3000, () => {
    console.log('Server is running on port 3000');
    });








    Add a start script in your package.json:






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








    Done. You now have a tiny server.


    Test it:






    node index.js








    Visit http://localhost:3000 and you’ll see the message.


    Step 2: Write a Dockerfile

    Create a file called Dockerfile (no extension).


    Inside, write:






    FROM node:18

    WORKDIR /app

    COPY package*.json ./

    RUN npm install

    COPY . .

    EXPOSE 3000

    CMD ["npm", "start"]








    Let’s break it down:
    • We start from a Node image
    • We set the working folder
    • We copy the package files and install dependencies
    • Then we copy the rest of the code
    • We tell Docker to open port 3000
    • And we tell it how to start the app


    Nice and clean.


    Step 3: Build and Run with Docker (Locally)

    Now let’s test if our container works.


    Run these commands:






    docker build -t my-node-app .
    docker run -p 3000:3000 my-node-app








    Visit http://localhost:3000 again.


    You should see the same message — but now it’s running inside Docker.


    Magic, right?


    Step 4: Push Your Code to GitHub

    Create a GitHub repo.


    If you’re using the terminal:






    git init
    git add .
    git commit -m "first commit"
    git branch -M main
    git remote add origin
    git push -u origin main








    Your code is now on GitHub. Now the fun begins.


    Step 5: Add a GitHub Actions Workflow

    Create a folder called .github/workflows


    Inside it, create a file called deploy.yml


    Here’s what you put in it:






    name: Build and Push Docker Image

    on:
    push:
    branches:
    - main

    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 DockerHub
    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@v5
    with:
    push: true
    tags: yourusername/my-node-app:latest








    Now let’s explain.
    • It runs every time you push to main
    • It checks out your code
    • It logs in to DockerHub using secrets (we’ll set these next)
    • It builds your Docker image and pushes it to DockerHub


    Step 6: Set Up DockerHub

    Go to https://hub.docker.com and create an account if you don’t have one.


    Make a new repository: my-node-app


    Now go to your GitHub repo → Settings → Secrets and variables → Actions


    Click New repository secret


    Add:
    • DOCKER_USERNAME with your DockerHub username
    • DOCKER_PASSWORD with your DockerHub password or access token


    Done.


    Now every push to main will build your image and send it to DockerHub.


    Step 7: Deploy Somewhere (Optional)

    You can use any platform that supports Docker.


    Some popular ones:
    • DigitalOcean
    • Railway
    • Render
    • Fly.io
    • Heroku (with Docker support)


    Each one has its own steps. But the point is — your app is now containerized. So it works anywhere Docker is supported.


    And GitHub Actions makes it happen automatically.


    Let’s Recap

    You:
    • Built a simple app
    • Put it in a Docker container
    • Uploaded it to GitHub
    • Set up GitHub Actions to build and push your image
    • Linked it all together with DockerHub


    All that, without writing a single bash script or touching a production server.


    Feels good, doesn’t it?


    Why This Matters

    Because you’ve just touched the heart of modern DevOps.


    You’ve built something real. Automated it. And shipped it like a pro.


    It might seem small now, but this is how real companies work.


    And now you’re part of that world.


    Extra Tips for Growth

    Want to go even further?


    Here are a few bonus moves:


    1. Add Testing

    Use GitHub Actions to run tests before building. That way, broken code never gets deployed.


    2. Use .env Files

    You can store environment variables and pass them into Docker using a .env file.


    3. Deploy to Production

    Use Render or Fly.io to deploy your container and have it online 24/7.


    4. Monitor Your Workflow

    Check the Actions tab in GitHub to see logs, status, and history.


    5. Learn YML

    The workflow files are written in YAML (yml). It’s worth getting familiar with it. Simple language. Big power.


    What You Now Know

    You know how to:
    • Use Docker to package your app
    • Use GitHub Actions to automate your builds
    • Push code and let bots do the heavy lifting
    • Feel a little more confident in your DevOps skills


    You’re ahead of many developers already.


    Final Thoughts

    This was just a taste. But it’s an important one.


    The world of coding is changing fast. Automation, containers, and cloud platforms are the future.


    And you’re right in the middle of it now.


    So build. Push. Break things. Fix them. Repeat.


    You’ll be amazed at how far you go.


    If You Found This Helpful

    Share it. Bookmark it. Come back to it.


    Or even better — take this guide, tweak the app, and deploy your own version.


    Make it your own.


    And hey, if you publish something cool, I’d love to see it.


    You’ve got this.


    Reach me!




    More...
Working...