Skip to main content

Dockerfile Configuration

The application uses a multi-stage Dockerfile to create an optimized container for the Node.js application.

Base Image

The pipeline uses the official Node.js Alpine Linux image for a lightweight container:
FROM node:18-alpine
node:18-alpine
string
Base Image: Node.js version 18 on Alpine Linux
  • Node.js version: 18 LTS
  • OS: Alpine Linux (minimal footprint)
  • Benefits: Smaller image size, faster builds, reduced attack surface

Container Configuration

Working Directory

The application files are stored in /app within the container:
WORKDIR /app

File Copying and Dependencies

# Copy package files first for layer caching
COPY package*.json ./
RUN npm install

# Copy application source code
COPY . .

Exposed Ports

EXPOSE
number
default:"3000"
The internal container port where the Node.js application listens.
EXPOSE 3000

Application Startup

The container runs the Node.js application using:
CMD ["node", "index.js"]

Port Mapping

When the container is deployed, the Jenkins pipeline maps ports as follows:
External Port
number
default:"80"
The port accessible from outside the container (public-facing).
Internal Port
number
default:"3000"
The port where the Node.js application runs inside the container.

Port Mapping Configuration

The deployment stage maps port 80 (external) to port 3000 (internal):
docker run --name Jenkins -d -p 80:3000 $IMAGE_NAME:latest
Access the application:
  • External: http://<server-ip>:80 or http://<server-ip>
  • Internal: http://localhost:3000 (within container)

Complete Dockerfile

FROM node:18-alpine

# Set working directory
WORKDIR /app

# Copy files
COPY package*.json ./
RUN npm install
COPY . .

# Expose port
EXPOSE 3000

# Run app
CMD ["node", "index.js"]

Docker Build Process

The Jenkins pipeline builds the Docker image in the Build stage:
stage('Build') {
    steps {
        echo 'Building Docker image...'
        sh 'docker build -t $IMAGE_NAME:latest .'
        echo 'Docker image built successfully!'
    }
}

Customization Options

Changing Node.js Version

To use a different Node.js version, modify the base image:
FROM node:20-alpine  # Use Node.js 20
FROM node:18-slim    # Use Debian-based image

Changing Port Mappings

Modify the deploy command in the Jenkinsfile:
sh 'docker run --name Jenkins -d -p 8080:3000 $IMAGE_NAME:latest'

Build docs developers (and LLMs) love