Day 2 of My Devops: 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
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
Initialize Git Repository:
- Initialize Git for version control:
git init
Write Your Application Code:
- Create a basic Node.js application. For example, create an
app.js
file:
- Create a basic Node.js application. For example, create an
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}/`);
});
Create a Dockerfile:
- Define a
Dockerfile
in the project root:
- Define a
Dockerfile:
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]
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
Create a GitHub Repository:
- Visit GitHub and create a new repository (
my-docker-app
).
- Visit GitHub and create a new repository (
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
Create a Workflow File:
- Inside
.github/workflows
, createdocker-build-deploy.yml
:
- Inside
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
Add GitHub Secrets:
Go to your GitHub repository > Settings > Secrets.
Add Docker Hub credentials (
DOCKER_USERNAME
andDOCKER_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:
Docker Basics: Get Started with Docker
GitHub Actions: Explore GitHub Actions Documentation
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!