Mohammad Ranjbar Z
2 min readJan 31, 2021

--

CI/CD For Dockerized Applications Using Github Action And Github Container Registry

If you want to create a CI/CD pipeline in Github ecosystem you can follow this tutorial, in this tutorial I cover these topics

  • Create a simple Nodejs application
  • Write Dockerfile for this project
  • Create Github actions pipeline for run build and tests scripts( if available)
  • Build docker image for project and push to Github container registry
  • Use Docker image

This is the repository that contains all codes of this https://github.com/mohammadranjbarz/github-actions-and-github-packages-integration

  1. Create Simple Express Nodejs application
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World')
})
app.listen(3000, ()=>{
console.log("Listening to port 3000")
})

2. Write Dockerfile
Create a file named Dockerfile in project with these lines of codes

FROM node:10-alpineWORKDIR /usr/src/appCOPY ./src ./src
COPY package*.json ./
# Don't install dev dependencies
RUN npm ci --only=production
CMD node ./src/index.js

3. Write a github actions config for run tests then build-publish docker images,
name: CI/CD

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: 10.x
- name: install dependencies
run: npm ci
- name: Run tests
run: npm run test

publish:
if: github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/master'
needs: test
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Push to GitHub Packages
uses: docker/build-push-action@v1
with:
username: ${{ github.actor }}
password: ${{ github.token }}
registry: ghcr.io
repository: mohammadranjbarz/ci-cd-tutorial
tag_with_sha: true
tag_with_ref: true

You can see the Github Actions jobs here :
https://github.com/mohammadranjbarz/github-actions-and-github-packages-integration/actions

4. Use Docker image

  • You can write a docker-compose.yml file with below content in anywhere that you want ( for example in your server,…)
version: '3.3'

services:
application:
image: ghcr.io/mohammadranjbarz/ci-cd-tutorial:latest
container_name: application
ports:
- 3000:3000

Then you can run docker with this command:

docker-compose up
  • Or if you prefer not using docker-compose you can run it with this command:
docker run -p 3000:3000 ghcr.io/mohammadranjbarz/ci-cd-tutorial:latest
  • For test to see if everything is right after running docker you can tun this command
curl http://localhost:3000

--

--