πŸš€ Day 13 of 30 Days DevOps Interview Preparation: Deploy Docker App via CI/CD

Series: 30 Days DevOps Interview Preparation

Author: Tathagat Gaikwad

In the DevOps world, one of the most common real-world tasks is packaging applications using Docker and automating deployments with CI/CD pipelines. Mastering this concept not only makes you job-ready but also helps you answer many DevOps interview questions with confidence.


πŸ”Ή Theoretical Foundations

1. What is Docker in DevOps?

Docker is a containerization platform that allows you to package an application with all its dependencies into a lightweight container. This ensures the app runs the same way regardless of where it’s deployed – developer laptop, testing server, or cloud.

2. Why CI/CD with Docker?

CI/CD (Continuous Integration and Continuous Deployment/Delivery) automates the building, testing, and deployment of code. When combined with Docker, it ensures:

  • βœ… Consistency across environments

  • βœ… Faster deployments with pre-built images

  • βœ… Rollback capabilities (by reverting to previous images)

  • βœ… Scalability when deployed on Kubernetes, ECS, or Swarm


πŸ”Ή Typical CI/CD Pipeline for a Dockerized App

Here’s the high-level flow of deploying a Docker app with CI/CD:

  1. Code Commit β†’ Developer pushes changes to GitHub/GitLab.

  2. Pipeline Triggered β†’ Jenkins / GitHub Actions / GitLab CI detects changes.

  3. Build Stage β†’ Application is containerized using a Dockerfile.

  4. Test Stage β†’ Run automated tests inside the container.

  5. Push Stage β†’ Push Docker image to container registry (Docker Hub, AWS ECR).

  6. Deploy Stage β†’ Deploy the containerized app to staging or production.


πŸ”Ή Practical Example – Deploying Docker App with GitHub Actions

Let’s walk through an example where we deploy a simple Node.js Docker app via GitHub Actions.

Step 1: Dockerfile

# Use Node.js base image
FROM node:18-alpine

# Set working directory
WORKDIR /app

# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install

# Copy source code
COPY . .

# Expose port
EXPOSE 3000

# Run the app
CMD ["npm", "start"]

Step 2: GitHub Actions Workflow (.github/workflows/docker-ci.yml)

name: CI/CD Pipeline for Docker App

on:
  push:
    branches: [ "main" ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Set up Docker
        uses: docker/setup-buildx-action@v2

      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build Docker Image
        run: docker build -t my-docker-user/devops-app:latest .

      - name: Push Docker Image to Docker Hub
        run: docker push my-docker-user/devops-app:latest

Step 3: Deploy to AWS EC2 (Example Command)

On the EC2 instance, pull the latest image and run:

docker pull my-docker-user/devops-app:latest
docker stop devops-app || true
docker rm devops-app || true
docker run -d -p 80:3000 --name devops-app my-docker-user/devops-app:latest

πŸ”Ή Interview Preparation – Detailed Q&A

Q1. Why do we use Docker in CI/CD pipelines?
πŸ‘‰ To ensure the application runs consistently across environments, with reduced dependency issues. It makes testing, building, and deployment reproducible.

Q2. Difference between CI and CD?

  • CI (Continuous Integration): Merges code changes, builds Docker images, and runs automated tests.

  • CD (Continuous Deployment/Delivery): Pushes tested images to production automatically (or with approval).

Q3. How do you optimize Docker builds in CI/CD?
πŸ‘‰ Use multi-stage builds, leverage caching, use smaller base images (e.g., alpine), and .dockerignore to reduce context size.

Q4. How do you handle secrets when deploying Docker apps?
πŸ‘‰ Store them in a secure system like AWS Secrets Manager, HashiCorp Vault, or CI/CD tool’s secret store (never hardcode in Dockerfiles).

Q5. What deployment strategies can you use with Docker apps?
πŸ‘‰ Blue-Green Deployment, Canary Releases, and Rolling Updates to ensure zero-downtime deployments.


πŸ”Ή Key Takeaways

  • Docker + CI/CD = speed, consistency, and scalability.

  • Automating builds and deployments reduces human error and increases reliability.

  • Be interview-ready with strong knowledge of pipelines, registries, and deployment strategies.


πŸ’‘ What CI/CD tool do you prefer for Docker deployments β€” Jenkins, GitHub Actions, or GitLab CI? Share your thoughts!

#DevOps #Docker #CICD #AWS #DevOpsInterviewPreparation #Hashnode

0
Subscribe to my newsletter

Read articles from Tathagat Gaikwad directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tathagat Gaikwad
Tathagat Gaikwad