Overview
This reference documents all Docker commands used in the Jenkins CI/CD pipeline, including build and run commands with their complete flags and options.Docker Build Command
The Docker build command creates a Docker image from the Dockerfile in the repository.Command Syntax
Full Example
Build Command Parameters
Tag the image with a name and optional tag in the format
name:tag. In this pipeline, the image is tagged as nodejs-demo-app:latest.The name of the Docker image. This value is set as an environment variable in the Jenkinsfile.
The tag applied to the Docker image. Using
latest indicates this is the most recent version.The build context path. The dot (
.) indicates the current directory, where the Dockerfile is located.Build Process
When executed, the build command:- Reads the Dockerfile from the current directory
- Pulls the base image
node:18-alpine - Sets working directory to
/app - Copies
package*.jsonfiles - Runs
npm installto install dependencies - Copies all application files
- Exposes port 3000
- Sets the default command to
node index.js
Docker Run Command
The Docker run command creates and starts a container from the built image.Command Syntax
Full Example
Run Command Parameters
Assigns a name to the container. In this pipeline, the container is named
Jenkins for easy identification and management.Detached mode. Runs the container in the background and prints the container ID. This allows the Jenkins pipeline to continue without waiting for the container to stop.
Port mapping in the format
HOST_PORT:CONTAINER_PORT. Maps port 80 on the host machine to port 3000 inside the container, making the application accessible on port 80.The image to run. Uses the image built in the Build stage with the
latest tag.Port Mapping Details
The port on the host machine where the application will be accessible. Port 80 is the standard HTTP port.
The port inside the container where the Node.js application is listening. This matches the port exposed in the Dockerfile and used by the Express app.
Complete Docker Workflow
Step 1: Build the Image
- Reads the Dockerfile
- Executes each instruction in the Dockerfile
- Creates a new image layer for each instruction
- Tags the final image as
nodejs-demo-app:latest
Step 2: Run the Container
- Creates a new container from the
nodejs-demo-app:latestimage - Names the container
Jenkins - Runs it in detached mode (background)
- Maps port 80 (host) to port 3000 (container)
- Starts the Node.js application inside the container
Dockerfile Reference
The Dockerfile used to build the image:Dockerfile Instructions
Base image for the Docker container. Uses Node.js version 18 on Alpine Linux for a minimal image size.
Sets the working directory inside the container to
/app. All subsequent commands execute in this directory.Copies
package.json and package-lock.json to the working directory. This is done before copying other files to leverage Docker layer caching.Installs Node.js dependencies defined in
package.json.Copies all application files from the build context to the working directory in the container.
Documents that the container listens on port 3000. This is informational and does not actually publish the port.
The default command to run when the container starts. Executes
node index.js to start the Express application.