DEV Community

Aisalkyn Aidarova
Aisalkyn Aidarova

Posted on

LAB 2 — Docker + Newman (REAL DEVOPS PIPELINE)

🎯 What you will learn

  • Run API in Docker
  • Test API using Newman
  • Simulate real CI/CD validation
  • Understand why pipelines fail

🔥 1. PROJECT STRUCTURE

docker-newman-lab/
├── app/
│   └── server.js
├── Dockerfile
├── collection.json
├── environment.json
├── package.json
└── .github/
    └── workflows/
        └── api-test.yml
Enter fullscreen mode Exit fullscreen mode

🔥 2. STEP 1 — CREATE SIMPLE API

📄 app/server.js

const express = require("express");
const app = express();

app.use(express.json());

app.get("/health", (req, res) => {
    res.status(200).json({ status: "UP" });
});

app.post("/users", (req, res) => {
    const { name, job } = req.body;

    if (!name || !job) {
        return res.status(400).json({ error: "Missing fields" });
    }

    res.status(201).json({
        id: Date.now(),
        name,
        job
    });
});

app.listen(3000, () => {
    console.log("Server running on port 3000");
});
Enter fullscreen mode Exit fullscreen mode

🔥 3. STEP 2 — PACKAGE FILE

📄 package.json

{
  "name": "devops-api",
  "version": "1.0.0",
  "main": "app/server.js",
  "scripts": {
    "start": "node app/server.js"
  },
  "dependencies": {
    "express": "^4.18.2"
  }
}
Enter fullscreen mode Exit fullscreen mode

🔥 4. STEP 3 — DOCKERFILE

📄 Dockerfile

FROM node:18

WORKDIR /app

COPY package.json .

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode

🔥 5. STEP 4 — BUILD & RUN

docker build -t devops-api .
docker run -d -p 3000:3000 devops-api
Enter fullscreen mode Exit fullscreen mode

Test:

curl http://localhost:3000/health
Enter fullscreen mode Exit fullscreen mode

Expected:

{"status":"UP"}
Enter fullscreen mode Exit fullscreen mode

🔥 6. STEP 5 — POSTMAN COLLECTION

📄 collection.json

Request 1 — Health

GET {{base_url}}/health
Enter fullscreen mode Exit fullscreen mode

Tests:

pm.test("Status 200", function () {
    pm.response.to.have.status(200);
});
Enter fullscreen mode Exit fullscreen mode

Request 2 — Create User

POST {{base_url}}/users
Enter fullscreen mode Exit fullscreen mode

Body:

{
  "name": "Aisalkyn",
  "job": "DevOps Engineer"
}
Enter fullscreen mode Exit fullscreen mode

Tests:

pm.test("User created", function () {
    pm.response.to.have.status(201);
});

pm.test("Response has name", function () {
    let json = pm.response.json();
    pm.expect(json.name).to.eql("Aisalkyn");
});
Enter fullscreen mode Exit fullscreen mode

🔥 7. STEP 6 — ENVIRONMENT FILE

📄 environment.json

{
  "id": "env-id",
  "name": "local",
  "values": [
    {
      "key": "base_url",
      "value": "http://localhost:3000",
      "enabled": true
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

🔥 8. STEP 7 — RUN NEWMAN LOCALLY

npm install -g newman

newman run collection.json -e environment.json
Enter fullscreen mode Exit fullscreen mode

🔥 9. STEP 8 — GITHUB ACTIONS (REAL CI/CD)

📄 .github/workflows/api-test.yml

name: API Docker Test

on:
  push:
    branches: [ main ]

jobs:
  test-api:
    runs-on: ubuntu-latest

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

      - name: Start Docker container
        run: |
          docker build -t devops-api .
          docker run -d -p 3000:3000 devops-api

      - name: Wait for API
        run: sleep 10

      - name: Install Newman
        run: npm install -g newman

      - name: Run Tests
        run: newman run collection.json -e environment.json
Enter fullscreen mode Exit fullscreen mode

🔥 10. WHY THIS LAB IS IMPORTANT (REAL DEVOPS)

You are simulating:

Code → Docker → Deploy → Test → Validate
Enter fullscreen mode Exit fullscreen mode

🔥 11. COMMON FAILURES (VERY IMPORTANT)

❌ 1. API not ready

Pipeline runs too fast

Fix:

sleep 10
Enter fullscreen mode Exit fullscreen mode

❌ 2. Wrong base_url

localhost ≠ container network
Enter fullscreen mode Exit fullscreen mode

❌ 3. Port not exposed

Docker missing:

EXPOSE 3000
Enter fullscreen mode Exit fullscreen mode

❌ 4. Container crashed

Check logs:

docker logs <container_id>
Enter fullscreen mode Exit fullscreen mode

🔥 12. WHAT YOU TELL INTERVIEWER

Short answer (perfect):

"We deploy application in Docker, then run Newman tests to validate API endpoints like health, functional, and response validation. If tests fail, pipeline blocks deployment."


🔥 13. WHAT YOU GIVE TO MANAGER

Example:

  • Deployment Status: SUCCESS
  • API Health: PASS
  • Functional Tests: PASS
  • Response Time: 220ms
  • Failures: 0

Top comments (0)