Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
FROM node:24-alpine
# Used stable version-LTS
FROM node:20-alpine

# Create app user and group
RUN addgroup app && adduser -S -G app app
# Set working directory inside container
WORKDIR /app

WORKDIR /usr/src/app

# Copy package files and install dependencies as root
# Copy package files first
COPY package*.json ./
RUN npm ci --omit=dev

# Copy rest of the app
COPY . .
# Install dependencies-into image
RUN npm install

# Switch to non-root user for runtime
USER app
# Copy remaining files
COPY . .

# Expose application port inside container
EXPOSE 3000
CMD ["node", "app.js"]
# Start the script inside package.json-when container runs
CMD ["npm", "start"]

53 changes: 53 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
pipeline {
agent any // In this case windows local

environment { // for reusability and clean code
IMAGE_NAME = "my-node-app2"
CONTAINER_NAME = "node-app-container2"
PORT = "3000"
}

stages { // contains all phases of CI-CD pipeline

stage('Pull Code') {
steps {
git( // provided by git plugins
branch: 'dev',
url: 'https://github.com/harshitmishra25/sample-node-project'
)
}
}

stage('Build Docker Image') {
steps { // image
bat 'docker build -t %IMAGE_NAME% .'
}
}

stage('Stop & Remove Old Container') {
steps {
bat '''
docker stop %CONTAINER_NAME% || exit 0
docker rm %CONTAINER_NAME% || exit 0
'''
}
}

stage('Run Docker Container') {
steps { // container
bat '''
docker run -d -p %PORT%:%PORT% --name %CONTAINER_NAME% %IMAGE_NAME%
'''
}
}
}

post { // runs after pipeline finishes(success/failure)
success {
echo "Pipeline completed successfully"
}
failure {
echo "Pipeline failed"
}
}
}