DEV Community

Cover image for STEP 2: Automating Docker Build & Push with GitHub Actions
Adekunle Osatuyi
Adekunle Osatuyi

Posted on

STEP 2: Automating Docker Build & Push with GitHub Actions

STEP 1: Prepare Your GitHub Repo
On GitHub → create a new repo_
Clone it to the local system
git clone https://github.com//docker-automation-demo.git
cd docker-automation-demo
Add your app files and a Dockerfile to local directory
i. Dockerfile:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

ii. app.js

const http = require('http');
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.end('Hello, LUbemart is done with Docker!\n');
});

server.listen(port, "0.0.0.0", () => {
console.log(Server running on port ${port});
});

iii. package.json

{
"name": "docker-lab",
"version": "1.0.0",
"main": "app.js",
"scripts": {
"start": "node app.js"
}
}

iv. Commit files:
git add .
git commit -m "New Dockerfile for automation"
git push origin main

Step 2: Add GitHub Secrets for ACR
From the GitHub repo:
Go to Settings → Secrets and variables → Actions → New repository secret
Add these secrets:
Secret Name Description

AZURE_CONTAINER_REGISTRY ACR name

ACR_LOGIN_SERVER ACR login server

ACR_USERNAME ACR admin username (copy from credentials)

ACR_PASSWORD ACR admin password (copy from credentials)

NB:Fetch credentials with:
az acr credential show --name

Step 3: Create a GitHub Actions Workflow
In your repo, create a new file:
.github/workflows/docker-build.yml

PASTE-INSIDE-FILE
on:
push:
branches:
- main

env:
IMAGE_NAME: myapp

jobs:
build-and-push:
runs-on: ubuntu-latest

steps:
  - name: Checkout source code
    uses: actions/checkout@v4

  - name: Login to Azure Container Registry
    run: |
      echo "${{ secrets.ACR_PASSWORD }}" | \
      docker login ${{ secrets.ACR_LOGIN_SERVER }} \
      -u ${{ secrets.ACR_USERNAME }} \
      --password-stdin

  - name: Build Docker image
    run: |
      docker build \
        -t ${{ secrets.ACR_LOGIN_SERVER }}/${{ env.IMAGE_NAME }}:${{ github.sha }} \
        -t ${{ secrets.ACR_LOGIN_SERVER }}/${{ env.IMAGE_NAME }}:latest \
        .

  - name: Push Docker image
    run: |
      docker push ${{ secrets.ACR_LOGIN_SERVER }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
      docker push ${{ secrets.ACR_LOGIN_SERVER }}/${{ env.IMAGE_NAME }}:latest
Enter fullscreen mode Exit fullscreen mode

Step 4: Commit and Push Workflow
git add .github/workflows/docker-build.yml
git commit -m "Add GitHub Actions workflow for ACR push"
git push origin main

Step 5: Go to your GitHub repo → Actions tab
Check for a new workflow triggered on your latest commit.
Inside — you have the following steps:
Checkout code
Login to ACR
Build Docker image
Push to ACR

Step 6: Verify in ACR
az acr repository list --name --output table
You’ll now see:

Result

myapp

Note--> At this level, every code pushed to main triggers an image build + push to Azure Container Registry.

Top comments (0)